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 中国大陆许可协议进行许可。

posted @   北纬31是条纬线哦  阅读(7)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 If 丁可
If - 丁可
00:00 / 00:00
An audio error has occurred.

I came your danger soul 我靠近你危险的灵魂

Think so you'll say hello 想着你会主动打招呼

Breaking you find to go 忽然你想要转身离去

Break down you might be lone 黯然神伤或许你也会孤独吧

Angel you down thinking 天使如你可否冷静思虑

Think so you freaking down 还以为你会不药而愈

Say hi to send you go 一声问候就让你安心离开

Break down you might be lone 黯然神伤或许你也会孤独吧

And you know you saying to go 你自顾自洒脱的离去

Breaking down I find to go 终于遍体鳞伤的我选择离开

I can't down to soul 怎敢抚慰那深至灵魂的痛

You don't know I love you so 却不知这份爱的深沉

Angel now you think so 我的爱人事到如今你还会这样想吧

Don't freak the danger soul 是我不该诱惑你危险不安的灵魂吧

Breaking I find to go 终于遍体鳞伤的我选择离开

Break down you might be lone 黯然神伤或许你也会孤独吧

And you know you saying to go 你自顾自洒脱的离去

You don't know I love you so 却不知这份爱的深沉

Breaking down you trying to go 伤痕累累你试图不告而别

I can't down to soul 怎敢抚慰那深至灵魂的痛

And you know you trying to go 你自顾自洒脱的离去

You don't know I love you so 却不知这份爱的深沉

Breaking down you trying to go 伤痕累累你试图不告而别

I can't down to soul 怎敢抚慰那深至灵魂的痛