C++(easyX)的游戏引擎制作(续)

        上次的代码有点问题,我现在说一下我的改良和各部分的介绍。

        下篇文章实践。

1、改良

        只改良了world_func.h(增加了碰撞检测模块),代码如下:

#pragma once
#include "font.h"
#include "sprite.h"


Rect& init(const int& weight, const int& height)
{
	initgraph(weight, height);
	Rect r(weight, height, weight/2, height/2);
	return r;
}//创建窗口并返回Rect对象

void quit()
{
	closegraph();
	exit(0);
}//销毁窗口

//碰撞检测模块
namespace Collide {
#include <algorithm>
	bool collide_rect(const Rect& r1, const Rect& r2)
	{
		float x_distance = r1.width / 2 + r2.width / 2, y_distance = r1.height / 2 + r2.height / 2;
		float real_x_d = abs(r1.x - r2.x), real_y_d = abs(r1.y - r2.y);
		if (real_x_d == x_distance && real_y_d == y_distance) return false;
		if (real_x_d <= x_distance || real_y_d <= y_distance)
		{
			return true;
		}
		return false;
	}

	
	bool collide_rect(const Sprite::Sprite& r1, const Sprite::Sprite& r2)
	{
		float x_distance = r1.width / 2 + r2.width / 2, y_distance = r1.height / 2 + r2.height / 2;
		float real_x_d = abs(r1.x - r2.x), real_y_d = abs(r1.y - r2.y);
		if (real_x_d == x_distance && real_y_d == y_distance) return false;
		if (real_x_d <= x_distance || real_y_d <= y_distance)
		{
			return true;
		}
		return false;
	}//普通碰撞检测

	bool collide_rect_circle(const Rect& r1, const Rect& r2)
	{
		float distance = r1.r + r2.r;
		if (static_cast<float>(sqrt(pow((r1.x - r2.x), 2) + pow((r1.y - r2.y), 2))) < distance)
			return true;
		return false;
	}
	bool collide_rect_circle(const Sprite::Sprite& r1, const Sprite::Sprite& r2)
	{
		float distance = r1.r + r2.r;
		if (static_cast<float>(sqrt(pow((r1.x - r2.x), 2) + pow((r1.y - r2.y), 2))) < distance)
			return true;
		return false;
	}//圆形碰撞检测
}

2、各部分介绍

        以下是各部分的介绍,可以结合源代码理解:

        1、Font.h:文字类(draw成员函数绘制)

        2、PNG.h:导入透明图片

        3、Speed.h: Speed: 速度类,Speed_X: X方向速度, Speed_Y:Y方向速度

        4、Rect.h:矩形(Rect)类,主要用于碰撞检测

        5.RSA.h: Rect类和Speed类的交互(不必理解)

        6、sprite.h:精灵(Sprite)类

        7、world_func.h:导入框架的头文件(接口)

3、最后

下篇文章开始用框架做一个简单的项目。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值