STM32学习笔记十九:WS2812制作像素游戏屏-飞行射击游戏(9)探索道具系统

本文主要是介绍STM32学习笔记十九:WS2812制作像素游戏屏-飞行射击游戏(9)探索道具系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 增加道具的初衷,是为了增加游戏的趣味性。但是现在有些游戏吧,啧啧啧。

考虑道具,我们要考虑几方面的事情:

1、道具产生,可以随机产生,指定位置或时间自动产生,击杀地方产生。

2、未捡拾的道具管理。可以自动获取,主动捡拾,如何移动,如何呈现,如何销毁等等

3、已捡拾道具管理。这和上面2是两个不同链表,特别是双人游戏,显然不能用上面那个。

4、道具使用。包括特效呈现,后端数据处理。这很重要!因为你做的时候才会发现,道具可能会影响各种各样的数据,全世界的任何数据都可能需要访问到,就看你希望道具是什么效果了。这对前面章节的数据管理方式影响很大。

不发散了,回归我们的游戏。

我们可以通过前面 IntervalAniTimer_t  来控制 玩家使用道具的间隔,道具的数量是通过玩家自己捡拾的方式来控制。

另外,我们加一点小小的游戏设定——捡到的道具先进入出放在链表里面,只能按顺序使用——这样,可以增加一点点小小的策略性。

不同道具的差异是很大的,为此,我们要为每种道具定义一个新的类。前面我们把道具用结构来做,这回我们把他们拆出来,统一当成道具效果来使用。暂定我们有四种道具:

炸弹、激光、护盾、恢复。

每个道具有两种显示形态:

-》下落过程中的包裹形态,是一条结构数据,用管理类PropManager进行管理。像子弹数据一样,保留基础信息即可,用tag保存类型。

-》显示特效形态。此时应处于玩家的道具列表进行管理。每种道具对应一个实体类。

开始吧。

1、先定义PropManager。

PropManager.h

/** PropManager.h**  Created on: Dec 27, 2023*      Author: YoungMay*/#ifndef SRC_PLANE_PROPMANAGER_H_
#define SRC_PLANE_PROPMANAGER_H_
#include "PlaneDef.h"
#include "../drivers/DList.h"
#include "PropBase.h"
#include "PropBomb.h"
#include "PropHealth.h"
#include "PropSheel.h"
#include "PropLaser.h"class PropManager {
public:PropManager();virtual ~PropManager();uint8_t tick(uint32_t t);void init();uint8_t show(void);ListNode *propList;PlaneObject_t* createPropObject(int type);PropBase* createPropEffect(PlaneObject_t *prop) {PropBase *pb = NULL;switch (prop->tag) {case 0:pb = new PropBomb;break;case 1:pb = new PropLaser;break;case 2:pb = new PropHealth;break;case 3:pb = new PropSheel;break;}pb->baseInfo.tag = prop->tag;return pb;}private:IntervalAniTimer_t createTimer = { 1000, 10000 };uint16_t propTypeProportion[4] = { 100, 100, 100, 100 };
};#endif /* SRC_PLANE_PROPMANAGER_H_ */

PropManager.cpp

