實現彈球小游戲
- 一.實現彈球小游戲:
- 1.初始化布:
- 2.初始化一個球的信息:
- 3.球的移動和碰撞反彈
- 4.底邊擋板的繪制和移動碰撞重置數據。
- 二.整體代碼:
一.實現彈球小游戲:
1.初始化布:
int main()
{initgraph(800, 600);setorigin(400, 300);setaspectratio(1, -1);setbkcolor(RGB(188, 227, 245));cleardevice();getchar();closegraph();
}
畫面效果:
2.初始化一個球的信息:
1.球的中心點坐標,球的半徑,
2.球的實際速度,水平豎直的分量速度。
3.球的顏色。
4.定義一個結構體去保存這些數值。
#define radius 30typedef struct ball {double x, y;double v, vx, vy;int radius;COLORREF colour;
}Ba;
//初始化球:
void InitBall(Ba* ball)
{//在一個范圍內隨機生成一個球,數值全部都是隨機的ball->x = ((rand() % 301) - 150);//[-150,150]ball->y = ((rand() % 201) - 100);//生成隨機速度ball->v = (rand() % 6)+3;//[3,8];//生成隨機的角度:int thead = rand() % 360;//定義水平豎直的速度:ball->vx = ball->v *cos((double)thead);ball->vy = ball->v *sin((double)thead);//初始化顏色;ball->colour = GREEN;
}
3.球的移動和碰撞反彈
//球的移動和碰撞反彈
void CrashBall(Ba* ball)
{while (1){cleardevice();//設置顏色繪制球;setfillcolor(ball->colour);fillcircle(ball->x, ball->y,radius);Sleep(40);//球的移動(ball->x) += (ball->vx);(ball->y) += (ball->vy);//判斷球是否到墻壁;//不考慮底邊是否存在擋板的情況;if ((ball->x >= 400 - radius) || (ball->x <= -400 + radius)){ball->vx = (-(ball->vx));}if ((ball->y >= 300 - radius) || (ball->y <= -300 + radius)){ball->vy = (-(ball->vy));}}
}
4.底邊擋板的繪制和移動碰撞重置數據。
void CrashBall(Ba* ball)
{int left, top, right, bottom;left = -100, top = -270;right = 100, bottom = -300;while (1){cleardevice();//設置顏色繪制球;setfillcolor(ball->colour);fillcircle(ball->x, ball->y,radius);//繪制擋板setfillcolor(RGB(113, 187, 234));//擋板不可以出界fillrectangle(left, top, right, bottom);Sleep(40);//球的移動(ball->x) += (ball->vx);(ball->y) += (ball->vy);//控制擋板移動if (_kbhit()){char ch = _getch();switch (ch){case 'a':case 'A':if (left < -400)break;left -= 5;right -= 5;break;case 'd':case 'D':if (right > 400)break;left += 5;right += 5;break;}}//判斷球是否到墻壁;//不考慮底邊是否存在擋板的情況;if ((ball->x >= 400 - radius) || (ball->x <= -400 + radius)){ball->vx = (-(ball->vx));}if ((ball->y >= 300 - radius)){ball->vy = (-(ball->vy));}//撞到擋板if ((ball->x >= left) && (ball->x <= right)){if (ball->y <= -240)ball->vy = (-(ball->vy));}//判斷出界if ((ball->x < left) || (ball->x > right)){if (ball->y < -300){InitBall(ball);left = -100, top = -270;right = 100, bottom = -300;}}}
}
二.整體代碼:
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<easyx.h>
#include<conio.h>
#include<time.h>
#include<math.h>
#include<stdbool.h>#define radius 30
#define move 10typedef struct ball {double x, y;double v, vx, vy;COLORREF colour;
}Ba;//初始化球:
void InitBall(Ba* ball)
{//在一個范圍內隨機生成一個球,數值全部都是隨機的ball->x = ((rand() % 301) - 150);//[-150,150]ball->y = ((rand() % 201) - 100);//生成隨機速度ball->v = (rand() % 6) + 5;//[5,11];//生成隨機的角度:int thead = rand() % 360;//定義水平豎直的速度:ball->vx = (ball->v) * cos((double)thead);ball->vy = (ball->v) * sin((double)thead);//初始化顏色;ball->colour = GREEN;
}//球的移動和碰撞反彈void CrashBall(Ba* ball)
{int left, top, right, bottom;left = -100, top = -270;right = 100, bottom = -300;while (1){cleardevice();//設置顏色繪制球;setfillcolor(ball->colour);fillcircle(ball->x, ball->y,radius);//繪制擋板setfillcolor(RGB(113, 187, 234));//擋板不可以出界fillrectangle(left, top, right, bottom);Sleep(40);//球的移動(ball->x) += (ball->vx);(ball->y) += (ball->vy);//控制擋板移動if (_kbhit()){char ch = _getch();switch (ch){case 'a':case 'A':if (left < -400)break;left -= 5;right -= 5;break;case 'd':case 'D':if (right > 400)break;left += 5;right += 5;break;}}//判斷球是否到墻壁;//不考慮底邊是否存在擋板的情況;if ((ball->x >= 400 - radius) || (ball->x <= -400 + radius)){ball->vx = (-(ball->vx));}if ((ball->y >= 300 - radius)){ball->vy = (-(ball->vy));}//撞到擋板if ((ball->x >= left) && (ball->x <= right)){if (ball->y <= -240)ball->vy = (-(ball->vy));}//判斷出界if ((ball->x < left) || (ball->x > right)){if (ball->y < -300){InitBall(ball);left = -100, top = -270;right = 100, bottom = -300;}}}
}int main()
{initgraph(800, 600);setorigin(400, 300);setaspectratio(1, -1);setbkcolor(RGB(188, 227, 245));cleardevice();//獲取當前時間作為隨機數種子;srand((unsigned int)time(NULL));//定義變量Ba ball;//初始化球:InitBall(&ball);//球的移動和碰撞反彈CrashBall(&ball);getchar();closegraph();
}