# C语言小游戏——反弹球(简单的图形化界面)
int main (void)
{
starup();//数据初始化
while(1)
{
show();//画面初始化
updateWithoutInput();//与用户输入无关的更新
updateWithInput();//与用户输入有关的更新
}
}
#include
#include
#include
#include
//全局变量
int high,width;//游戏尺寸
int ball_x,ball_y,ball_r;//球
int board_x,board_y,board_long,board_bottom;//木板
int gaol_x,gaol_y,gaol_long;//目标
int velocity_x,velocity_y;//速度
void startup()
{
high=540;width=480;//游戏尺寸
ball_y=30;ball_x=width/3;ball_r=15;//球的坐标
board_y=high/2;board_x=width/2;//木板
board_long=150;board_bottom=10;
gaol_y=2;
gaol_x=width/2;
gaol_long=20; //目标第一个点的坐标
velocity_x=1,velocity_y=1;//速度
initgraph(width,high);
}
void show()
{
setbkcolor(RGB(255,255,255));
cleardevice();//清屏
setfillcolor(RGB(0,0,255));
fillcircle(ball_x,ball_y,ball_r);
setfillcolor(RGB(0,0,0));
fillrectangle(board_x,board_y,board_x+board_long,board_y+board_bottom);
setfillcolor(RGB(255,0,0));
fillrectangle(gaol_x,gaol_y,gaol_x+gaol_long,gaol_y+gaol_long);
}
void updateWithoutInpurt()
{
ball_x+=velocity_x;
ball_y+=velocity_y;
if(ball_x==1||ball_x==width-2)//碰壁反弹
velocity_x=-velocity_x;
if(ball_y==1||ball_y==high-2)
velocity_y=-velocity_y;
if(ball_y==board_y&&(ball_x>=board_x&&ball_x//碰木板反弹
velocity_y=-velocity_y;
if(ball_y==board_y+board_bottom&&(ball_x>=board_x&&ball_x//碰木板反弹
velocity_y=-velocity_y;
if((ball_x>gaol_x&&ball_xgaol_y&&ball_y//如果球击中目标
{
srand((unsigned)time(NULL));/*做随机数产生种子*/
gaol_y=rand()%(high/2-gaol_long)+1;
gaol_x=rand()%(width/2-gaol_long)+1;
}
}
功能:
* 碰壁反弹
* 碰木板反弹
* 如果球碰到目标,目标重新刷新
void updateWithInpurt()
{
char input;
if(kbhit())
{
input=getch();
if(input=='w'&&board_y>1)
board_y-=10;
if(input=='s'&&board_y+board_bottom2)
board_y+=10;
if(input=='a'&&board_x>1)
board_x-=10;
if(input=='d'&&board_x+board_long2)
board_x+=10;
}
}
#include
#include
#include
#include
//全局变量
int high,width;//游戏尺寸
int ball_x,ball_y,ball_r;//球
int board_x,board_y,board_long,board_bottom;//木板
int gaol_x,gaol_y,gaol_long;//目标
int velocity_x,velocity_y;//速度
void startup()
{
high=540;width=480;//游戏尺寸
ball_y=30;ball_x=width/3;ball_r=15;//球的坐标
board_y=high/2;board_x=width/2;//木板
board_long=150;board_bottom=10;
gaol_y=2;
gaol_x=width/2;
gaol_long=20; //目标第一个点的坐标
velocity_x=1,velocity_y=1;//速度
initgraph(width,high);
}
void show()
{
setbkcolor(RGB(255,255,255));
cleardevice();//清屏
setfillcolor(RGB(0,0,255));
fillcircle(ball_x,ball_y,ball_r);
setfillcolor(RGB(0,0,0));
fillrectangle(board_x,board_y,board_x+board_long,board_y+board_bottom);
setfillcolor(RGB(255,0,0));
fillrectangle(gaol_x,gaol_y,gaol_x+gaol_long,gaol_y+gaol_long);
}
void updateWithoutInpurt()
{
ball_x+=velocity_x;
ball_y+=velocity_y;
if(ball_x==1||ball_x==width-2)//碰壁反弹
velocity_x=-velocity_x;
if(ball_y==1||ball_y==high-2)
velocity_y=-velocity_y;
if(ball_y>board_y&&(ball_x>=board_x&&ball_x//碰木板反弹
velocity_y=-velocity_y;
if(ball_y==board_y+board_bottom&&(ball_x>=board_x&&ball_x//碰木板反弹
velocity_y=-velocity_y;
if((ball_x>gaol_x&&ball_xgaol_y&&ball_y//如果球击中目标
{
srand((unsigned)time(NULL));/*做随机数产生种子*/
gaol_y=rand()%(high/2-gaol_long)+1;
gaol_x=rand()%(width/2-gaol_long)+1;
}
}
void updateWithInpurt()
{
char input;
if(kbhit())
{
input=getch();
if(input=='w'&&board_y>1)
board_y-=10;
if(input=='s'&&board_y+board_bottom2)
board_y+=10;
if(input=='a'&&board_x>1)
board_x-=10;
if(input=='d'&&board_x+board_long2)
board_x+=10;
}
}
int main(void)
{
startup();
while(1)
{
show();
updateWithoutInpurt();
updateWithInpurt();
}
}