#include <conio.h> #include <graphics.h>
#define High 480 // 游戏画面尺寸 #define Width 640
// 全局变量 int ball_x,ball_y; // 小球的坐标 int ball_vx,ball_vy; // 小球的速度 int radius; // 小球的半径 int brick_x,brick_y;//砖块坐标 int bar_x,bar_y;//挡板中心坐标 int bar_left,bar_right,bar_top,bar_bottom;//挡板上下左右边界坐标 int bar_width,bar_high;//挡板宽度 高度 int a[8]={0,0,0,0,0,0,0,0};
void startup() // 数据初始化 { ball_x = Width/2; ball_y = High/2; ball_vx = 1; ball_vy = 1; radius = 20; bar_x=Width/2; bar_y=465; bar_width=80; bar_high=15;
initgraph(Width, High);
BeginBatchDraw();
}
void clean() // 显示画面 { // 绘制黑线、黑色填充的圆 setcolor(BLACK); setfillcolor(BLACK); fillcircle(ball_x, ball_y, radius); setcolor(BLACK); setfillcolor(BLACK); fillrectangle(bar_left,bar_high,bar_right,bar_bottom);
}
void show() // 显示画面
{
// 绘制黄线、绿色填充的圆
setcolor(YELLOW);
setfillcolor(GREEN);
fillcircle(ball_x, ball_y, radius);
//绘制挡板
setcolor(YELLOW);
setfillcolor(GREEN);
fillrectangle(bar_left,bar_high,bar_right,bar_bottom);
setcolor(YELLOW);
setfillcolor(RED);
fillrectangle(0,0,80,40);
fillrectangle(80,0,160,40);
fillrectangle(160,0,240,40);
fillrectangle(240,0,320,40);
fillrectangle(320,0,400,40);
fillrectangle(400,0,480,40);
fillrectangle(480,0,560,40);
fillrectangle(560,0,640,40);
if(a[0])
{
setcolor(BLACK);
setfillcolor(BLACK);
fillrectangle(0,0,80,40);
}
if(a[1])
{
setcolor(BLACK);
setfillcolor(BLACK);
fillrectangle(80,0,160,40);
}
if(a[2])
{
setcolor(BLACK);
setfillcolor(BLACK);
fillrectangle(160,0,240,40);
}
if(a[3])
{
setcolor(BLACK);
setfillcolor(BLACK);
fillrectangle(240,0,320,40);
}
if(a[4])
{
setcolor(BLACK);
setfillcolor(BLACK);
fillrectangle(320,0,400,40);
}
if(a[5])
{
setcolor(BLACK);
setfillcolor(BLACK);
fillrectangle(400,0,480,40);
}
if(a[6])
{
setcolor(BLACK);
setfillcolor(BLACK);
fillrectangle(480,0,560,40);
}
if(a[7])
{
setcolor(BLACK);
setfillcolor(BLACK);
fillrectangle(560,0,640,40);
}
FlushBatchDraw();
// 延时
Sleep(3);
}
void updateWithoutInput() // 与用户输入无关的更新 { // 更新小圆坐标 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;
if(bar_y-ball_y<=35&&bar_y-ball_y>=0&&(ball_x-bar_x<=80||ball_x-bar_x>=80))
ball_vy = -ball_vy;
if(ball_y<=60)
ball_vy = -ball_vy;
if(ball_x>=0&&ball_x<=80 && ball_y==62)
a[0]=1;
if(ball_x>80&&ball_x<=160 && ball_y==62)
a[1]=1;
if(ball_x>160&&ball_x<=240 && ball_y==62)
a[2]=1;
if(ball_x>240&&ball_x<=320 &&ball_y==62)
a[3]=1;
if(ball_x>320&&ball_x<=400&&ball_y==62)
a[4]=1;
if(ball_x>400&&ball_x<=480&&ball_y==62)
a[5]=1;
if(ball_x>480&&ball_x<=560&&ball_y==62)
a[6]=1;
if(ball_x>560&&ball_x<=640&&ball_y==62)
a[7]=1;
bar_left=bar_x-80;
bar_right=bar_x+80;
bar_high=bar_y-15;
bar_bottom=bar_y+15;
}
void updateWithInput() // 与用户输入有关的更新
{
MOUSEMSG m; // 定义鼠标消息
// 获取一条鼠标消息
m = GetMouseMsg();
if(m.uMsg == WM_MOUSEMOVE)
{
bar_x=m.x;
bar_y=m.y;
}
}
void gameover() { EndBatchDraw(); closegraph(); }
int main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
clean(); // 把之前绘制的内容清除
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
show(); // 显示新画面
}
gameover(); // 游戏结束、后续处理
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。