当前位置:编程学习 > C/C++ >>

利用HGE引擎设计的角色部分

游戏中角色类的设计(C 语言描述)
最近一段时间自己正在编写一款RPG游戏,前几天用空余的时间设计了角色模块,给热爱游戏编成的朋友们分享分享,代码分为三部分:1.抽象角色类,2.玩家控制的角色类,3角色管理类.本游戏使用的是hge 2D引擎 代码如何:


一:抽象角色类:

FGRoleObject.h文件:

class FGRoleObject { public:

static void Init(HGE* m_hge) { hge = m_hge; }

/ /逻辑 virtual void Logic() = 0;

//绘制 virtual void Render() = 0;

//获取X坐标 float GetX(){return m_x;}

//获取Y坐标 float GetY(){return m_y;}

/ /获取ID int GetID(){return m_ID;}

//获取名字 char* GetName(){return m_Name;}

//获取等级 int GetLevel(){return m_Level;}

//获取HP float GetHp(){return m_Hp;}

//获取MaxHp float GetMaxHp(){return m_MaxHp;}

//获取Ep float GetEp(){return m_Ep;}

//获取MaxEp float GetMaxEp(){return m_MaxEp;}

//获取CP float GetCp(){return m_CP;}

//获取MaxCp float GetMaxCp(){return m_MaxCp;} //获取STR float GetSTR(){return m_STR;}

//获取DEF float GetDEF(){return m_DEF;}

//获取ATS float GetATS(){return m_ATS;}

//获取ATF float GetADF(){return m_ADF;}

//获取SPD float GetSPD(){return m_SPD;}

//获取DEX float GetDEX(){return m_DEX;}

//获取ADL float GetAGL(){return m_AGL;}

//获取MOV float GetMOV(){return m_MOV;}

//获取RNG float GetRNG(){return m_RNG;}

//获取EXP float GetEXP(){return m_Exp;}

//获取MaxExp float GetMaxExp(){return m_MaxExp;}

//获取阵营类型 TYPE_Camp GetCamp(){return m_Camp;}

//设置碰撞区域范围 void SetRect(float ltx,float lty,float rdx,float rdy) { m_rect.Set(ltx,lty,rdx,rdy); }

//角色与角色之间的碰撞处理 bool Intersect(FGRoleObject& role) { if( m_rect.Intersect(&role. m_rect)) { return true; } else { return false; } }

//设置英雄属性 void SetAttribute(int _level,float _MaxHp,float _Hp,float _MaxEp, float _Ep,float _STR,float _DEF,float _ATS, float _ADF,float _SPD,float _DEX,float _AGL,float _MOV,float _RNG,float _MaxCp,float _CP, float _MaxExp,float _Exp,float _NextExp); //设置状态 void SetMode(AtionMode _mode) { m_ationmode = _mode; } protected: //1.编号 2.阵营标示 3.角色名称 4.角色X坐标 5.角色Y坐标 FGRoleObject(short _id, TYPE_Camp _camp,const char* _name, float _x,float _y);

//析构 virtual ~FGRoleObject(void);

static HGE* hge;

//金钱 static float Mria;

//游戏时间 static float PlayTime;

//阵营的种类 TYPE_Camp m_Camp;

//字符 char m_Name[256];

//编号 short m_ID;

//角色x坐标 float m_x;

//角色y坐标 float m_y;

//碰撞区域对象 hgeRect m_rect;

//速度 float m_speed;

//角色属性 int m_Level;

float m_MaxHp;

float m_Hp;

float m_MaxEp;

float m_Ep;

float m_STR; float m_DEF;

float m_ATS; float m_ADF; float m_SPD; float m_DEX; float m_AGL; float m_MOV;

float m_RNG;

float m_MaxCp;

float m_CP;

float m_MaxExp;

float m_Exp;

float m_NextExp;

//行走方向 Drection m_drection;

//角色状态 AtionMode m_ationmode; };


FGRoleObject.cpp文件:


#include "FGRoleObject.h"

HGE* FGRoleObject:: hge = 0;

FGRoleObject::~FGRoleObject(void){

}


FGRoleObject::FGRoleObject(short _id, TYPE_Camp _camp,const char* _name, float _x,float _y){ m_ID = _id;

//赋值阵营-我方还是敌方 m_Camp = _camp;

strcpy( m_Name,_name);

m_x = _x;

m_y = _y;

SetMode(WAIT);

//初始化的时候没有设置碰撞区域,碰撞为0 m_rect.Set(0,0,0,0);}


void FGRoleObject::SetAttribute(int _level,float _MaxHp,float _Hp,float _MaxEp, float _Ep,float _STR,float _DEF,float _ATS, float _ADF,float _SPD,float _DEX,float _AGL,float _MOV,float _RNG,float _MaxCp,float _CP, float _MaxExp,float _Exp,float _NextExp){ m_Level = _level;

m_MaxHp = _MaxHp;

m_Hp = _Hp;

m_MaxEp = _MaxEp;

m_Ep = _Ep;

m_STR = _STR; m_DEF = _DEF;

m_ATS = _ATS; m_ADF = _ADF; m_SPD = _SPD; m_DEX = _DEX; m_AGL = _AGL; m_MOV = _MOV;

m_RNG = _RNG;

m_MaxCp = _MaxCp;

m_CP = _CP;

m_MaxExp = _MaxExp;

m_Exp = _Exp;

m_NextExp = _NextExp; }


FGMyRole.h文件:


class FGMyRole :public FGRoleObject{public: //基类的参数:1.编号 2.阵营标示 3.角色名称 4.角色X坐标 5.角色Y坐标 //hgeAnimation类需要参数: 6.贴图中的x坐标 7.贴图中的y坐标 8.贴图中每一帧的宽 9.贴图中每一帧的高 // 10.动画播放的速度 11.动画总共的帧数 12.动画所需要的贴图 //我方角色本身的参数: 13.UI中角色人物图片 14.角色的移动的速度 FGMyRole(short _id, TYPE_Camp _camp, char* _name,float _x, float _y, float _tx, float _ty, float _tw, float _th, float _fps, int _nframe, HTEXTURE _tex,short _h_Index, float m_speed); virtual ~FGMyRole(void); void Logic();


void Render();


short GethSpri_UIIndex() { return hSpri_UIIndex; }


//初始化装备槽 void InitEquips();


//装备栏数组-次游戏拥有5个装备槽 ItemObject* Equips[5];

private: //角色头像图片索引(UI ) short hSpri_UIIndex;

//动画对象 hgeAnimation* spr[8];

//fps播放的速度 float FPS;

//帧数 int Nframe;

//贴图中的X坐标 float tx;

//贴图中的Y坐标 float ty;

//贴图中的长 float tw;

//贴图中的宽 float th; };


FGMyRole.cpp文件:


#include "FGMyRole.h"#include "FGGameScreen.h"

FGMyRole::FGMyRole(short _id, TYPE_Camp _camp, char* _name,float _x, float _y, float _tx, float _ty, float _tw, float _th, float _fps, int _nframe, HTEXTURE _tex,short _h_Index, float _speed):FGRoleObject(_id,_camp,_name,_x,_y) { SetAttribute(4,163,163,250,203,44,46,20,15,15,16,4,4,2,46,46,500,100,250);

m_speed = _speed;

FPS = _fps;

Nframe = _nframe;

tx = _tx;

ty = _ty;

tw = _tw;

th = _th;

hSpri_UIIndex = _h_Index;

for(int i=0;i<8;i ) { spr[i] = new hgeAnimation(_tex,Nframe,FPS,tx,ty 128*i,tw,th);

spr[i]->Play(); } m_drection = RIGHT; //初始化角色装备 InitEquips(); }

FGMyRole::~FGMyRole(void){ for(int i=7;i<0;i--) { if(spr[i]!=NULL) { delete spr[i];

spr[i] = NULL; } }}

void FGMyRole::Logic(){ //动画更新 spr[m_drection]->Update(hge->Timer_GetDelta());

//状态 switch(m_ationmode) { case MOVE: //键盘响应 if(hge->Input_GetKeyState(HGEK_W)) //上 { if(hge->Input_GetKeyState(HGEK_D)) { m_y-=m_speed; m_x =m_speed; m_drection = RIGHTUP;

} else if(hge->Input_GetKeyState(HGEK_A)) { m_y-=m_speed; m_x-=m_speed; m_drection = LEFTUP; } else if(hge->Input_GetKeyState(HGEK_S)) { m_y =m_speed;

m_drection = DOWN; } else { m_y-=m_speed;

m_drection = UP; }

spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1")); } else if(hge->Input_GetKeyState(HGEK_S)) //下 { if(hge->Input_GetKeyState(HGEK_W)) { m_y-=m_speed;

m_drection = UP;

} else if(hge->Input_GetKeyState(HGEK_D)) { m_x =m_speed;

m_y =m_speed;

m_drection = RIGHTDOWN; } else if(hge->Input_GetKeyState(HGEK_A)) { m_x-=m_speed;

m_y =m_speed; m_drection = LEFTDOWN; } else { m_y =m_speed; m_drection = DOWN; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1")); } else if(hge->Input_GetKeyState(HGEK_D)) //右 { if(hge->Input_GetKeyState(HGEK_W)) { m_x =m_speed;

m_y-=m_speed;

m_drection = RIGHTUP; } else if(hge->Input_GetKeyState(HGEK_S)) { m_x =m_speed;

m_y =m_speed;

m_drection = RIGHTDOWN; } else if(hge->Input_GetKeyState(HGEK_A)) { m_x-=m_speed;

m_drection = LEFT; } else { m_x =m_speed; m_drection = RIGHT; } spr[m_drection]->SetTexture(FGResMangner::mangner->GetTexture("role2-move1")); } else if(hge->Input_GetKeyState(HGEK_A)) //左 { if(hge->Input_GetKeyState(HGEK_W)) { m_x-=m_speed;

m_y-=m_speed;

m_d

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,