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();
}
相关推荐
软件资深者11 天前
植物大战僵尸1经典版(小游戏)+超强辅助工具 自动收取阳光
windows·游戏程序·windows11
孟无岐13 天前
【Laya】Socket 使用指南
websocket·typescript·游戏引擎·游戏程序·laya
Watermelo61713 天前
随机扣款实现赛博共产主义,《明日方舟:终末地》公测支付事故复盘
数据库·后端·游戏程序·技术美术·用户体验·游戏策划·游戏美术
晚霞的不甘13 天前
Flutter 方块迷阵游戏开发全解析:构建可扩展的关卡式益智游戏
前端·flutter·游戏·游戏引擎·游戏程序·harmonyos
孟无岐16 天前
【Laya】HttpRequest 网络请求
网络·typescript·游戏引擎·游戏程序·laya
孟无岐17 天前
【Laya】LocalStorage 本地存储
typescript·游戏引擎·游戏程序·laya
怣疯knight17 天前
外部类触发角色状态切换
游戏程序
孟无岐19 天前
【Laya】Byte 二进制数据处理
网络·typescript·游戏引擎·游戏程序·laya
孟无岐19 天前
【Laya】ClassUtils 类反射工具
typescript·游戏引擎·游戏程序·laya
孟无岐19 天前
【Laya】Ease 缓动函数
typescript·游戏引擎·游戏程序·laya