STM32学习笔记十三:WS2812制作像素游戏屏-飞行射击游戏(3)探索数据管理,比例分布随机数算法

本文介绍了如何在C++中设计和实现一个飞行射击游戏的敌机系统,包括敌机基类(EnemyBase)、多种敌机子类(EnemyT1,EnemyT2,EnemyT3)以及敌机管理器(EnemyManager),同时探讨了数据结构的设计和内存管理策略,如单例模式用于跨manager的数据访问。

这回,开始做敌机。

我们设定敌机有3中,小型,大型和BOSS,分别叫 EnemyT1 / EnemyT2 / EnemyT3。

先定义一个敌机基类:

EnemyBase.h

/*
 * EnemyBase.h
 *
 *  Created on: Dec 24, 2023
 *      Author: YoungMay
 */

#ifndef SRC_PLANE_ENEMYBASE_H_
#define SRC_PLANE_ENEMYBASE_H_
#include "../drivers/DList.h"
#include "PlaneDef.h"

class EnemyBase {
public:
	EnemyBase();
	virtual ~EnemyBase() {
	}
	virtual uint8_t tick(uint32_t t)=0;
	virtual void init()=0;
	virtual uint8_t show(void)=0;
	ListNode *enemyBulletList;
	PlaneObject_t baseInfo;
	uint16_t HP;
};

#endif /* SRC_PLANE_ENEMYBASE_H_ */

然后是三种敌机:

EnemyT1.h

/*
 * EnemyT1.h
 *
 *  Created on: Dec 24, 2023
 *      Author: YoungMay
 */

#ifndef SRC_PLANE_EnemyT1_H_
#define SRC_PLANE_EnemyT1_H_
#include "EnemyBase.h"

class EnemyT1: public EnemyBase {
public:
	EnemyT1();
	~EnemyT1();
	uint8_t tick(uint32_t t);
	void init();
	uint8_t show(void);
	bool sharp[3][3] = {
 { 1, 0, 1 },
 { 1, 1, 1 },
 { 0, 1, 0 } };

};

#endif /* SRC_PLANE_EnemyT1_H_ */

 EnemyT1.cpp

/*
 * EnemyT1.cpp
 *
 *  Created on: Dec 24, 2023
 *      Author: YoungMay
 */

#include "EnemyT1.h"

EnemyT1::EnemyT1() {
	HP = 100;
	baseInfo.speed = 15;
	baseInfo.width = 3;
	baseInfo.height = 3;
	getRainbowColor(&baseInfo.color, 400);
}

EnemyT1::~EnemyT1() {
	// TODO Auto-generated destructor stub
}

void EnemyT1::init() {

}

uint8_t EnemyT1::tick(uint32_t t) {
	baseInfo.y += t * baseInfo.speed;
	return 0;
}

uint8_t EnemyT1::show(void) {
	for (uint8_t y = 0; y < baseInfo.height; y++) {
		for (uint8_t x = 0; x < baseInfo.width; x++) {
			if (sharp[y][x])
				ws2812_pixel(
						x + baseInfo.x / PlaneXYScale - baseInfo.width / 2,
						y + baseInfo.y / PlaneXYScale - baseInfo.height / 2,
						baseInfo.color.R, baseInfo.color.G, baseInfo.color.B);
		}
	}
	return 0;
}

 EnemyT2.h

/*
 * EnemyT2.h
 *
 *  Created on: Dec 24, 2023
 *      Author: YoungMay
 */

#ifndef SRC_PLANE_EnemyT2_H_
#define SRC_PLANE_EnemyT2_H_
#include "EnemyBase.h"

class EnemyT2: public EnemyBase {
public:
	EnemyT2();
	~EnemyT2();
	uint8_t tick(uint32_t t);
	void init();
	uint8_t show(void);

	bool sharp[5][5] = {
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vvind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值