#include<graphics.h>
#include<conio.h>
#include<math.h>
#pragma warning(disable:4996)
//#undef UNICODE #undef _UNICODE
#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 / 5;
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);
}
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 = i * 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() //与用户输入无关的更新
{
//更新小球的坐标
ball_x += ball_vx;
ball_y += ball_vy;
//挡板和小球碰撞,小球反弹
if ((ball_x + radius >= bar_left) && (ball_x - radius <= bar_right)
&& (ball_y + radius >= bar_top))
ball_vy = -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_top = 0, 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() //与用户输入有关的更新
{
char input;
//用asdw来控制挡板的移动,小球碰到挡板后反弹
if (kbhit()) //判断是否有输入
{
input = getch(); //根据用户的输入来判断,不必输入回车
if (input == 'a' && bar_left > 0)
{
bar_x = bar_x - 15; //位置左移
bar_left = bar_x - bar_width / 2;
bar_right = bar_x + bar_width / 2;
}
if (input == 'd' && bar_right < Width) //位置右移
{
bar_x = bar_x + 15;
bar_left = bar_x - bar_width / 2;
bar_right = bar_x + bar_width / 2;
}
if (input == 'w' && bar_top > 0) //位置上移
{
bar_y = bar_y - 15;
bar_top = bar_y - bar_high / 2;
bar_bottom = bar_y + bar_high / 2;
}
if (input == 's' && bar_bottom < High) //位置下移
{
bar_y = bar_y + 15;
bar_top = bar_y - bar_high / 2;
bar_bottom = bar_y + bar_high / 2;
}
}
}
void gameover() //游戏结束
{
EndBatchDraw();
closegraph();
}
int main()
{
startup(); //数据初始化
while (1) //游戏循环执行
{
clean(); //清楚之前绘制的内容
updateWithoutInput(); //与用户输入有无关的更新
updateWithInput(); //与用户输入有关的更新
show(); //显示画面
}
gameover(); //游戏结束,进行后续的处理
return 0;
}
用EasyX实现反弹球消砖块
于 2024-02-20 20:34:17 首次发布