您的位置:首页 > 编程语言 > C语言/C++

C++游戏系列6:自己动起来

2016-04-13 22:54 183 查看
更多见:C++游戏系列目录

知识点:(1)对象数组,自动生成了很多个“玩家”(角色类的对象),自动玩;(2)使用文件:武器要从文件中读,玩的过程,写入到了文件。

不足(待改进之处):(1)产生随机数时,没有避免重复,以致于会有自己攻击自己的情况——好在加了血又减去了,没有变化,还有同样的武器会有多件——都是棍,但带两条棍,也行;(2)只是让自动产生的角色玩起来了,自己没有参与,其实,指定对象数组中第0个对象就是自己(随机选取动作者时,随机数最小为1),加个菜单,选择动作,即可。

【项目-让玩家自己动起来】

(与前几个版本相比,主要变化在main.cpp中)

1.game.h:类声明

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <string>
using namespace std;
const int N=5; //每个角色最多拥有的武器
const int M=30;  //游戏人物的最大数
const int NOWEAPON=-1;  //表示手中无武器
const int SIZE=100;  //将从文件是读取武器信息,最多100种武器

class Point     //Point类声明
{
public: //外部接口
Point(int x=0, int y=0);
int getX();
int getY();
double distance(const Point &p);  //返回与另外一点p之间的距离
void moveTo(int x, int y); //移到另外一点
void move(int dx, int dy); //从当前位置移动
private:
int x, y;  //座标
};

class Weapon
{
public:
Weapon(){};
Weapon(string wnam, int f, double k);
Weapon(const Weapon&);
string getWname();
int getForce();         //返回杀伤力
double getKillRange();  //返回杀伤距离
void setWeapon(string,int,double);
private:
string wname;   //名称
int force;       //杀伤力
double killRange;   //杀伤距离
};

class Role
{
public:
Role():weaponNum(0),holdWeapon(NOWEAPON){};
Role(string nam, int b, Point l, Weapon w[], int n); //构造函数
~Role(); //析构函数
void eat(int d); //吃东西,涨d血(死了后吃上东西可以复活)
void attack(Role &r); //攻击别人,自己涨血,同时对方被攻击失血。血量取决于当前用的武器
void beAttack(int f); //被别人攻击,参数f是承受的攻击力
double distance(Role &r); //返回与另一角色的距离
bool isAlived(); //是否活着
void moveTo(int x, int y); //移到另外一点
void move(int dx, int dy); //从当前位置移动
void changeWeapon(int wno); //换手中的武器
void show(); //显示
void setBaseInfo(string, int);//角色名称和初始血量
void setLocation(int,int);//设置位置
void addWeapon(Weapon);//添一件武器
string getName();
int getWeaponNum();
string getCurWeapon();
private:
string name;  //角色名称
int blood;    //当前血量
bool life;    //是否活着
Point location;  //位置
Weapon weapons
;  //武器
int weaponNum;      //武器数目
int holdWeapon;     //现在手持哪一件武器(空手为NOWEAPON,初始时空手)
};

#endif // GAME_H_INCLUDED


2.point.cpp,定义点类,表示位置

#include "game.h"
#include <cmath>

Point::Point(int x, int y): x(x), y(y) { }
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
//移到另外一点
void Point::moveTo(int x, int y)
{
this->x=x;
this->y=y;
}
//从当前位置移动
void Point::move(int dx, int dy)
{
this->x+=dx;
this->y+=dy;
}
double Point::distance(const Point& p)
{
double dx = this->x - p.x;
double dy = this->y - p.y;
return (sqrt(dx * dx + dy * dy));
}


3.weapon.cpp,定义武器类

#include "game.h"
Weapon::Weapon(string wnam, int f, double k):wname(wnam),force(f),killRange(k) {}
Weapon::Weapon(const Weapon &w):wname(w.wname),force(w.force),killRange(w.killRange) {}
string Weapon::getWname()
{
return wname;
}
//返回杀伤力
int Weapon::getForce()
{
return force;
}
//返回杀伤距离
double Weapon::getKillRange()
{
return killRange;
}
void Weapon::setWeapon(string name,int f,double r)
{
wname=name;
force=f;
killRange=r;
}


4.role.cpp,定义角色类,表示参与游戏的角色

#include <iostream>
#include "game.h"
using namespace std;

Role::Role(string nam, int b, Point l, Weapon w[], int n)
:name(nam),blood(b),location(l),weaponNum(n),holdWeapon(NOWEAPON)
{
if(blood>0)
life=true;
else
life=false;
for(int i=0; i<n; i++)
weapons[i]=w[i];
}
Role::~Role()
{
cout<<name<<"退出江湖..."<<endl;
}

//吃东西,涨d血(死了后吃上东西可以复活)
void Role::eat(int d) //吃东西,涨d血(死了也能吃,别人喂的,以使能复活)
{
blood+=d;
if(blood>0)
life=true;
}

//攻击别人,自己涨血,同时对方被攻击失血,血量取决于当前用的武器
//在武器的攻击范围内才可以攻击
void Role::attack(Role &r)
{
if(isAlived()&&holdWeapon>NOWEAPON&&weapons[holdWeapon].getKillRange()>this->distance(r)) //活着且在杀伤范围内
{
blood+=weapons[holdWeapon].getForce();
r.beAttack(weapons[holdWeapon].getForce());
}
}

//被别人攻击,参数f是承受的攻击力
void Role::beAttack(int f)
{
blood-=f;
if(blood<=0)
life=false;
}

