RaceGame-Qt游戏项目构建-游戏地图
RaceGame-Qt游戏项目构建-游戏地图
游戏地图概述
游戏界面固定为 450px * 800px;游戏地图由 10px * 10px 像素的方块构成,采用等比缩放记录在一个 45 * 80 的 array 容器中。
GameMap 相关类
GameMap
相关类放在 gamemap.h
头文件中,对应的源文件是 gamemap.cpp
。
一、 class GameMap
核心类,储存一个静态的类型为成员变量,std::array<std::array<char,MapHeight>,MapWidth>
; 储存地图信息。使用char
字符类型储存地图中墙体信息,地图中的墙体类型总数不超过10,因此使用char
类型即可。
#define MapWidth 45 #define MapHeight 80 // singleton class GameMap : public QObject { Q_OBJECT protected: QPoint endPoint; public: typedef std::array<std::array<char,MapHeight>,MapWidth> mapType; static mapType gameMap; GameMap(); void createFrame(); void createRoom(); void createEndPoint(); Q_INVOKABLE char getlevel(int x, int y); Q_INVOKABLE QPoint getEndPoint() {return this->endPoint; } };
具体的地图信息由它的派生类实现。
二、 GameMap的派生类
class gameMap_1 : public GameMap { public: gameMap_1(); }; class gameMap_2 : public GameMap { public: gameMap_2(); }; class gameMap_3 : public GameMap { public: gameMap_3(); }; class gameMap_4 : public GameMap { public: gameMap_4(); }; // ...
使用不同的游戏地图,会具有不同的墙体信息,墙体信息的初始化在这些派生类的构造函数中实现。
三、 class wallLevel
枚举类,储存一个枚举类型,表示不同的墙体信息。
0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
空 | 玩家1 | 玩家2 | 玩家3 | 玩家4 | 墙体 |
其中 “玩家1” 等,表示指定玩家可以通过的墙,而墙体则所有玩家都不能通过。即会发生碰撞。
class wallLevel { public: enum level { SPACE = 0, Player_1 = 1, Player_2 = 2, Player_3 = 3, Player_4 = 4, WALL = 5, ENDPOINT = 6 }; };
四、class mapsManager
地图管理器,其中存放一个指向地图信息的共享指针;可以从管理器中通过地图ID获取相应地图。
// singleton class mapsManager { private: static std::shared_ptr<mapsManager> mapsMgr; std::shared_ptr<GameMap> map; mapsManager(); public: static std::shared_ptr<mapsManager> getInstance(); std::shared_ptr<GameMap> getMap(); void createMap(int mapId); void destoryMap(); };
通过管理器的 getMap()
方法就可以获取相关地图,并且返回一个指向这个地图的指针。如果切换地图,只需要调用 createMap()
即可。地图信息储存在 class GameMap
的静态变量中。
本文作者:北纬31是条纬线哦
本文链接:https://www.cnblogs.com/beiwei31/p/18663028
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步