RaceGame-Qt游戏项目构建-游戏框架
RaceGame-Qt游戏项目构建-游戏框架
游戏企划
使用Qt图形化界面开发一款名为 RaceGame 的小游戏,游戏玩法是4方玩家(方块)在带有墙体的地图中以一定速度、一定方向前进,碰到墙体会反弹,最终玩家按照到达目的地的先后顺序排名。
游戏过程中,玩家可以通过界面上的 Button 按钮进行释放技能,不同的技能会对不同的玩家进行状态改变,以此增加游戏的自由度和可玩性。
Player相关类
Player
相关类在放在 player.h
头文件中,对应的源文件是 player.cpp
。
一、 class Player
核心类:玩家,代表在游戏界面运动的玩家,也就是小方块。
class Player : public QObject
{
public:
Player(int id, int speed,
QPoint pos, QPointF vec);
private:
int playerId;
int speed;
QPoint position;
QPointF moveVector;
std::shared_ptr<Tools> tool;
QTimer * preLaunchTimer;
public:
QMetaObject::Connection playerMoveConnection;
QMetaObject::Connection playerDrawConnection;
std::shared_ptr<Tools> getTool();
void useTool();
void stopTool();
void setPosition(QPoint pos);
int getPlayerId() {return this->playerId;}
QPoint getPosition() {return this->position; }
void setMoveVector(QPointF vec);
QPointF getMoveVector() {return this->moveVector;}
void setSpeed(int s);
int getSpeed() {return this->speed; }
void moveStep();
void reachEndPoint();
};
具有私有属性(成员变量)玩家号,速度,位置,以及运动方向等。
公有成员函数:获取或设置对象的私有属性的方法;获取道具方法,可以从游戏的 7 种工具中随机获得一个道具;使用道具方法:使用获取的道具,通过 QTimer * preLaunchTimer
计时器类型控制使用时限,10秒内使用道具,否则自动释放道具。停止使用道具方法:当使用道具,道具具有特定时长,当特定时长结束后,玩家会停止使用道具,并根据情况是否恢复道具对玩家的效果。
二、 class Tools
道具,代表玩家使用的道具。
class Tools : public QObject {
Q_OBJECT
public:
Tools(int duration, QString toolname, int usr_id);
virtual ~Tools();
protected:
QTimer * toolTimer;
int duration;
QString toolname;
int usrId;
public:
QString getName() {return this->toolname;}
virtual void launchTool() = 0;
virtual void recoveryTool() = 0;
};
道具构造的时候需要传入参数,道具的持续时间,道具的名字,使用者的ID,并对私有成员变量初始化。
道具具有两个纯虚函数,分别是 释放道具、恢复道具。需要在子类的具体道具类型中进行重写,释放道具即使用者使用工具的效果,恢复道具即使用完道具后玩家需要恢复原有状态。
virtual void launchTool() = 0;
virtual void recoveryTool() = 0;
三、 工具派生类
构造工具类的时候需要传递一个用户Id的参数。
代表具体的工具。工具的效果在派生类中实现,每个工具都有 void launchTool();
; 和 void recoveryTool();
方法,重写基类的对应方法。。
// 冻结
// 冻结除自己以外随机的一位玩家(5)秒。
class frizenTool : public Tools {};
// 监狱
// 在自己后方放置一堵墙(5)秒。
class prisonTool : public Tools {};
// 弹射
// 提高自己速度(5)秒后逐渐降低为原速度。
class jumpTool : public Tools {};
// 滑行
// 永久提高自己速度 1 点。(最高提高3 点)
class slideTool : public Tools {};
// 冰霜
// 永久降低自己速度 1 点。(最多降低 3 点)
class resistanceTool : public Tools {};
// 变换
// 随机改变运动方向
class transformTool : public Tools {};
// 破坏(稀有)
// 直接撞碎一堵墙,但此次会反弹
class destoryTool : public Tools {};
例如,冻结(frizenTool
)工具的作用是冻结除自己以外的一位玩家5秒。 lanchTool()
函数实现冻结的效果,并开始计时,五秒之后触发槽函数,调用 recoveryTool()
函数,就需要把原来冻结的玩家解除冻结,即使玩家的速度恢复到原来的速度。
四、 class playersManager
玩家管理类,可以创建指定个数的玩家,以及记录玩家数量,记录玩家轮流情况。具有一个静态成员变量 PlayerList
,存放创建好的玩家对象。
class playersManager {
friend class Player;
friend class Tools;
private:
public:
playersManager();
static int nemberNum;
static int PlayerTrun;
static int reachPlayerNum;
void createPlayer(int memberNum = 1);
static QList<std::shared_ptr<Player>> PlayerList;
static void trunNextPlayer();
};
指定了两个友元类,Player
类和 Tools
类可以直接获取管理器的成员变量、成员函数。
五、 class playersMove
玩家移动管理器,用于计时玩家的移动。
class playersMove : public QObject {
Q_OBJECT
public:
playersMove();
private:
static QTimer * playerMoveTimer;
public:
void startTimer();
void stopTimer();
void move(std::shared_ptr<Player>& player);
void stop(std::shared_ptr<Player>& player);
};
playerMoveTimer
是一个 QTimer*
类型的静态变量,用于对玩家的移动计时。 定义了一个宏 #define MOVE_FLASH 26
,playerMoveTimer
每 MOVE_FLASH
毫秒发送一次信号,调用相应槽函数,使玩家移动一步(一次)。玩家移动一次的方法是 Player
类中的:void moveStep();
六、 class Triangle
用静态常量类型存放一些特殊向量。
// 特殊向量
class Triangle {
public:
static const QPointF right_vec ;
static const QPointF left_vec ;
static const QPointF up_vec ;
static const QPointF down_vec ;
static const QPointF up_right ;
static const QPointF up_left ;
static const QPointF down_right;
static const QPointF down_left ;
};