EasyX绘图实现反弹球消砖块

编程入门 行业动态 更新时间:2024-10-07 12:21:31

EasyX绘图实现反弹球消<a href=https://www.elefans.com/category/jswz/34/1728855.html style=砖块"/>

EasyX绘图实现反弹球消砖块

知识点:数组、循环、函数调用、鼠标交互

参考:《C语言课程设计与游戏开发实践教程》童晶

做游戏,学编程(C语言)

#include <conio.h>
#include <graphics.h>#define High 480  // 游戏画面尺寸
#define Width 640
#define Brick_num 10 // 砖块个数// 全局变量
int ball_x, ball_y; // 小球的坐标
int ball_vx, ball_vy; // 小球的速度
int radius;  // 小球的半径
int bar_x, bar_y; // 挡板中心坐标
int bar_high, bar_width;  // 挡板的高度和宽度
int bar_left, bar_right, bar_top, bar_bottom; // 挡板的上下左右位置坐标int isBrickExisted[Brick_num]; // 每个砖块是否存在,1为存在,0为没有了
int brick_high, brick_width; // 每个砖块的高度和宽度void startup()  // 数据初始化
{ball_x = Width / 2;ball_y = High / 2;ball_vx = 1;ball_vy = 1;radius = 20;bar_high = High / 20;bar_width = Width / 2;bar_x = Width / 2;bar_y = High - bar_high / 2;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;brick_width = Width / Brick_num;brick_high = High / Brick_num;int i;for (i = 0; i < Brick_num; i++)isBrickExisted[i] = 1;initgraph(Width, High);BeginBatchDraw();
}void clean()  // 消除画面
{setcolor(BLACK);setfillcolor(BLACK);fillcircle(ball_x, ball_y, radius); 	// 绘制黑线、黑色填充的圆bar(bar_left, bar_top, bar_right, bar_bottom);	// 绘制黑线、黑色填充的挡板int i, brick_left, brick_right, brick_top, brick_bottom;for (i = 0; i < Brick_num; i++){brick_left = i * brick_width;brick_right = brick_left + brick_width;brick_top = 0;brick_bottom = brick_high;if (!isBrickExisted[i])	 // 砖块没有了,绘制黑色fillrectangle(brick_left, brick_top, brick_right, brick_bottom);}
}void show()  // 显示画面
{setcolor(YELLOW);setfillcolor(GREEN);fillcircle(ball_x, ball_y, radius);	// 绘制黄线、绿色填充的圆bar(bar_left, bar_top, bar_right, bar_bottom);	// 绘制黄线、绿色填充的挡板int i, brick_left, brick_right, brick_top, brick_bottom;for (i = 0; i < Brick_num; i++){brick_left = i * brick_width;brick_right = brick_left + brick_width;brick_top = 0;brick_bottom = brick_high;if (isBrickExisted[i])	 // 砖块存在,绘制砖块{setcolor(WHITE);setfillcolor(RED);fillrectangle(brick_left, brick_top, brick_right, brick_bottom);	// 绘制砖块}}FlushBatchDraw();// 延时Sleep(3);
}void updateWithoutInput()  // 与用户输入无关的更新
{// 挡板和小圆碰撞,小圆反弹;分小球在挡板上面和下面两种情况if (((ball_y + radius >= bar_top) && (ball_y + radius < bar_bottom - bar_high / 3))|| ((ball_y - radius <= bar_bottom) && (ball_y - radius > bar_top - bar_high / 3)))if ((ball_x >= bar_left) && (ball_x <= bar_right))ball_vy = -ball_vy;// 更新小圆坐标ball_x = ball_x + ball_vx;ball_y = ball_y + ball_vy;// 小圆和边界碰撞if ((ball_x == radius) || (ball_x == Width - radius))ball_vx = -ball_vx;if ((ball_y == radius) || (ball_y == High - radius))ball_vy = -ball_vy;// 判断小圆是否和某个砖块碰撞int i, brick_left, brick_right, brick_bottom;for (i = 0; i < Brick_num; i++){if (isBrickExisted[i])	 // 砖块存在,才判断{brick_left = i * brick_width;brick_right = brick_left + brick_width;brick_bottom = brick_high;if ((ball_y <= brick_bottom + radius) && (ball_x >= brick_left) && (ball_x <= brick_right)){isBrickExisted[i] = 0;ball_vy = -ball_vy;}}}
}void updateWithInput()  // 与用户输入有关的更新
{MOUSEMSG m;		// 定义鼠标消息if (MouseHit())  //这个函数用于检测当前是否有鼠标消息{m = GetMouseMsg();  	// 获取一条鼠标消息if (m.uMsg == WM_MOUSEMOVE){// 鼠标移动时,挡板的位置等于鼠标所在的位置bar_x = m.x;bar_y = m.y;bar_left = bar_x - bar_width / 2;bar_right = bar_x + bar_width / 2;bar_top = bar_y - bar_high / 2;bar_bottom = bar_y + bar_high / 2;}else if (m.uMsg == WM_LBUTTONDOWN){// 按下鼠标左键,初始化小球的位置为挡板上面中心ball_x = bar_x;ball_y = bar_top - radius - 3;}}
}void gameover()
{EndBatchDraw();closegraph();
}int main()
{startup();  // 数据初始化	while (1)  //  游戏循环执行{clean();  // 把之前绘制的内容取消updateWithoutInput();  // 与用户输入无关的更新updateWithInput();     // 与用户输入有关的更新show();  // 显示新画面}gameover();     // 游戏结束、后续处理return 0;
}

 运行结果:

更多推荐

EasyX绘图实现反弹球消砖块

本文发布于:2024-03-10 19:33:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1728853.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:砖块   反弹球   EasyX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!