实现弹球小游戏
- 一.实现弹球小游戏:
- 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 30
typedef 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 10
typedef 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();
}