/** PropManager.cpp**  Created on: Dec 27, 2023*      Author: YoungMay*/#include "PropManager.h"PropManager::PropManager() {propList = ListCreate();}PropManager::~PropManager() {Serial_print("Destroy PropManager start");for (ListNode *cur = propList->next; cur != propList; cur = cur->next) {delete ((PlaneObject_t*) (cur->data));}ListDestroy(propList);Serial_print("Destroy PropManager OK");
}void PropManager::init() {}PlaneObject_t* PropManager::createPropObject(int type) {PlaneObject_t *prop = new PlaneObject_t;prop->tag = type;prop->x = ran_range(5, 27) * PlaneXYScale;prop->y = 0;prop->speedX = 0;prop->speedY = 10;prop->width = 3;prop->height = 3;prop->visiable = 1;prop->color = 0x900090;prop->life = 60000;return prop;
}
uint8_t PropManager::tick(uint32_t t) {if (createTimer.tick(t)) {int type = ran_seq(4, propTypeProportion);PlaneObject_t *prop = createPropObject(type);ListPushBack(propList, (LTDataType) prop);}for (ListNode *cur = propList->next; cur != propList; cur = cur->next) {PlaneObject_t *prop = ((PlaneObject_t*) (cur->data));prop->y += prop->speedY * t;prop->life -= t;if (prop->y > 64 * PlaneXYScale || prop->life < 0)prop->visiable = 0;}return 0;
}uint8_t PropManager::show(void) {ListNode *cur = propList->next;while (cur != propList) {PlaneObject_t *prop = ((PlaneObject_t*) (cur->data));if (prop->visiable) {ws2812_fill(prop->x / PlaneXYScale - 1, prop->y / PlaneXYScale - 1,3, 3, 128, 0, 128);uint8_t idx = (prop->life >> 7) & 0x7;ws2812_pixel(prop->x / PlaneXYScale + Explode_X[idx],prop->y / PlaneXYScale + Explode_Y[idx],(PropColor[prop->tag] & 0xff0000) >> 16,(PropColor[prop->tag] & 0xff00) >> 8,PropColor[prop->tag] & 0xff);ws2812_pixel(prop->x / PlaneXYScale + Explode_X[(idx + 4) & 7],prop->y / PlaneXYScale + Explode_Y[(idx + 4) & 7],(PropColor[prop->tag] & 0xff0000) >> 16,(PropColor[prop->tag] & 0xff00) >> 8,PropColor[prop->tag] & 0xff);} else {delete prop;ListErase(cur);}cur = cur->next;}return 0;
}

四种掉落包的形状是一样的,做了一个转圈的颜色特效。

2、在plane.cpp的tick中,加入掉落物的处理

uint8_t Plane::tick(uint32_t t, uint8_t b1, uint8_t b2) {
。。enemyManager.tick(t);propManager.tick(t);checkEffectCollision(t, player1);checkEffectCollision(t, player2);checkEnemyCollision();checkPlayerCollision();checkPropCollision();backGroundStar.show();enemyManager.show();player1->show();player2->show();propManager.show();
。。。
}

3、添加掉落物的碰撞检测

void Plane::checkPropCollision() {ListNode *cur = propManager.propList->next;while (cur != propManager.propList) {PlaneObject_t *prop = (PlaneObject_t*) (cur->data);if (player1->baseInfo.visiable&& checkAABBCollision(&player1->baseInfo, prop)) {prop->visiable = 0;player1->pickProp(propManager.createPropEffect(prop));}if (player2->baseInfo.visiable&& checkAABBCollision(&player2->baseInfo, prop)) {prop->visiable = 0;player2->pickProp(propManager.createPropEffect(prop));}cur = cur->next;}
}

这里用了AABB碰撞:

	uint8_t checkAABBCollision(PlaneObject_t *a, PlaneObject_t *b) {return abs(a->x - b->x) / PlaneXYScale < (a->width + b->width) / 2&& abs(a->y - b->y) / PlaneXYScale < (a->height + b->height) / 2;}

 4、为玩家添加拾取操作pickProp

class PlanePlayer {
public:PlanePlayer();~PlanePlayer();void init(uint8_t id);void start(uint8_t id);uint8_t tick(uint32_t t, uint8_t b1);uint8_t show(void);uint8_t hitDetect(int x, int y, int damage);uint8_t hitEffectDetect(int x, int y, int r);void pickProp(PropBase *prop) {if (propCount < 30) {ListPushBack(propList, (LTDataType) prop);propCount++;}}PlaneObject_t baseInfo;ListNode *bulletList;PropBase *currentProp = NULL;
private:DispersedAnimation *damageAnimation;ListNode *animationList;ListNode *propList;uint8_t propCount = 0;IntervalAniTimer_t fireTimer = { 0, 200 };PlaneObject_t* createBulletObject();void showProp(int locX);
};

5、然后是实现玩家甩道具。

uint8_t PlanePlayer::tick(uint32_t t, uint8_t b1) {
。。。if (currentProp == NULL) {if (b1 & KEY_BUTTON_D && propList->next != propList) {currentProp = (PropBase*) (propList->next->data);currentProp->init(&baseInfo);propCount--;ListErase(propList->next);}} else {currentProp->tick(t);if (currentProp->baseInfo.life < 0) {delete currentProp;currentProp = NULL;}}。。。return 0;
}

如果道具已经甩出去了,则执行道具的TICK方法。

6、在玩家的显示操作中添加显示道具特效