//返回与另一角色的距离
double Role::distance(Role &r)
{
return location.distance(r.location);
}
//换手中的武器
void Role::changeWeapon(int wno)
{
if(wno<weaponNum)
holdWeapon=wno;
}
//是否活着
bool Role::isAlived()
{
return life;
}
//移到另外一点
void Role::moveTo(int x, int y)
{
if(isAlived())  //死了就不能动了
location.moveTo(x,y);
}
//从当前位置移动
void Role::move(int dx, int dy)
{
if(isAlived())
location.move(dx,dy);
}
//显示
void Role::show()
{
cout<<name<<" has "<<blood<<" blood, hold ";
if(holdWeapon==NOWEAPON)
cout<<"no weapon";
else
cout<<weapons[holdWeapon].getWname();
cout<<"(";
for(int i=0; i<weaponNum; i++)
cout<<weapons[i].getWname()<<",";
cout<<"\b)";
cout<<". He is in ("<<location.getX()<<", "<<location.getY()<<") and ";
if(isAlived())
cout<<"alived.";
else
cout<<"dead.";
cout<<endl;
}
//角色名称和初始血量
void Role::setBaseInfo(string nam, int b)
{
name=nam;
blood=b;
if(blood>0)
life=true;
}
//设置位置
void Role::setLocation(int x,int y)
{
location.moveTo(x,y);
}
//添一件武器
void Role::addWeapon(Weapon w)
{
if(weaponNum<N)
{
weapons[weaponNum]=w;
weaponNum++;
}
}

//返回名称
string Role::getName()
{
return name;
}

int Role::getWeaponNum()
{
return weaponNum;
}
string Role::getCurWeapon()
{
return weapons[holdWeapon].getWname();
}


5.main.cpp,测试函数,表示位置

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include "game.h"
using namespace std;
void initializeRoles(Role roles[]); //初始化角色,由计算机随机产生
int readWeaponInfo(Weapon WeaponBase[]); //从文件中读取武器信息
int randBetween(int low, int high); //产生一定范围内的随机数
void play(Role roles[], int n);

//主函数
int main( )
{
srand(time(0));
Role roles[M];
initializeRoles(roles);
play(roles, 1000);
return 0;
}

//初始化角色
void initializeRoles(Role roles[])
{
Weapon weaponBase[SIZE]; //存储系统中可以用到的武器库数组
int weaponNum = readWeaponInfo(weaponBase); //从文件中读取武器信息到武器库数组,并返回武器种类数
char cno[5]; //人物序号
int wn; //要加的武器数
int wno; //要加入的武器的编号(weaponBase中的下标)
for(int i=0; i<M; i++) //产生M个角色对象,即游戏中的人物
{
itoa(i,cno,10);
roles[i].setBaseInfo(string("Soldier")+cno,randBetween(10, 100));
roles[i].setLocation(randBetween(0,1000),randBetween(0, 1000));
wn=randBetween(1,N);
for(int j=0; j<wn; j++) //添加wn件武器
{
wno=randBetween(0,weaponNum);
roles[i].addWeapon(weaponBase[wno]);
}
roles[i].changeWeapon(randBetween(0,wn));//当前持什么武器
}
}

//初始化武器库
int readWeaponInfo(Weapon WeaponBase[])
{
ifstream infile("weapon.txt",ios::in);
int n=0;
string wn;
int wf;
double wr; //分别代表武器名、杀伤力、杀伤范围
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
while(infile>>wn>>wf>>wr)
{
WeaponBase[n++].setWeapon(wn,wf,wr);
}
infile.close();
return n;
}
//产生一定大于等于low,小于high范围内的随机数
int randBetween(int low, int high)
{
return low+rand()%(high-low);
}

//玩n轮游戏,每一轮有一个角色行动
//行动可以是攻击、移动、换武器、吃
//所有的结果,保存到log.txt文件中
void play(Role roles[], int n)
{
int i;
int rno,rno2; //选中的游戏者
int action; //行动0-攻击,1-移动,2-换武器,3-吃东西
int newx, newy, newWeapon, eatd;
ofstream outfile("log.txt",ios::out);  //创建输出流对象
if(!outfile)                    //如果打开失败,outfile返回0值
{
cerr<<"open error!"<<endl;
exit(1);
}

cout<<"开始前...."<<endl;
for(i=0; i<M; i++)
roles[i].show();
cout<<"开始游戏,请到日志文件中看过程...."<<endl<<endl;
for(i=0; i<n; i++)
{
rno=randBetween(0,M);
outfile<<"第"<<i<<"轮: "<<roles[rno].getName();
action=randBetween(0,4);
switch(action)
{
case 0: //攻击
rno2=randBetween(0,M);
outfile<<"攻击"<<roles[rno2].getName();
roles[rno].attack(roles[rno2]);
break;
case 1: //移动
newx=randBetween(0,1000);
newy=randBetween(0, 1000);
outfile<<"移动到("<<newx<<","<<newy<<")";
roles[rno].moveTo(newx,newy);
break;
case 2: //换武器
newWeapon=randBetween(0,roles[rno].getWeaponNum());
roles[rno].changeWeapon(newWeapon);
outfile<<"将武器换为: "<<roles[rno].getCurWeapon();
break;
case 3: //吃
eatd=randBetween(0,100);
roles[rno].eat(eatd);
outfile<<"吃了: "<<eatd;
break;
}
outfile<<". "<<endl;
}
outfile<<endl;
outfile.close();
cout<<"游戏结束后...."<<endl;
for(i=0; i<M; i++)
roles[i].show();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: