第八周项目四 游戏中的角色类增强版
2016-04-21 09:01
176 查看
/*
* Copyright(c)2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:游戏中的角色类增强版(2).Cpp
* 作者:刘默涵
* 完成日期:2016年4月21日
* 版本号:v1.0
*
* 问题描述:在上面已经建好的项目基础上,将Weapon类的数据成员改为数组,以支持一个角色可以带多件武器的需求,为此,在原类基础上,改造及增加的数据成员和成员函数至少包括:
* 输入描述:
* 程序输出:
*/
//1.game.h:类声明
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <string>
using namespace std;
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(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); //构造函数
~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 show(); //显示
void changeWeapon(int wno); //换手中的武器
private:
string name; //角色名称
int blood; //当前血量
bool life; //是否活着
Point location; //位置
Weapon weapon; //武器
Weapon weapons
; //武器,N为定义的常变量
int weaponNum; //武器数目
int holdWeapon; //现在手持哪一件武器(用武器数组的下标做标识)。
//0 ≤ holdWeapon < weaponNum
//初始时为空手,空手时holdWeapon值为-1,将-1定义为常变量为宜)
};
#endif // GAME_H_INCLUDED
//2.point.cpp,定义点类,表示位置
#include <iostream>
#include "game.h"
using namespace std;
int main( )
{
Weapon w1[1]= {Weapon("Gold stick",160, 80)}; //黑色切割者
Weapon w2[3]= {Weapon("Fire-Tip Lance",160,280), //无尽之刃
Weapon("Universal Ring",100,400), //血色幕刃
Weapon("Sky Muddling Damask",50,970) //幻影之舞
};
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;
}
.
* Copyright(c)2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:游戏中的角色类增强版(2).Cpp
* 作者:刘默涵
* 完成日期:2016年4月21日
* 版本号:v1.0
*
* 问题描述:在上面已经建好的项目基础上,将Weapon类的数据成员改为数组,以支持一个角色可以带多件武器的需求,为此,在原类基础上,改造及增加的数据成员和成员函数至少包括:
* 输入描述:
* 程序输出:
*/
//1.game.h:类声明
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <string>
using namespace std;
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(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); //构造函数
~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 show(); //显示
void changeWeapon(int wno); //换手中的武器
private:
string name; //角色名称
int blood; //当前血量
bool life; //是否活着
Point location; //位置
Weapon weapon; //武器
Weapon weapons
; //武器,N为定义的常变量
int weaponNum; //武器数目
int holdWeapon; //现在手持哪一件武器(用武器数组的下标做标识)。
//0 ≤ holdWeapon < weaponNum
//初始时为空手,空手时holdWeapon值为-1,将-1定义为常变量为宜)
};
#endif // GAME_H_INCLUDED
//2.point.cpp,定义点类,表示位置
#include <iostream>
#include "game.h"
using namespace std;
int main( )
{
Weapon w1[1]= {Weapon("Gold stick",160, 80)}; //黑色切割者
Weapon w2[3]= {Weapon("Fire-Tip Lance",160,280), //无尽之刃
Weapon("Universal Ring",100,400), //血色幕刃
Weapon("Sky Muddling Damask",50,970) //幻影之舞
};
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;
}
.
相关文章推荐
- 什么是java本地方法(Native Method)
- 贝叶斯学习--极大后验概率假设和极大似然假设
- 俄罗斯方块游戏JavaScript代码
- win7下装完ubuntu linux后,开机画面怎直接进入linux了,win7怎么启动
- 管道 PIPE
- JS、jQuery中select的用法详解
- 为什么你10年经验依旧平庸,他人却2年成就卓越?( 转载过来的)
- spring框架学习(一)
- IE11 F12工具报错 An error has occurredJSPlugin.3005
- 快速排序--Java
- char数组的scanf与strlen
- Unity IOC注入详细配置(MVC,WebApi)
- 初识函数:输入两个数,再输入一个运算符,得到结果,用函数封装
- MFC添加鼠标移动到控件上时的提示信息
- 正则表达式
- 第四周项目:小学生算术能力测试系统
- J-LINK连产品出现连接不上的问题
- CSS笔记(语法)
- c++第四次实验——分数的累加
- C++实验4-输出星号图