uint8_t PlanePlayer::show(void) {
。。。if (currentProp != NULL) {currentProp->show();}if (baseInfo.tag == 1)showProp(0);elseshowProp(31);return 0;
}void PlanePlayer::showProp(int locX) {uint8_t h1 = baseInfo.life / (PlayerMaxLife / 30);uint8_t h2 = baseInfo.life - h1 * (PlayerMaxLife / 30);ws2812_fill(locX, 31 - h1, 1, h1, 240, 0, 0);ws2812_pixel(locX, 31 - h1 - 1, h2 * 5, h2 * 3, 0);int locY = 32;for (ListNode *cur = propList->next; cur != propList; cur = cur->next) {uint8_t tag = ((PropBase*) (cur->data))->baseInfo.tag;ws2812_pixel(locX, locY, (PropColor[tag] >> 16) & 0xff,(PropColor[tag] >> 8) & 0xff, PropColor[tag] & 0xff);locY++;}
}

showProp用于在屏幕两侧分别显示两个玩家的未使用的道具和当前血量。

现在实现几种道具:

1、先定义好基类:

/** PropBase.h**  Created on: Dec 27, 2023*      Author: YoungMay*/#ifndef SRC_PLANE_PLANEPROP_H_
#define SRC_PLANE_PLANEPROP_H_
#include "PlaneDef.h"
#include "../drivers/DList.h"
#include "../drivers/DataBulk.h"
#include "../drivers/ws2812Frame.h"const uint32_t PropColor[4] = { 0xf02000, 0x0020f0, 0x20f000, 0x008080 };class PropBase {
public:PropBase();virtual ~PropBase();virtual uint8_t tick(uint32_t t)=0;virtual void init(PlaneObject_t *player)=0;virtual void hitEffectDetect(uint32_t t)=0;virtual uint8_t show()=0;PlaneObject_t baseInfo;PlaneObject_t *player;
protected:uint32_t totalTick = 0;};#endif /* SRC_PLANE_PLANEPROP_H_ */

2、然后是几种道具。每种道具有不同的显示特效和碰撞检测

炸弹道具:PropBomb.cpp 

/** PropBomb.cpp**  Created on: Dec 27, 2023*      Author: YoungMay*/#include "PropBomb.h"
#include "EnemyBase.h"PropBomb::PropBomb() {// TODO Auto-generated constructor stub}PropBomb::~PropBomb() {// TODO Auto-generated destructor stub
}void PropBomb::init(PlaneObject_t *_player) {player = _player;baseInfo.x = player->x;baseInfo.y =player->y > 25 * PlaneXYScale ? player->y - 25 * PlaneXYScale : 0;baseInfo.life = 4000;baseInfo.visiable = 1;
}uint8_t PropBomb::tick(uint32_t t) {baseInfo.life -= t;return 0;
}uint8_t PropBomb::show() {ws2812_Fill_Circle(baseInfo.x / PlaneXYScale, baseInfo.y / PlaneXYScale, 10,PropColor[0]);return 0;
}uint8_t PropBomb::hitEffectDetectOnce(int x, int y, int r) {int a = (x - baseInfo.x) / 100;int b = (y - baseInfo.y) / 100;int c = (r + 10) * 100;return (a * a + b * b < c * c) ? 20 : 0;
}void PropBomb::hitEffectDetect(uint32_t t) {for (ListNode *enemy = EnemyList->next; enemy != EnemyList;enemy = enemy->next) {EnemyBase *ene = (EnemyBase*) enemy->data;if (ene->explodeState)continue;uint8_t res = hitEffectDetectOnce(ene->baseInfo.x, ene->baseInfo.y,(ene->baseInfo.width + ene->baseInfo.height) / 3);if (res) {ene->baseInfo.life -= res * t;ene->hurt();}}for (ListNode *enemyBul = EnemyBulletList->next;enemyBul != EnemyBulletList; enemyBul = enemyBul->next) {PlaneObject_t *bul = (PlaneObject_t*) enemyBul->data;uint8_t res = hitEffectDetectOnce(bul->x, bul->y, 1);if (res) {bul->visiable = 0;}}for (ListNode *enemyBul = EnemyRocketList->next;enemyBul != EnemyRocketList; enemyBul = enemyBul->next) {PlaneObject_t *bul = (PlaneObject_t*) enemyBul->data;uint8_t res = hitEffectDetectOnce(bul->x, bul->y, 1);if (res) {bul->visiable = 0;}}
}

