这回,开始做敌机。
我们设定敌机有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] = {

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

被折叠的 条评论
为什么被折叠?



