本示例展示了利用C语言编写双人反弹球游戏的详细源代码,供读者参考学习。
一、最终项目描述和效果
核心需求:构建一款支持两名玩家共同参与的弹跳球竞技游戏。
游戏运行后的视觉呈现如下所示:

二、基本框架实现
初始版本的代码结构如下:
#include
#include
#define High 480//游戏画面尺寸
#define Width 640
//全局变量
int ball1_x,ball1_y;//小球1的坐标
int ball2_x,ball2_y;//小球2的坐标
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(void)
{
startup();//数据的初始化
while(1)
{
clean();//把之前绘制的内容取消
updateWithoutInput();//与用户输入无关的更新
updateWithInput();//与用户输入有关的更新
show();//显示新画面
}
gameover();//游戏结束,进行后续处理
return 0;
}
该方案存在明显局限:在同一时间点,仅允许一方进行控制,双方无法并行操作。
三、异步操作的实现

优化后的代码实现如下:
#include
#include
#define High 480//游戏画面尺寸
#define Width 640
//全局变量
int ball1_x,ball1_y;//小球1的坐标
int ball2_x,ball2_y;//小球2的坐标
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()//与用户输入有关的更新
{
int step=1;
if((GetAsyncKeyState(0x41)&0x8000))//a
ball1_x-=step;
if((GetAsyncKeyState(0x44)&0x8000))//d
ball1_x+=step;
if((GetAsyncKeyState(0x57)&0x8000))//w
ball1_y-=step;
if((GetAsyncKeyState(0x53)&0x8000))//s
ball1_y+=step;
if((GetAsyncKeyState(VK_LEFT)&0x8000))//左方向键
ball2_x-=step;
if((GetAsyncKeyState(VK_RIGHT)&0x8000))//右方向键
ball2_x+=step;
if((GetAsyncKeyState(VK_UP)&0x8000))//上方向键
ball2_y-=step;
if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向键
ball2_y+=step;
}
void gameover()
{
EndBatchDraw();
closegraph();
}
int main(void)
{
startup();//数据的初始化
while(1)
{
clean();//把之前绘制的内容取消
updateWithoutInput();//与用户输入无关的更新
updateWithInput();//与用户输入有关的更新
show();//显示新画面
}
gameover();//游戏结束,进行后续处理
return 0;
}
改进后的运行效果如图所示:

四、双人反弹球
最终完整的双人游戏代码如下:
#include
#include
#include
#define High 480//游戏画面尺寸
#define Width 640
//全局变量
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球2的速度
int bar1_left,bar1_right,bar1_top,bar1_bottom;//挡板1的上下左右位置坐标
int bar2_left,bar2_right,bar2_top,bar2_bottom;//挡板2的上下左右位置坐标
int bar_height,bar_width;//挡板的高度和宽度
int radius;
void startup()//数据的初始化
{
ball_x=Width/2;
ball_y=High/2;
ball_vx=1;
ball_vy=1;
radius=20;
bar_width=Width/30;
bar_height=High/4;
bar1_left=Width*1/20;//挡板1的数据初始化
bar1_top=High/4;
bar1_right=bar1_left+bar_width;
bar1_bottom=bar1_top+bar_height;
bar2_left=Width*18.5/20;//挡板2的数据初始化
bar2_top=High/4;
bar2_right=bar2_left+bar_width;
bar2_bottom=bar2_top+bar_height;
initgraph(Width,High);
BeginBatchDraw();
}
void clean()//消除画面
{
setcolor(BLACK);
setfillcolor(BLACK);
fillcircle(ball_x,ball_y,radius);
fillcircle(ball_x,ball_y,radius);
bar(bar1_left,bar1_top,bar1_right,bar1_bottom);//绘制挡板
bar(bar2_left,bar2_top,bar2_right,bar2_bottom);
}
void show()//显示画面
{
setcolor(GREEN);
setfillcolor(GREEN);
fillcircle(ball_x,ball_y,radius);//绘制绿圆
setcolor(RED);
setfillcolor(RED);
bar(bar1_left,bar1_top,bar1_right,bar1_bottom);
bar(bar2_left,bar2_top,bar2_right,bar2_bottom);
FlushBatchDraw();
Sleep(3);
}
void updateWithoutInput()//与用户输入无关的更新
{
//挡板和小球碰撞,小球反弹
if(ball_x+radius>=bar2_left&&ball_y+radius>=bar2_top&&ball_y+radius=bar1_top&&ball_y+radius+Width-radius))
ball_vx=-ball_vx;
if((ball_y<=radius)||(ball_y>+High-radius))
ball_vy=-ball_vy;
}
void updateWithInput()//与用户输入有关的更新
{
int step=1;
if((GetAsyncKeyState(0x57)&0x8000))//w
bar1_top-=step;
if((GetAsyncKeyState(0x53)&0x8000))//s
bar1_top+=step;
if((GetAsyncKeyState(VK_UP)&0x8000))//上方向键
bar2_top-=step;
if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向键
bar2_top+=step;
if(bar1_top<0)//判断挡板是否超过屏幕
bar1_top+=step;
if(bar2_top<0)
bar2_top+=step;
if(bar1_top+bar_height>High)
bar1_top-=step;
if(bar2_top+bar_height>High)
bar2_top-=step;
bar1_bottom=bar1_top+bar_height;
bar2_bottom=bar2_top+bar_height;
}
void gameover()
{
EndBatchDraw();
closegraph();
}
int main(void)
{
startup();//数据的初始化
while(1)
{
clean();//把之前绘制的内容取消
updateWithoutInput();//与用户输入无关的更新
updateWithInput();//与用户输入有关的更新
show();//显示新画面
}
gameover();//游戏结束,进行后续处理
return 0;
}
最终效果图展示如下:

以上内容即为本次分享的全部资源,希望能为各位的学习带来便利,也恳请持续关注本站更新。