很多其它见:C++游戏系列文件夹

知识点:对象数组作为数据成员

改进:每一个角色所持有的武器不仅仅一件,故持有的武器,用了对象数组来表示,当然,也能够是空手。

由此而带来的,还得记录一共同拥有几件武器,当前手持哪种武器。

【项目-角色有多样武器】

1.game.h:类声明

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <string>
using namespace std;
const int N=10; //每一个角色最多拥有的武器
const int NOWEAPON=-1; //表示手中无武器 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(); //返回杀伤距离
private:
string wname; //名称
int force; //杀伤力
double killRange; //杀伤距离
}; class Role
{
public:
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(); //显示
private:
string name; //角色名称
int blood; //当前血量
bool life; //是否活着
Point location; //位置
Weapon weapons[N]; //武器
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;
}

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<<". He is in ("<<location.getX()<<", "<<location.getY()<<") and ";
if(isAlived())
cout<<"alived.";
else
cout<<"dead.";
cout<<endl;
}

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

#include <iostream>
#include "game.h"
using namespace std; int main( )
{
Weapon w1[1]= {Weapon("Gold stick",200, 100)}; //金箍棒
Weapon w2[3]= {Weapon("Fire-Tip Lance",180,300), //火尖枪
Weapon("Universal Ring",100,500), //乾坤圈
Weapon("Sky Muddling Damask",50,1000) //混天绫
};
Role wuKong("WuKong", 500, Point(0, 0), w1, 1);
Role neZha("NeZha", 210, Point(30,30), w2, 3);
wuKong.changeWeapon(0);
neZha.changeWeapon(0);
cout<<"---begin---"<<endl;
wuKong.show();
neZha.show();
cout<<"---1st round---"<<endl;
wuKong.attack(neZha);
wuKong.show();
neZha.show();
cout<<"---2nd round---"<<endl;
neZha.changeWeapon(2);
neZha.attack(wuKong);
wuKong.show();
neZha.show();
cout<<"---3rd round---"<<endl;
neZha.moveTo(100,100);
wuKong.attack(neZha);
wuKong.show();
neZha.show();
cout<<"---4th round---"<<endl;
neZha.attack(wuKong);
wuKong.show();
neZha.show();
cout<<"---then---"<<endl;
neZha.attack(wuKong);
neZha.attack(wuKong);
wuKong.attack(neZha);
wuKong.show();
neZha.show();
cout<<"---end---"<<endl;
return 0;
}

C++游戏系列5:不止有一件武器的更多相关文章

  1. C++游戏系列:文件夹

    C++游戏系列1:角色类 C++游戏系列2:给角色装备武器 C++游戏系列3:用多文件组织角色类 C++游戏系列4:杀伤距离有限制 C++游戏系列5:不止有一件武器 C++游戏系列6:自己动起来 C+ ...

  2. WPF编游戏系列 之四 用户控件

    原文:WPF编游戏系列 之四 用户控件        在上一篇<WPF编游戏系列 之三 物品清单>中,对物品清单进行了演示,其中反复用到了同一组控件(如下图),而且 颜昌钢也指出在3.2. ...

  3. WPF编游戏系列 之八 银行界面及金额校验

    原文:WPF编游戏系列 之八 银行界面及金额校验        在前面<WPF编游戏系列 之四 用户控件>一文中通过用户控件创建了"My Shop"中物品列表框.本篇继 ...

  4. WPF编游戏系列 之九 物品清单再优化

    原文:WPF编游戏系列 之九 物品清单再优化        在"第三篇"和"第四篇"中通过用户控件和数据绑定功能对物品清单进行一些优化减少了部分C#代码,但感觉 ...

  5. WPF编游戏系列 之五 数据绑定

    原文:WPF编游戏系列 之五 数据绑定        在上一篇通过用户控件将重复使用的控件封装为一个控件组,大大减少了C#代码数量,本篇继续对该控件组进行数据绑定,节省为每个控件赋值的工作.对于数据绑 ...

  6. 【用PS3手柄在安卓设备上玩游戏系列】连接手柄和设备

    背景 硬件要求1:PS3 手柄 + 手柄配套的USB线 硬件要求2:已经获得 ROOT 权限并且支持蓝牙的安卓设备 软件要求1:Sixaxis Compatibility Checker PS3 手柄 ...

  7. C++游戏系列2:角色装备武器

    很多其它见:C++游戏系列文件夹 知识点:类的组合,A类的数据成员.是B类的对象,或B类的对象.做A类的数据成员 [项目-带武器的游戏角色] 设计一个武器类,其数据成员至少要有武器名.威力,还能够加上 ...

  8. WPF编游戏系列 之七 动画效果(2)

    原文:WPF编游戏系列 之七 动画效果(2)        上一篇已经对关闭窗口图标进行了动画效果处理,本篇将对窗口界面的显示和关闭效果进行处理.由于所有的动画效果都是针对窗口界面的Canvas,所以 ...

  9. WPF编游戏系列 之六 动画效果(1)

    原文:WPF编游戏系列 之六 动画效果(1)        本篇主要针对界面进行动画效果处理.首先在打开或关闭界面时,使其产生动态效果而不是生硬的显示或消失(如下图).其次在鼠标放到关闭窗口图标上时, ...

随机推荐

  1. 查看mysql服务器连接

    查看服务器连接,排查连接过多,查看连接状态时特别有用! 命令: show full processlist 作用: 显示当前运行的线程以及状态,也可以根据该命令来查看服务器状态. Id: 连接Id.U ...

  2. appium+python自动化51-adb文件导入和导出(pull push)

    前言 用手机连电脑的时候,有时候需要把手机(模拟器)上的文件导出到电脑上,或者把电脑的图片导入手机里做测试用,我们可以用第三方的软件管理工具直接复制粘贴,也可以直接通过adb命令导入和导出. adb ...

  3. [转]Using the Microsoft Connector for Oracle by Attunity with SQL Server 2008 Integration Services

    本文转自:http://technet.microsoft.com/en-us/library/ee470675(v=sql.100).aspx SQL Server Technical Articl ...

  4. python wheel 包命名规则和 abi 兼容

    wheel 包的命名规定 wheel 包的命名格式为 {distribution}-{version}(-{build tag})?-{python tag}-{abi tag}-{platform ...

  5. distance field(占坑

    signed distance field https://kosmonautblog.wordpress.com/2017/05/09/signed-distance-field-rendering ...

  6. KMP字符串模式匹配详解(zz)

    刚看到位兄弟也贴了份KMP算法说明,但本人觉得说的不是很详细,当初我在看这个算法的时候也看的头晕昏昏的,我贴的这份也是网上找的.且听详细分解: KMP字符串模式匹配详解 来自CSDN     A_B_ ...

  7. 解析PHP中如何将数组变量写入文件

    在用PHP记录日志,或者是 Ajax 请求出错想要 debug 的时候.我们一般都会将信息写入到一个指定的文件当中.然后根据相应的信息来处理问题.比如笔者最喜欢在用 Ajax 取不到数据的时候,在PH ...

  8. Java实现文件MD5加密

    代码实现: import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.s ...

  9. 电子商务ICP经营许可证申请条件

    电子商务ICP经营许可证申请条件:注册资金100万的纯内资公司:服务器在本地:域名备案以公司名义备案:申请条件:1.公司营业执照副本复印件需清晰有效,并已完成该年度年检事宜,特别注意营业执照的有效期要 ...

  10. python fabric使用 http://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html

    fab -u username -p password  -H hostname -P -- cmd   或root@'hostname'  -H多个主机是引号用逗号隔开 -P异步