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();
}
相关推荐
HahaGiver66617 小时前
Unity Shader Graph 3D 实例 - 基础的模型贴图渲染
3d·unity·游戏程序·贴图·游戏美术
@LYZY6 天前
Unity 中隐藏文件规则
unity·游戏引擎·游戏程序·vr
棉猴6 天前
《pygame中Sprite类实现多帧动画》注-通过多张序列帧显示动画2-2
开发语言·python·游戏·游戏程序·pygame
HahaGiver6668 天前
从0到1做一个“字母拼词”Unity小游戏(含源码/GIF)- 字母拼词正确错误判断
unity·游戏引擎·游戏程序
程序编程- Java9 天前
三角洲行动-java游戏程序
java·游戏程序·安全架构·玩游戏
程序编程- Java9 天前
和平精英java 游戏程序
java·游戏程序·安全架构·玩游戏
reddingtons15 天前
体验设计总监的“第五维度”:用Adobe Aero,无代码构建AR沉浸式体验
人工智能·adobe·ar·游戏程序·设计师·增强现实·adobe aero
WaWaJie_Ngen24 天前
【OpenGL】模板测试(StencilTest)
c++·算法·游戏·游戏引擎·游戏程序·图形渲染
takashi_void1 个月前
本地实现斯坦福小镇(利用大语言模型使虚拟角色自主发展剧情)类似项目“Microverse”
人工智能·语言模型·自然语言处理·godot·游戏程序·斯坦福小镇
饮浊酒1 个月前
Python学习-----小游戏之人生重开模拟器(普通版)
python·学习·游戏程序