myplayer.h
来自「gamecode 很不错的小游戏源代码」· C头文件 代码 · 共 535 行
H
535 行
//************************************************************
//游戏角色类
//作者:曾铮
//时间:2004年6月
//说明:游戏中玩家和电脑的角色类,由成员变量iscomp来标识。
// 该类的成员变量及函数自开始写以来,一直在不断的协调,但始终应保持较高独立。
// 该类处理角色的行走,但是兼顾屏幕显示的协调。
//************************************************************
#ifndef MyPlayer_h_
#define MyPlayer_h_
#include "MyDdraw.h"
#include "MyMaps.h"
//物品头文件
#include "stopcard.h"
#include "supercar.h"
#include "remodice.h"
#include "tortcard.h"
#include "passport.h"
#include "compmanager.h"
#include "buycard.h"
#include "Myllist.h"
#include "MyTool.h"
//#include "CZDemo.h"
#include "time.h"
#include "path.h"
#include "mmsystem.h"
#pragma comment(lib,"winmm.lib")
#define GODOWN 0
#define GOLEFT 1
#define GOUP 2
#define GORIGHT 3
#define MYERROR -1
extern int DX,DY;
extern int SX,SY;
extern int Rx,Ry;
extern int DBX,DBY;
class MyPlayer
{
public:
int x;//当前位置。
int y;
int old_x;//上一步位置。
int old_y;
int status;//显示步伐图片编号。(每个方向3张图)
int orientation;//down=0,l=1,r=2,u=3.
int oldorient;//上步的人物朝向。
int rollx;//是否需要滚屏x
int rolly;//是否需要滚屏y
int contribute;//角色贡献值。
char honour[20];//玩家封号
char name[20];//玩家名字
bool existence;//是否还存在此玩家
int level;//玩家级别
int speed;//玩家走动速度
//卡片效果
bool stop;//是否在原地停留
bool doubspeed;//是否有双倍速度
int choosestep;//是否选择行走步数
bool onestep;//是否只能走一步
int buildingnum;//建筑数量
int factorynum;//工厂数量
LPDIRECTDRAWSURFACE7 pPhoto1;//人物图片50*50大小
linklist<MyTool*> tools;//玩家物品链
int toolindex;//物品链索引(toolindex>=1)
// int nextori;
bool iscomp;//是否是电脑玩家
public:
long money;
int exp;
public:
MyPlayer();
MyPlayer(int px ,int py ,int pstatus ,int porientation,bool iscomputer=false );//设定初始位置。
bool IsComp();//返回是否为电脑
bool LoadPhoto(char* path);//加载图片
void PlayerGo(int ori); //角色行走,改变坐标。
void PlayerGoEx(int ori);//根据新的路径选择算法行走,需传行走初始方向。
void DrawPlayer(LPDIRECTDRAWSURFACE7 pBBuf,LPDIRECTDRAWSURFACE7 pPlayer);//画角色。
void KeepOldxy();//保持在旧的坐标上。
bool AffirmGo();//确定行走坐标。
bool AffirmOri();//确定面对方向。
void LevelUp();//角色升级
void SetRxy();//调整屏幕的显示坐标。
void ScreenAdj();//屏幕显示修正。
void GetProfit();//取得所有投资在一个时间里的回报。
void CheckRoll();//返回是否需要滚动
bool BuyTools(MyTool *tool,int playerid);//购买物品
bool UseTool(int index);//使用物品
bool IsExist();//是否还存在
public:
long GetMoney();
int GetExp();
void ChangeMoney(long m);
void ChangeExp(int e);
virtual ~MyPlayer();
};
//构造函数中所有数值仅用于测试
MyPlayer::MyPlayer(int px ,int py ,int pstatus ,int porientation,bool iscomputer )
{
this->x=px;
this->y=py;
strcpy(this->honour,"新人");
this->status=pstatus;
this->orientation=porientation;
this->oldorient=orientation;
this->old_x=x;
this->old_y=y;
this->money=300;
this->contribute=0;
this->level=0;
this->speed=8;
this->toolindex=1;
this->iscomp=iscomputer;
this->existence=true;
//由于人物类在DirectDraw初始化之前建立,所以在构造函数中不能使用CreateBitmap
// CreateBitmap(pPhoto1,50,50,"photo/photo01.bmp",DDSCAPS_SYSTEMMEMORY);
}
MyPlayer::MyPlayer()
{
strcpy(honour,"新人");
this->money=300;
this->contribute=0;
this->level=0;
this->speed=8;
this->toolindex=1;
this->iscomp=false;
this->existence=true;
}
bool MyPlayer::LoadPhoto(char* path)
{
if(CreateBitmap(this->pPhoto1,140,163,path,DDSCAPS_SYSTEMMEMORY))
{
DDCOLORKEY key;
key.dwColorSpaceLowValue=0xff00ff;
key.dwColorSpaceLowValue=0xff00ff;
this->pPhoto1->SetColorKey(DDCKEY_SRCBLT,&key);
return true;
}
else
return false;
}
MyPlayer::~MyPlayer()
{
if(pPhoto1!=NULL)
{
pPhoto1->Release();
pPhoto1=NULL;
}
tools.clearlist();
}
bool MyPlayer::IsComp()
{
return this->iscomp;
}
bool MyPlayer::IsExist()
{
return this->existence;
}
//设置屏幕显示的相对坐标。
void MyPlayer::SetRxy()
{
//请参看CheckRoll()的说明。
this->CheckRoll();
switch (this->rollx)
{
case 1://人物在大地图的最左半边内
SX=DX;//坐标系不变
break;
case 2:
SX=DBX-32;//屏幕左边界为大屏幕的二分界线
break;
case 3:
SX=this->x/32-16;//人物的绝对坐标减去屏幕一半的矩阵长度。
break;
default:
return;
break;
}
switch (this->rolly)
{
case 1://对于纵坐标,同上。
SY=DY;
break;
case 2:
SY=DBY-24;
break;
case 3:
SY=this->y/32-12;
break;
default:
return;
}
return;
}
//滚动判定函数:
void MyPlayer::CheckRoll()
{
/* -----变量说明-------
int SX=0,SY=0;//屏幕大小的矩阵第一元素索引。
int DX=0,DY=0;//地图矩阵左上角。
int DBX=64,DBY=48;//地图矩阵右下角。
*/
//设定SX
if( (this->x-DX*32) <= (ScreenW / 2) )//人物在大地图的最左半边内
{
this->rollx=1;
}
else if( (DBX*32 - this->x) < (ScreenW / 2))//人物到了大地图的最右半边内
{
this->rollx=2;
}
else//人物在一般地方(人物应处于屏幕中心)
{
this->rollx=3;
}
//设定SY:
if( (this->y-DY*32) <=(ScreenH / 2))
{
this->rolly=1;
}
else if( (DBY*32 - this->y) < (ScreenH / 2))
{
this->rolly=2;
}
else
{
this->rolly=3;
}
}
//修正屏幕显示:
void MyPlayer::ScreenAdj()
{
switch (this->orientation)
{
case GODOWN: //向下:
{
Ry+=8;
}
break;
case GOLEFT: //向左:
{
Rx-=8;
}
break;
case GOUP://向上:
{
Ry-=8;
}
break;
case GORIGHT: //向右:
{
Rx+=8;
}
break;
}
}
//行走。
void MyPlayer::PlayerGo(int ori)
{
switch (ori)
{
case GODOWN: //向下:
{
this->orientation=0;
this->y+=32;
this->status++;
if( this->status>=3 )
this->status=0;
}
break;
case GOLEFT: //向左:
{
this->orientation=1;
this->x-=32;
this->status++;
if( this->status>=3 )
this->status=0;
}
break;
case GOUP://向上:
{
this->orientation=2;
this->y-=32;
this->status++;
if( this->status>=3 )
this->status=0;
}
break;
case GORIGHT: //向右:
{
this->orientation=3;
this->x+=32;
this->status++;
if( this->status>=3 )
this->status=0;
}
break;
}
this->SetRxy();
}
//行走ex。
void MyPlayer::PlayerGoEx(int ori)
{
this->orientation=ori;
//orientation=GetNextDirection( (x+16)/32,(y+47)/32, orientation);//放在了外面
// SetRxy();//放在这里也可以
this->CheckRoll();
switch (this->orientation)
{
case GODOWN: //向下:
{
this->orientation=0;
if(this->rolly==3)
{
Ry-=this->speed;
if(Ry<=-32)
{
Ry=0;
SY++;
}
}
this->y+=this->speed;
this->status++;
if( this->status>=3 )
this->status=0;
}
break;
case GOLEFT: //向左:
{
this->orientation=1;
if(this->rollx==3)
{
Rx+=this->speed;
if(Rx>=32)
{
Rx=0;
SX--;
}
}
this->x-=this->speed;
this->status++;
if( this->status>=3 )
this->status=0;
}
break;
case GOUP://向上:
{
this->orientation=2;
if(this->rolly==3)
{
Ry+=this->speed;
if(Ry>=32)
{
Ry=0;
SY--;
}
}
this->y-=this->speed;
this->status++;
if( this->status>=3 )
this->status=0;
}
break;
case GORIGHT: //向右:
{
this->orientation=3;
if(this->rollx==3)
{
Rx-=this->speed;
if(Rx<=-32)
{
Rx=0;
SX++;
}
}
this->x+=this->speed;
this->status++;
if( this->status>=3 )
this->status=0;
}
break;
}
// SetRxy(); //调整屏幕显示范围。必须在这个地方,否则会引起作图错误。
//因为必须人物先走动再调整屏幕。(现在不必了)
}
void MyPlayer::DrawPlayer(LPDIRECTDRAWSURFACE7 pBBuf,LPDIRECTDRAWSURFACE7 pPlayer)
{
// SetRxy(); //调整屏幕显示范围。在这个地方会引起作图错误。
//Sleep(50); //控速。
switch (this->orientation)
{
case 0:
{
// CheckBord();
RECT rect={this->status*32, this->orientation*48, this->status*32+32, this->orientation*48+48};
Blt(pBBuf, this->x-SX*32+Rx, this->y-SY*32+Ry, pPlayer, rect, true);
}
break;
case 1:
{
// CheckBord();
RECT rect={this->status*32, this->orientation*48, this->status*32+32, this->orientation*48+48};
Blt(pBBuf, this->x-SX*32+Rx, this->y-SY*32+Ry, pPlayer, rect,true);
}
break;
case 2:
{
// CheckBord();
RECT rect={this->status*32,this->orientation*48,this->status*32+32,this->orientation*48+48};
Blt(pBBuf, this->x-SX*32+Rx, this->y-SY*32+Ry, pPlayer, rect, true);
}
break;
case 3:
{
// CheckBord();
RECT rect={this->status*32, this->orientation*48, this->status*32+32, this->orientation*48+48};
Blt(pBBuf, this->x-SX*32+Rx, this->y-SY*32+Ry, pPlayer, rect, true);
}
break;
}
}
void MyPlayer::KeepOldxy()
{
this->x=this->old_x;
this->y=this->old_y;
}
bool MyPlayer::AffirmGo()
{
this->old_x=this->x;
this->old_y=this->y;
return true;
}
bool MyPlayer::AffirmOri()
{
this->oldorient=this->orientation;
return true;
}
long MyPlayer::GetMoney()
{
return this->money;
}
int MyPlayer::GetExp()
{
return this->exp;
}
void MyPlayer::ChangeMoney(long m)
{
this->money+=m;
}
void MyPlayer::ChangeExp(int e)
{
this->exp+=e;
}
void MyPlayer::LevelUp()
{
this->level+=1;
}
bool MyPlayer::BuyTools(MyTool *tool,int playerid)
{
if(tool->BeBought(this->money,playerid)==true && this->toolindex<=8)//每人只能有8个物品
{
this->tools.insert(tool,this->toolindex);//插入物品
this->toolindex++;
this->ChangeMoney(-1*tool->GetPrice());
return true;
}
else
return false;
}
bool MyPlayer::UseTool(int index)
{
if(this->tools.listlength()<1)
return false;
else if(index>this->tools.listlength())
return false;
else
{
MyTool *tool=new MyTool;
this->tools.getelem(index,tool);
bool result=tool->ToUse();
//tool=NULL;
if(result==true)
{
this->tools.delelem(index);
this->toolindex-=1;//索引减1
}
//delete tool;
return result;
}
}
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?