激光道具:PropLaser.cpp

/** PropLaser.cpp**  Created on: Dec 27, 2023*      Author: YoungMay*/#include "PropLaser.h"
#include "EnemyBase.h"PropLaser::PropLaser() {// TODO Auto-generated constructor stub}PropLaser::~PropLaser() {// TODO Auto-generated destructor stub
}
void PropLaser::init(PlaneObject_t *_player) {player = _player;baseInfo.x = player->x;baseInfo.y = player->y;baseInfo.life = 6000;
}uint8_t PropLaser::tick(uint32_t t) {baseInfo.life -= t;return 0;
}uint8_t PropLaser::show() {ws2812_fill(player->x / PlaneXYScale, 0, 1, player->y / PlaneXYScale,(PropColor[1] >> 16) & 0xff, (PropColor[1] >> 8) & 0xff,PropColor[1] & 0xff);return 0;
}uint8_t PropLaser::hitEffectDetectOnce(int x, int y, int r) {return abs(x - player->x) < r * PlaneXYScale && y < player->y ? 20 : 0;
}void PropLaser::hitEffectDetect(uint32_t t) {for (ListNode *enemy = EnemyList->next; enemy != EnemyList;enemy = enemy->next) {EnemyBase *ene = (EnemyBase*) enemy->data;if (ene->explodeState)continue;uint8_t res = hitEffectDetectOnce(ene->baseInfo.x, ene->baseInfo.y,ene->baseInfo.width / 2);if (res) {ene->baseInfo.life -= res * t;ene->hurt();}}for (ListNode *enemyBul = EnemyBulletList->next;enemyBul != EnemyBulletList; enemyBul = enemyBul->next) {PlaneObject_t *bul = (PlaneObject_t*) enemyBul->data;uint8_t res = hitEffectDetectOnce(bul->x, bul->y, 1);if (res) {bul->visiable = 0;}}for (ListNode *enemyBul = EnemyRocketList->next;enemyBul != EnemyRocketList; enemyBul = enemyBul->next) {PlaneObject_t *bul = (PlaneObject_t*) enemyBul->data;uint8_t res = hitEffectDetectOnce(bul->x, bul->y, 1);if (res) {bul->visiable = 0;}}
}

恢复道具 PropHealth.cpp:这个道具不需要碰撞检测

/** PropHealth.cpp**  Created on: Dec 27, 2023*      Author: YoungMay*/#include "PropHealth.h"PropHealth::PropHealth() {// TODO Auto-generated constructor stub}PropHealth::~PropHealth() {// TODO Auto-generated destructor stub
}
void PropHealth::init(PlaneObject_t *_player) {player = _player;baseInfo.x = player->x;baseInfo.y = player->y;baseInfo.life = 10000;
}uint8_t PropHealth::tick(uint32_t t) {baseInfo.life -= t;player->life += t;if (player->life > PlayerMaxLife)player->life = PlayerMaxLife;return 0;
}uint8_t PropHealth::show() {ws2812_Draw_Circle(player->x / PlaneXYScale, player->y / PlaneXYScale, 4,PropColor[2]);return 0;
}void PropHealth::hitEffectDetect(uint32_t t) {}

护盾道具 PropSheel.cpp: 护盾只能防子弹,不能防飞机。当然,敌我飞机的碰撞也没做。屏幕小,飞机一多就容易混在一起了,所以就不让他们碰了。

