您的位置:首页 > 编程语言 > C语言/C++

C语言实现反弹球游戏

2020-04-21 07:24 1586 查看

C语言小游戏――反弹球(简单的图形化界面),供大家参考,具体内容如下

1.环境准备和安装

  • 安装Visual C++ 6.0。
  • 去Easy X官网下载Easy X安装包。

2.Eaxy X功能的简单介绍

  • Easy X类似于一个库函数,其中带有许多很有用的函数。
  • Easy x首先创建一个新的窗口进行绘图。
  • 可以画常见点 线 多边形 可以调节颜色。
  • 可以插入图片,音乐。
  • 可以获取鼠标信息。
  • 其中函数的具体使用可以看安装包中带有的帮助文件

3.反弹球游戏主函数框架

int main (void)
{
starup();//数据初始化
while(1)
{
show();//画面初始化
updateWithoutInput();//与用户输入无关的更新
updateWithInput();//与用户输入有关的更新
}

}
  • 与上篇贪吃蛇的框架类似
  • starup();对全局变量初始化
  • 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);
}
  • 所有图像都是以像素为单位,所以设定的很大
  • 木板为长方形 目标为正方形
  • initgraph(width,high);建立一个宽为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语言 反弹球