用EasyX实现简单动画—反弹球

#include<graphics.h>
#include<conio.h>
#pragma warning(disable:4996)

#define High 600
#define Wigh 800

int main()
{
	initgraph(Wigh, High);
	
	  
	float ball_x, ball_y;	//小球的坐标
	float ball_vx, ball_vy;	//小球的速度
	float radius;			//小球的半径

	
	ball_x = Wigh / 2;		//初始化
	ball_y = High / 2;
	ball_vx = 1;
	ball_vy = 1;
	radius = 20;

	while(1)
	{
		setcolor(BLACK);		//用黑线黑球掩盖上一轮的黄线绿球	
		setfillcolor(BLACK);
		fillcircle(ball_x, ball_y,radius);

		ball_x += ball_vx;	//小球的坐标变化
		ball_y += ball_vy;

		//小球碰壁反弹
		if ((ball_x <= radius) || (ball_x >= Wigh - radius))
			ball_vx = -ball_vx;
		if ((ball_y <= radius) || (ball_y >= High - radius))
			ball_vy = -ball_vy;


		//画出黄线绿球
		setcolor(YELLOW);
		setfillcolor(GREEN);
		fillcircle(ball_x, ball_y, radius);

		//与下面的EndBatchDraw()一起,解决小球循环时画面闪烁的问题
		FlushBatchDraw();

		//延时
		Sleep(0.9);

	}
	EndBatchDraw();
	closegraph();				
	return 0;

}

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
弹球游戏和消方块游戏都是比较经典的小游戏,使用easyx库可以很方便地实现这两个游戏。下面是弹球游戏和消方块游戏的要求: 弹球游戏要求: 1. 画出一个小球和一个挡板; 2. 小球从挡板上方开始运动,碰到边界会弹; 3. 小球碰到挡板会弹,如果小球落到挡板下方,则游戏结束; 4. 挡板可以左右移动,通过键盘控制; 5. 当小球碰到砖块时,砖块会消失,小球会弹; 6. 当所有砖块都被消除时,游戏胜利。 消方块游戏要求: 1. 画出一个矩形区域,里面有多个小矩形组成的砖块; 2. 有一个小球从上方开始运动,碰到边界会弹; 3. 有一个挡板可以左右移动,通过键盘控制; 4. 当小球碰到砖块时,砖块会消失,小球会弹; 5. 当所有砖块都被消除时,游戏胜利; 6. 如果小球落到挡板下方,则游戏结束。 下面是弹球游戏的代码示例: ```cpp #include <graphics.h> #include <conio.h> #include <time.h> #include <stdlib.h> #define BALL_RADIUS 10 #define BLOCK_WIDTH 50 #define BLOCK_HEIGHT 20 #define BLOCK_ROWS 5 #define BLOCK_COLS 10 #define BOARD_WIDTH 100 #define BOARD_HEIGHT 10 #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 int blocks[BLOCK_ROWS][BLOCK_COLS] = {0}; int score = 0; void initBlocks() { for (int i = 0; i < BLOCK_ROWS; i++) { for (int j = 0; j < BLOCK_COLS; j++) { blocks[i][j] = 1; } } } void drawBlocks() { for (int i = 0; i < BLOCK_ROWS; i++) { for (int j = 0; j < BLOCK_COLS; j++) { if (blocks[i][j] == 1) { int x = j * BLOCK_WIDTH; int y = i * BLOCK_HEIGHT; setfillcolor(RGB(255, 0, 0)); fillrectangle(x, y, x + BLOCK_WIDTH, y + BLOCK_HEIGHT); } } } } void drawBoard(int x) { setfillcolor(RGB(0, 255, 0)); fillrectangle(x, WINDOW_HEIGHT - BOARD_HEIGHT, x + BOARD_WIDTH, WINDOW_HEIGHT); } void drawBall(int x, int y) { setfillcolor(RGB(0, 0, 255)); fillellipse(x, y, BALL_RADIUS, BALL_RADIUS); } bool isCollide(int x, int y) { if (y + BALL_RADIUS >= WINDOW_HEIGHT - BOARD_HEIGHT && y + BALL_RADIUS <= WINDOW_HEIGHT) { if (x + BALL_RADIUS >= boardX && x - BALL_RADIUS <= boardX + BOARD_WIDTH) { return true; } } return false; } bool isCollideBlock(int x, int y, int &row, int &col) { row = (y - BALL_RADIUS) / BLOCK_HEIGHT; col = x / BLOCK_WIDTH; if (row >= 0 && row < BLOCK_ROWS && col >= 0 && col < BLOCK_COLS && blocks[row][col] == 1) { return true; } return false; } void updateBall(int &x, int &y, int &vx, int &vy) { x += vx; y += vy; if (x - BALL_RADIUS <= 0 || x + BALL_RADIUS >= WINDOW_WIDTH) { vx = -vx; } if (y - BALL_RADIUS <= 0) { vy = -vy; } if (y + BALL_RADIUS >= WINDOW_HEIGHT) { exit(0); } if (isCollide(x, y)) { vy = -vy; } int row, col; if (isCollideBlock(x, y, row, col)) { blocks[row][col] = 0; score++; vy = -vy; } } int main() { initgraph(WINDOW_WIDTH, WINDOW_HEIGHT); initBlocks(); int ballX = WINDOW_WIDTH / 2; int ballY = WINDOW_HEIGHT / 2; int ballVX = 5; int ballVY = 5; int boardX = WINDOW_WIDTH / 2 - BOARD_WIDTH / 2; while (true) { cleardevice(); drawBlocks(); drawBoard(boardX); drawBall(ballX, ballY); updateBall(ballX, ballY, ballVX, ballVY); if (score == BLOCK_ROWS * BLOCK_COLS) { MessageBox(NULL, TEXT("You Win!"), TEXT("Message"), MB_OK); break; } if (_kbhit()) { int key = _getch(); if (key == 'a' && boardX > 0) { boardX -= 10; } if (key == 'd' && boardX + BOARD_WIDTH < WINDOW_WIDTH) { boardX += 10; } } Sleep(20); } closegraph(); return 0; } ``` 下面是消方块游戏的代码示例: ```cpp #include <graphics.h> #include <conio.h> #include <time.h> #include <stdlib.h> #define BALL_RADIUS 10 #define BLOCK_WIDTH 50 #define BLOCK_HEIGHT 20 #define BLOCK_ROWS 5 #define BLOCK_COLS 10 #define BOARD_WIDTH 100 #define BOARD_HEIGHT 10 #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 int blocks[BLOCK_ROWS][BLOCK_COLS] = {0}; int score = 0; void initBlocks() { for (int i = 0; i < BLOCK_ROWS; i++) { for (int j = 0; j < BLOCK_COLS; j++) { blocks[i][j] = rand() % 2; } } } void drawBlocks() { for (int i = 0; i < BLOCK_ROWS; i++) { for (int j = 0; j < BLOCK_COLS; j++) { if (blocks[i][j] == 1) { int x = j * BLOCK_WIDTH; int y = i * BLOCK_HEIGHT; setfillcolor(RGB(255, 0, 0)); fillrectangle(x, y, x + BLOCK_WIDTH, y + BLOCK_HEIGHT); } } } } void drawBoard(int x) { setfillcolor(RGB(0, 255, 0)); fillrectangle(x, WINDOW_HEIGHT - BOARD_HEIGHT, x + BOARD_WIDTH, WINDOW_HEIGHT); } void drawBall(int x, int y) { setfillcolor(RGB(0, 0, 255)); fillellipse(x, y, BALL_RADIUS, BALL_RADIUS); } bool isCollide(int x, int y) { if (y + BALL_RADIUS >= WINDOW_HEIGHT - BOARD_HEIGHT && y + BALL_RADIUS <= WINDOW_HEIGHT) { if (x + BALL_RADIUS >= boardX && x - BALL_RADIUS <= boardX + BOARD_WIDTH) { return true; } } return false; } bool isCollideBlock(int x, int y, int &row, int &col) { row = (y - BALL_RADIUS) / BLOCK_HEIGHT; col = x / BLOCK_WIDTH; if (row >= 0 && row < BLOCK_ROWS && col >= 0 && col < BLOCK_COLS && blocks[row][col] == 1) { return true; } return false; } void updateBall(int &x, int &y, int &vx, int &vy) { x += vx; y += vy; if (x - BALL_RADIUS <= 0 || x + BALL_RADIUS >= WINDOW_WIDTH) { vx = -vx; } if (y - BALL_RADIUS <= 0) { vy = -vy; } if (y + BALL_RADIUS >= WINDOW_HEIGHT) { exit(0); } if (isCollide(x, y)) { vy = -vy; } int row, col; if (isCollideBlock(x, y, row, col)) { blocks[row][col] = 0; score++; vy = -vy; } } int main() { initgraph(WINDOW_WIDTH, WINDOW_HEIGHT); srand(time(NULL)); initBlocks(); int ballX = WINDOW_WIDTH / 2; int ballY = WINDOW_HEIGHT / 2; int ballVX = 5; int ballVY = 5; int boardX = WINDOW_WIDTH / 2 - BOARD_WIDTH / 2; while (true) { cleardevice(); drawBlocks(); drawBoard(boardX); drawBall(ballX, ballY); updateBall(ballX, ballY, ballVX, ballVY); if (score == BLOCK_ROWS * BLOCK_COLS) { MessageBox(NULL, TEXT("You Win!"), TEXT("Message"), MB_OK); break; } if (_kbhit()) { int key = _getch(); if (key == 'a' && boardX > 0) { boardX -= 10; } if (key == 'd' && boardX + BOARD_WIDTH < WINDOW_WIDTH) { boardX += 10; } } Sleep(20); } closegraph(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值