代码拉取完成,页面将自动刷新
#include <conio.h>
#include <graphics.h>
#define High 480 // 游戏画面尺寸
#define Width 640
// 全局变量
int ball1_x,ball1_y; // 小球的坐标
int ball2_x,ball2_y; // 小球的速度
int radius; // 小球的半径
void startup() // 数据初始化
{
ball1_x = Width/3;
ball1_y = High/3;
ball2_x = Width*2/3;
ball2_y = High*2/3;
radius = 20;
initgraph(Width,High);
BeginBatchDraw();
}
void clean() // 消除画面
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball1_x, ball1_y, radius);
fillcircle(ball2_x, ball2_y, radius);
}
void show() // 显示画面
{
setcolor(GREEN);
setfillcolor(GREEN);
fillcircle(ball1_x, ball1_y, radius); // 绘制绿圆
setcolor(RED);
setfillcolor(RED);
fillcircle(ball2_x, ball2_y, radius); // 绘制绿圆
FlushBatchDraw();
// 延时
Sleep(3);
}
void updateWithoutInput() // 与用户输入无关的更新
{
}
void updateWithInput() // 与用户输入有关的更新
{
char input;
if (kbhit())
{
input = getch();
int step = 10;
if (input == 'a')
ball1_x-=step;
if (input == 'd')
ball1_x+=step;
if (input == 'w')
ball1_y-=step;
if (input == 's')
ball1_y+=step;
if (input == '4')
ball2_x-=step;
if (input == '6')
ball2_x-=step;
if (input == '8')
ball2_y-=step;
if (input == '5')
ball2_y+=step;
}
}
void gameover()
{
EndBatchDraw();
closegraph();
}
int main()
{
startup(); // 数据初始化
while (1) // 游戏循环执行
{
clean(); // 把之前绘制的内容取消
updateWithoutInput(); // 与用户输入无关的更新
updateWithInput(); // 与用户输入有关的更新
show(); // 显示新画面
}
gameover(); // 游戏结束、后续处理
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。