easyx图形库基础:3实现弹球小游戏

实现弹球小游戏

一.实现弹球小游戏:

1.初始化布:

c 复制代码
int main()
{
	initgraph(800, 600);
	setorigin(400, 300);
	setaspectratio(1, -1);

	setbkcolor(RGB(188, 227, 245));
	cleardevice();

	getchar();
	closegraph();
}

画面效果:

2.初始化一个球的信息:

1.球的中心点坐标,球的半径,

2.球的实际速度,水平竖直的分量速度。

3.球的颜色。

4.定义一个结构体去保存这些数值。

c 复制代码
#define radius 30

typedef struct ball {
	double x, y;
	double v, vx, vy;
	int radius;
	COLORREF colour;
}Ba;
c 复制代码
//初始化球:
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.球的移动和碰撞反弹

c 复制代码
//球的移动和碰撞反弹
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.底边挡板的绘制和移动碰撞重置数据。

c 复制代码
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;
			}

		}

	}
}

二.整体代码:

c 复制代码
#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();
}
相关推荐
BuHuaX4 天前
Unity-DOTween插件
unity·c#·游戏引擎·游戏程序·游戏策划
BuHuaX5 天前
UGUI优化
unity·c#·游戏引擎·游戏程序·游戏策划
Igallta_81362212 天前
【小游戏】C++控制台版本俄罗斯轮盘赌
c语言·开发语言·c++·windows·游戏·游戏程序
github_czy14 天前
(9/100)每日小游戏平台系列
javascript·python·游戏引擎·游戏程序
木兰不吃草15 天前
如何在 Mac 上下载安装仙剑游戏仙剑世界?可以通过IPA砸壳包安装非常简单
游戏·macos·ios·游戏程序·mac
局外人_Jia15 天前
【C++游戏开发-五子棋】
开发语言·c++·人工智能·算法·游戏程序
BuHuaX17 天前
Unity-New Input System
unity·c#·游戏引擎·游戏程序·lucene
在学0218 天前
pytorch训练五子棋ai
人工智能·pytorch·python·神经网络·机器学习·游戏程序
science怪兽23 天前
日志2025.2.9
算法·unity·游戏程序
Damon小智1 个月前
使用Pygame制作“Flappy Bird”游戏
python·游戏·游戏程序·pygame