这篇文章将为大家详细讲解有关使用C语言怎么实现一个反弹球游戏,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1.环境准备和安装
安装Visual C++ 6.0。
去Easy X官网下载Easy X安装包。
2.Eaxy X功能的简单介绍
3.反弹球游戏主函数框架
int main (void)
{
starup();//数据初始化
while(1)
{
show();//画面初始化
updateWithoutInput();//与用户输入无关的更新
updateWithInput();//与用户输入有关的更新
}
}
4.头文件的加载和全局变量的设定
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
//全局变量
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;//速度
5.第一个函数starup() 全局变量的初始化
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);
}
6.第二个函数show() 打印画面
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);
}
setbkcolor(RGB(255,255,255));设置当前绘图背景色(白)
cleardevice();清屏(使用当前背景色覆盖)
setfillcolor(RGB(0,0,255));设置当前的填充颜色(蓝)
fillcircle(x,y,r);画一个圆心为(x,y)半径为r有颜色填充的圆
fillrectangle(x1,y1,x2,y2);画一个左上座标为(x1,y1)右下为(x2,y2)有颜色填充的矩形
7.第三个函数updateWithoutInput();与输入无关的更新
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<board_x+board_long))//碰木板反弹
velocity_y=-velocity_y;
if(ball_y==board_y+board_bottom&&(ball_x>=board_x&&ball_x<board_x+board_long))//碰木板反弹
velocity_y=-velocity_y;
if((ball_x>gaol_x&&ball_x<gaol_x+gaol_long)&&(ball_y>gaol_y&&ball_y<gaol_y+gaol_long))//如果球击中目标
{
srand((unsigned)time(NULL));/*做随机数产生种子*/
gaol_y=rand()%(high/2-gaol_long)+1;
gaol_x=rand()%(width/2-gaol_long)+1;
}
}
功能:
* 碰壁反弹
* 碰木板反弹
* 如果球碰到目标,目标重新刷新
8.第四个函数 updateWithInput();与用户输入有关的更新
void updateWithInpurt()
{
char input;
if(kbhit())
{
input=getch();
if(input=='w'&&board_y>1)
board_y-=10;
if(input=='s'&&board_y+board_bottom<high-2)
board_y+=10;
if(input=='a'&&board_x>1)
board_x-=10;
if(input=='d'&&board_x+board_long<width-2)
board_x+=10;
}
}
因为是以像素为单位绘画,所以每次移动10个单位
完整代码
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
//全局变量
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<board_x+board_long))//碰木板反弹
velocity_y=-velocity_y;
if(ball_y==board_y+board_bottom&&(ball_x>=board_x&&ball_x<board_x+board_long))//碰木板反弹
velocity_y=-velocity_y;
if((ball_x>gaol_x&&ball_x<gaol_x+gaol_long)&&(ball_y>gaol_y&&ball_y<gaol_y+gaol_long))//如果球击中目标
{
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_bottom<high-2)
board_y+=10;
if(input=='a'&&board_x>1)
board_x-=10;
if(input=='d'&&board_x+board_long<width-2)
board_x+=10;
}
}
int main(void)
{
startup();
while(1)
{
show();
updateWithoutInpurt();
updateWithInpurt();
}
}
关于使用C语言怎么实现一个反弹球游戏就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。