第八周项目四(2)-游戏中角色类的增强版
2016-05-22 23:00
281 查看
问题及代码:
main.cpp
game.h
role.cpp
weapon.cpp
point.cpp
main.cpp
/* *烟台大学计算机学院 *文件名称:xiangmu1(2).cpp *作 者:闫安 *完成日期:2016年5月5日 *版 本 号:codeblocks 16.01 * *问题描述:在(1)的基础上,将Weapon类的数据成员改为数组,以支持一个角色可以带多件武器的需求。 *输入描述:无 *程序输出:任务的各种状态 */ #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; }
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 ; //武器 int weaponNum; //武器数目 int holdWeapon; //现在手持哪一件武器(空手为NOWEAPON,初始时空手) }; #endif // GAME_H_INCLUDED
role.cpp
#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 ; //武器 int weaponNum; //武器数目 int holdWeapon; //现在手持哪一件武器(空手为NOWEAPON,初始时空手) }; #endif // GAME_H_INCLUDED
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; }
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)); }
相关文章推荐
- Linux学习之位置参数变量
- 通用单链表C实现
- 【转】MSMQ消息队列安装
- 数据库SQL优化大总结之 百万级数据库优化方案
- Window Server 2008 R2 在Active Directory域中不能更改服务器密码策略
- 单例模式中的线程安全(延迟加载)
- 测试内存屏障存在
- 数组
- X264码率控制总结1——ABR,CQP,CRF
- #版本管理工具使用总结(git,svn,hg)
- CODEFORCES Educational Round 12 E. Beautiful Subarrays
- Python学习笔记(一)三步走安装pip
- Java实现将一个文件进行压缩
- 希尔排序
- spring集成Ehcache
- c语言学习笔记13之二维数组1
- 协查技巧总结
- c++第6次作业
- recyclerview使用详解
- 推荐8个实现 SVG 动画的 JavaScript 库