使用C++写一个RPG类游戏,有Role类,Hero类和Monster类分别公有继承Role类,Role类里的属性有名字、攻击力、防御力、血量、最大血量、暴击率、闪避率、掉落率,Hero类的私有成员有级别,Monster类的私有成员有经验,使用封装、继承、多态,写成回合制游戏
时间: 2023-10-20 15:09:06 浏览: 42
好的,我来给您写一个简单的代码参考:
```c++
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 角色类
class Role {
protected:
string name; // 名字
int attack; // 攻击力
int defense; // 防御力
int hp; // 当前血量
int maxHp; // 最大血量
double criticalRate; // 暴击率
double dodgeRate; // 闪避率
double dropRate; // 掉落率
public:
Role(string n, int a, int d, int h, int mh, double c, double dg, double dr)
: name(n), attack(a), defense(d), hp(h), maxHp(mh), criticalRate(c), dodgeRate(dg), dropRate(dr) {}
virtual void attackRole(Role& role) = 0; // 攻击
bool isDead() { return hp <= 0; } // 是否死亡
void showInfo() { // 显示信息
cout << "名字:" << name << endl;
cout << "攻击力:" << attack << endl;
cout << "防御力:" << defense << endl;
cout << "当前血量:" << hp << endl;
cout << "最大血量:" << maxHp << endl;
cout << "暴击率:" << criticalRate << endl;
cout << "闪避率:" << dodgeRate << endl;
cout << "掉落率:" << dropRate << endl;
}
};
// 英雄类
class Hero : public Role {
private:
int level; // 级别
public:
Hero(string n, int a, int d, int h, int mh, double c, double dg, double dr, int l)
: Role(n, a, d, h, mh, c, dg, dr), level(l) {}
// 英雄攻击
void attackRole(Role& role) {
srand(time(0)); // 以时间为种子,生成随机数
int damage = attack - role.defense;
if (damage < 0) damage = 0; // 最小伤害为0
if (rand() / double(RAND_MAX) < criticalRate) { // 暴击
damage *= 2;
cout << "暴击!";
}
if (rand() / double(RAND_MAX) < role.dodgeRate) { // 闪避
cout << "攻击被闪避!" << endl;
return;
}
role.hp -= damage;
cout << name << "对" << role.name << "造成了" << damage << "点伤害。" << endl;
if (role.isDead()) cout << role.name << "已死亡!" << endl;
}
// 显示英雄信息
void showInfo() {
cout << "英雄信息:" << endl;
Role::showInfo();
cout << "级别:" << level << endl;
}
};
// 怪物类
class Monster : public Role {
private:
int experience; // 经验
public:
Monster(string n, int a, int d, int h, int mh, double c, double dg, double dr, int e)
: Role(n, a, d, h, mh, c, dg, dr), experience(e) {}
// 怪物攻击
void attackRole(Role& role) {
srand(time(0)); // 以时间为种子,生成随机数
int damage = attack - role.defense;
if (damage < 0) damage = 0; // 最小伤害为0
if (rand() / double(RAND_MAX) < criticalRate) { // 暴击
damage *= 2;
cout << "暴击!";
}
if (rand() / double(RAND_MAX) < role.dodgeRate) { // 闪避
cout << "攻击被闪避!" << endl;
return;
}
role.hp -= damage;
cout << name << "对" << role.name << "造成了" << damage << "点伤害。" << endl;
if (role.isDead()) cout << role.name << "已死亡!" << endl;
}
// 显示怪物信息
void showInfo() {
cout << "怪物信息:" << endl;
Role::showInfo();
cout << "经验:" << experience << endl;
}
};
// 战斗函数
void fight(Role& hero, Role& monster) {
cout << "战斗开始:" << endl;
hero.showInfo(); // 显示英雄信息
cout << endl;
monster.showInfo(); // 显示怪物信息
cout << endl;
while (!hero.isDead() && !monster.isDead()) { // 双方未死亡
cout << hero.name << "攻击" << monster.name << ":" << endl;
hero.attackRole(monster);
cout << endl;
if (monster.isDead()) break; // 怪物死亡
cout << monster.name << "攻击" << hero.name << ":" << endl;
monster.attackRole(hero);
cout << endl;
}
if (hero.isDead()) cout << "英雄已死亡!" << endl;
if (monster.isDead()) cout << "怪物已死亡!" << endl;
}
int main() {
Hero hero("英雄", 10, 5, 100, 100, 0.1, 0.1, 0.2, 1); // 创建英雄
Monster monster("怪物", 5, 2, 50, 50, 0.05, 0.05, 0.1, 10); // 创建怪物
fight(hero, monster); // 开始战斗
return 0;
}
```
这个代码实现了一个简单的战斗,您可以根据需要进行修改和完善。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![image/jpeg](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)