当前位置:首页 > 通信资讯 > 正文

c语言反弹球编程(反弹的球C语言)

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

1.环境准备和安装

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

2.Eaxy X功能的简单介绍

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

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

?
1 2 3 4 5 6 7 8 9 10 11 int main (void) { starup();//数据初始化 while(1) { show();//画面初始化 updateWithoutInput();//与用户输入无关的更新 updateWithInput();//与用户输入有关的更新 } }
  • 与上篇贪吃蛇的框架类似
  • starup();对全局变量初始化
  • show();画具体的图像(球 目标 木板)
  • updateWithoutInput(); 碰壁反弹 碰到木板反弹
  • updateWithInput();控制长方形的移动

4.头文件的加载和全局变量的设定

?
1 2 3 4 5 6 7 8 9 10 11 #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() 全局变量的初始化

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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() 打印画面

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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();与输入无关的更新

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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();与用户输入有关的更新

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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个单位

完整代码

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 #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(); } }

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

原文链接:https://blog.csdn.net/qq_38499859/article/details/71307286

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。