C语言实现反弹球游戏
C语言小游戏——反弹球(简单的图形化界面),供大家参考,具体内容如下
1.环境准备和安装
- 安装VisualC++6.0。
- 去EasyX官网下载EasyX安装包。
2.EaxyX功能的简单介绍
- EasyX类似于一个库函数,其中带有许多很有用的函数。
- Easyx首先创建一个新的窗口进行绘图。
- 可以画常见点线多边形可以调节颜色。
- 可以插入图片,音乐。
- 可以获取鼠标信息。
- 其中函数的具体使用可以看安装包中带有的帮助文件
3.反弹球游戏主函数框架
intmain(void)
{
starup();//数据初始化
while(1)
{
show();//画面初始化
updateWithoutInput();//与用户输入无关的更新
updateWithInput();//与用户输入有关的更新
}
}
- 与上篇贪吃蛇的框架类似
- starup();对全局变量初始化
- show();画具体的图像(球目标木板)
- updateWithoutInput();碰壁反弹碰到木板反弹
- updateWithInput();控制长方形的移动
4.头文件的加载和全局变量的设定
#include#include #include #include //全局变量 inthigh,width;//游戏尺寸 intball_x,ball_y,ball_r;//球 intboard_x,board_y,board_long,board_bottom;//木板 intgaol_x,gaol_y,gaol_long;//目标 intvelocity_x,velocity_y;//速度
5.第一个函数starup()全局变量的初始化
voidstartup()
{
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);
}
- 所有图像都是以像素为单位,所以设定的很大
- 木板为长方形目标为正方形
- initgraph(width,high);建立一个宽为width,高为high的窗口
6.第二个函数show()打印画面
voidshow()
{
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();与输入无关的更新
voidupdateWithoutInpurt()
{
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&&ball_xgaol_x&&ball_xgaol_y&&ball_y
功能:
*碰壁反弹
*碰木板反弹
*如果球碰到目标,目标重新刷新
8.第四个函数updateWithInput();与用户输入有关的更新
voidupdateWithInpurt()
{
charinput;
if(kbhit())
{
input=getch();
if(input=='w'&&board_y>1)
board_y-=10;
if(input=='s'&&board_y+board_bottom1)
board_x-=10;
if(input=='d'&&board_x+board_long
因为是以像素为单位绘画,所以每次移动10个单位
完整代码
#include
#include
#include
#include
//全局变量
inthigh,width;//游戏尺寸
intball_x,ball_y,ball_r;//球
intboard_x,board_y,board_long,board_bottom;//木板
intgaol_x,gaol_y,gaol_long;//目标
intvelocity_x,velocity_y;//速度
voidstartup()
{
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);
}
voidshow()
{
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);
}
voidupdateWithoutInpurt()
{
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&&ball_xgaol_x&&ball_xgaol_y&&ball_y1)
board_y-=10;
if(input=='s'&&board_y+board_bottom1)
board_x-=10;
if(input=='d'&&board_x+board_long
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。