C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C语言反弹球

C语言实现反弹球游戏

作者:冰炭不投day

这篇文章主要为大家详细介绍了C语言实现反弹球游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

1.环境准备和安装

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);


}

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();
 } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文