/** PropSheel.cpp**  Created on: Dec 27, 2023*      Author: YoungMay*/#include "PropSheel.h"PropSheel::PropSheel() {// TODO Auto-generated constructor stub}PropSheel::~PropSheel() {// TODO Auto-generated destructor stub
}
void PropSheel::init(PlaneObject_t *_player) {player = _player;baseInfo.x = player->x;baseInfo.y = player->y;baseInfo.life = 10000;
}uint8_t PropSheel::tick(uint32_t t) {baseInfo.life -= t;return 0;
}uint8_t PropSheel::show() {ws2812_Draw_Circle(player->x / PlaneXYScale, player->y / PlaneXYScale, 4,PropColor[3]);return 0;
}uint8_t PropSheel::hitEffectDetectOnce(int x, int y, int r) {int a = (x - player->x) / 100;int b = (y - player->y) / 100;int c = (r + 5) * 100;return (a * a + b * b < c * c) ? 2 : 0;
}void PropSheel::hitEffectDetect(uint32_t t) {for (ListNode *enemyBul = EnemyBulletList->next;enemyBul != EnemyBulletList; enemyBul = enemyBul->next) {PlaneObject_t *bul = (PlaneObject_t*) enemyBul->data;uint8_t res = hitEffectDetectOnce(bul->x, bul->y, 1);if (res) {bul->visiable = 0;}}for (ListNode *enemyBul = EnemyRocketList->next;enemyBul != EnemyRocketList; enemyBul = enemyBul->next) {PlaneObject_t *bul = (PlaneObject_t*) enemyBul->data;uint8_t res = hitEffectDetectOnce(bul->x, bul->y, 1);if (res) {bul->visiable = 0;}}
}

类似的方法,还可以加上火力加强道具,玩家导弹道具等等。

最后看看效果:

STM32学习笔记十九:WS2812制作像素游戏屏-飞行射击

STM32学习笔记二十:WS2812制作像素游戏屏-飞行射击游戏(10)探索游戏平衡

这篇关于STM32学习笔记十九:WS2812制作像素游戏屏-飞行射击游戏(9)探索道具系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/573282

相关文章

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

51单片机学习记录———定时器

文章目录 前言一、定时器介绍二、STC89C52定时器资源三、定时器框图四、定时器模式五、定时器相关寄存器六、定时器练习 前言 一个学习嵌入式的小白~ 有问题评论区或私信指出~ 提示:以下是本篇文章正文内容,下面案例可供参考 一、定时器介绍 定时器介绍:51单片机的定时器属于单片机的内部资源,其电路的连接和运转均在单片机内部完成。 定时器作用: 1.用于计数系统,可

问题:第一次世界大战的起止时间是 #其他#学习方法#微信

问题:第一次世界大战的起止时间是 A.1913 ~1918 年 B.1913 ~1918 年 C.1914 ~1918 年 D.1914 ~1919 年 参考答案如图所示

[word] word设置上标快捷键 #学习方法#其他#媒体

word设置上标快捷键 办公中,少不了使用word,这个是大家必备的软件,今天给大家分享word设置上标快捷键,希望在办公中能帮到您! 1、添加上标 在录入一些公式,或者是化学产品时,需要添加上标内容,按下快捷键Ctrl+shift++就能将需要的内容设置为上标符号。 word设置上标快捷键的方法就是以上内容了,需要的小伙伴都可以试一试呢!

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

AssetBundle学习笔记

AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。 目录 1.定义: 2.AssetBundle的生成: 1)设置AssetBundle包的属性——通过编辑器界面 补充:分组策略 2)调用引擎接口API

Javascript高级程序设计(第四版)--学习记录之变量、内存

原始值与引用值 原始值:简单的数据即基础数据类型,按值访问。 引用值:由多个值构成的对象即复杂数据类型,按引用访问。 动态属性 对于引用值而言,可以随时添加、修改和删除其属性和方法。 let person = new Object();person.name = 'Jason';person.age = 42;console.log(person.name,person.age);//'J

大学湖北中医药大学法医学试题及答案,分享几个实用搜题和学习工具 #微信#学习方法#职场发展

今天分享拥有拍照搜题、文字搜题、语音搜题、多重搜题等搜题模式,可以快速查找问题解析,加深对题目答案的理解。 1.快练题 这是一个网站 找题的网站海量题库,在线搜题,快速刷题~为您提供百万优质题库,直接搜索题库名称,支持多种刷题模式:顺序练习、语音听题、本地搜题、顺序阅读、模拟考试、组卷考试、赶快下载吧! 2.彩虹搜题 这是个老公众号了 支持手写输入,截图搜题,详细步骤,解题必备

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

《offer来了》第二章学习笔记

1.集合 Java四种集合:List、Queue、Set和Map 1.1.List:可重复 有序的Collection ArrayList: 基于数组实现,增删慢,查询快,线程不安全 Vector: 基于数组实现,增删慢,查询快,线程安全 LinkedList: 基于双向链实现,增删快,查询慢,线程不安全 1.2.Queue:队列 ArrayBlockingQueue: