重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
前言:本项目所有图片素材均来自网络,如有侵权,请及时联系;由于项目代码量较多,仅展示部分代码,需要源文件私信或评论里留下邮箱,无偿交流。
本项目是基于C++重构的练习项目,具体功能与原项目基本相同,但实现方法更为精简,并且加入了网络编程以实现联机,原项目可参考链接C++横板格斗小游戏。
二、具体功能设计同样,由于代码量较多,仅在这里展示一下类的设计,由于具体功能设计与上篇文章一致,不再过多赘述。
2.1 Npc基类#ifndef NPC_H
#define NPC_H
#include#include#include#includeclass GameObject;
class npc : public QObject
{Q_OBJECT
public:
npc(GameObject *a,int s);
npc(GameObject *a);
void hideAll();
void showNpcReady();
int getNpcx();
int getNpcy();
void setNpcxy(int x,int y);
protected:
int npcx,npcy;
QMovie* npcready;
QLabel* npcreadylab;
signals:
};
#endif // NPC_H
继承自QObject类,用于绘制npc。
2.2 Boss子类#ifndef BOSS_H
#define BOSS_H
#include "npc.h"
class boss : public npc
{public:
boss(GameObject *a,int s);
void hideAll();
void showNpcReady();
void showLeft();
void showRight();
void showUp();
void showDown();
void showYS();
void showYX();
void showZS();
void showZX();
void showAttack();
private:
int bosshp = 1000,bossmp = 1000;
QMovie* bossright;
QLabel* bossrightlab;
QMovie* bossleft;
QLabel* bossleftlab;
QMovie* bossup;
QLabel* bossuplab;
QMovie* bossdown;
QLabel* bossdownlab;
QMovie* bossys;
QLabel* bossyslab;
QMovie* bossyx;
QLabel* bossyxlab;
QMovie* bosszs;
QLabel* bosszslab;
QMovie* bosszx;
QLabel* bosszxlab;
QMovie* bossattack;
QLabel* bossattacklab;
};
#endif // BOSS_H
用于绘制BOSS的子类,使用QMovie和QLabel根据设定逻辑播放gif图以表示BOSS。
2.3 Hero类#ifndef HERO_H
#define HERO_H
#include#include#include#include#include#includeclass GameObject;
class hero :public QObject
{Q_OBJECT
public:
hero(GameObject* a);
~hero();
void hideAll();
void hideBar();
void showHeroReady();
void showLeft();
void showRight();
void showUp();
void showDown();
void showYS();
void showYX();
void showZS();
void showZX();
void showguanghuan();
void showLeftAttack();
void showRightAttack();
void showSkill1();
void showjian(int a);
void showHerohp();
void showHeromp();
int getHerox();
int getHeroy();
void setHeroxy(int x,int y);
int getHerohp();
int getHeromp();
void setHerohp(int hp);
void setHeromp(int mp);
void setHeroAttribute(int hp,int mp);
bool getHright();
bool getHleft();
bool getHup();
bool getHdown();
void setHright(bool k);
void setHleft(bool k);
void setHup(bool k);
void setHdown(bool k);
bool getHfacer();
bool getHfacel();
void setHfacer(bool k);
void setHfacel(bool k);
bool getheroskill1();
bool getheroskillall();
bool getheroskilljian();
void setheroskilljian(bool k);
void setheroskill1(bool k);
void setheroskillall(bool k);
private:
int herox,heroy,herohp = 1000,heromp = 1000;
bool hright = false,hleft = false,hup = false,hdown = false,hfacer = true,hfacel = false,heroskill1 = false,heroskilljian = false,heroskillall = false;
QMovie* heroready;
QLabel* heroreadylab;
QMovie* heroleft;
QLabel* heroleftlab;
QMovie* heroright;
QLabel* herorightlab;
QMovie* heroup;
QLabel* herouplab;
QMovie* herodown;
QLabel* herodownlab;
QMovie* heroys;
QLabel* heroyslab;
QMovie* heroyx;
QLabel* heroyxlab;
QMovie* herozs;
QLabel* herozslab;
QMovie* herozx;
QLabel* herozxlab;
QMovie* heroguanghuan;
QLabel* heroguanghuanlab;
QMovie* rightattack;
QLabel* rightattacklab;
QMovie* leftattack;
QLabel* leftattacklab;
QMovie* jian1;
QLabel* jian1lab;
QMovie* jian2;
QLabel* jian2lab;
QMovie* skill1;
QLabel* skill1lab;
QProgressBar *herohpbar;
QProgressBar *herompbar;
public slots:
};
#endif // HERO_H
//This ain’t for the weak links or the faint of heart
//The foes coming up ahead are bound to make a mark
用于绘制玩家,同样使用QMovie和QLabel绘制玩家的各种行动状态。
2.4 Widget类#ifndef WIDGET_H
#define WIDGET_H
#include#include#include//标签
#include#include#include#include#include#include#include#include#includeclass Widget : public QWidget
{Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
void bgmove();
void run();
void hideall();
void showall();
QTcpSocket* getsocket();
int otherx = 0;
int othery = 0;
int othermapindex = 0;
bool otherlink = false;
bool otherleft = false;
bool otherright = false;
bool otherup = false;
bool otherdown = false;
void mouseMoveEvent(QMouseEvent *event);
private:
QMovie* login;
QLabel* loginlab;
QLabel *lab1, *lab2;
QLineEdit *editID, *editPwd;
QPushButton *btn1,*btn2;
QTcpSocket *socket;
public slots:
void cancelSlot();
void connectedSlot();
void disconnectedSlot();
void readData();
void loginrequest();
};
#endif // WIDGET_H
用于绘制登录界面,接收并保存其他客户端玩家的坐标和状态信息。
2.5 GameContral基类#ifndef GAMECONTRAL_H
#define GAMECONTRAL_H
#include//视图
#include//场景
#include//图形元素
#include#include "widget.h"
#include#include#include#include#include#include#include#include#include#includeclass hero;
// gamecontral基类 设置定时器 淡入淡出特效 放大缩小特效 视频、图片路径加载
class GameContral : public QGraphicsView
{Q_OBJECT
public:
GameContral(Widget* a);
~GameContral();
void fadeIn(QGraphicsPixmapItem *a);
void fadeOut(QGraphicsPixmapItem *a);
Widget* b; //用于保存主窗口的地址
protected:
QGraphicsOpacityEffect *opacity;
QPropertyAnimation *opacityAnimation;
QPushButton *startgamebtn,*endgamebtn,*returnstartbtn,*returnbtn,*exitbtn;
QGraphicsScene mscence;
QMediaPlayer *player;
QVideoWidget *videoWidget;
QMediaPlayer *pause;
QVideoWidget *pauseWidget;
QMovie* teleport;
QLabel* teleportlab;
QMovie* teleportinmaintown;
QLabel* teleportinmaintownlab;
QGraphicsPixmapItem background[5];
QTimer *centertime;
QTimer *centertime1;
QTimer *btntime;
QTimer *herokeytime;
QTimer *heromovetime;
QTimer *heromovetimeinother;
public slots:
void centerxyin(); //窗口放大
void centerxy(); //窗口缩小
};
#endif // 1_H
gamecontral基类 用于设置定时器 淡入淡出特效 放大缩小特效 视频、图片路径加载
2.6 GameObject子类#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
#include "GameContral.h"
class hero;
class npc;
class boss;
//游戏对象子类 控制游戏运行逻辑
class GameObject : public GameContral
{Q_OBJECT
public:
GameObject(Widget *a);
~GameObject();
void startGame();//开始游戏
void mainTown();
void nightMarket();
void WarfareField();
void heroActionCheck();
void heroLocationCheck();
void heroLocationCheckInOther(); //hero位置检测
void heroSkillCheck();
void bossActionCheck(); //boss行为检测
void bossLocationCheckInOther(); //boss位置检测
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
private:
hero *ps;
hero *other;
npc *pn;
boss *pb;
int bossrandom = 0;
int mapindex = 0;
QListherokeylist;
QListherokeyreleaselist;
QTimer *otherherotime;
QTimer *heroskilltime;
QTimer *sendxytime;
QTimer *bossrandomtime;
QTimer *bossactiontime;
QTimer *bosssuperskilltime;
QTimer *bossmovetimeinother;
public slots:
void gotostart(); //返回菜单
void re(); //退出
void gotomaintown(); //前往主城镇
void herokeycheck();
void returnthis();
void returnstart();
};
#endif // GAME_H
游戏对象子类,控制游戏运行逻辑,使用大量定时器并重写按键按下抬起虚函数以实现具体的行为控制。
2.7 服务器端#ifndef DATASQL_H
#define DATASQL_H
#include#include#include#includeclass DataSql
{public:
~DataSql();
static QListallheroxylist;
static int clientindex;
static DataSql* getDataSql();//获取到唯一的对象
int checkLogin(QString id,QString password);
private:
DataSql();
static DataSql *pIntentce;// 保存创建好的唯一的对象的地址
QSqlDatabase database;//数据库指针
};
#endif // DATASQL_H
数据库单例类,用作登录
#ifndef SERVERFIX_H
#define SERVERFIX_H
#include "threadfix.h"
class ServerFix : public QTcpServer
{struct Dlt
{ThreadFix * thread_dlt;
Dlt * next;
};
Dlt * dlt_ = 0;
public:
ServerFix(QObject * parent = Q_NULLPTR) : QTcpServer(parent){}
~ServerFix();
protected:
void incomingConnection(qintptr socketDescriptor);
};
#endif // SERVERFIX_H
重写的QTcpSocket类,重写虚函数incomingConnection,在其中创建线程用于多线程通信。
#ifndef THREADFIX_H
#define THREADFIX_H
#include#include#include#include#include#include#include#includeclass ThreadFix : public QThread
{public:
ThreadFix(qintptr s) : QThread()
{socketDescriptor = s;
}
void run(); //重写QThread虚函数 run()
~ThreadFix();
private:
int herox = 0;
int heroy = 0;
int mapindex = 0;
QByteArray arry;
QTcpSocket * socket;
qintptr socketDescriptor;
//signals:
// void sendxy(QTcpSocket * socket);
};
#endif // THREADFIX_H
重写的QThread类,重写run虚函数以进行具体的信息收发处理。
#ifndef WIDGET_H
#define WIDGET_H
#include#include#include#include#include#include#include#include#include#include "serverfix.h"
class Widget : public QWidget
{Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QByteArray arry;
QTimer *sendtime;
ServerFix *server;
};
#endif // WIDGET_H
Widget类,用于监听客户端。
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