用C++写一个RPG游戏,用类和封装继承,里面的属性有攻击力、防御力、暴击率、闪避率、掉落率、血量,要用指针和构造函数、析构函数
时间: 2023-10-20 21:26:24 浏览: 133
好的,以下是一个简单的RPG游戏的类设计:
```cpp
#include <iostream>
#include <string>
#include <cstdlib> // 用于随机数生成
using namespace std;
class Role {
public:
Role(string name, int attack, int defense, double criticalRate, double dodgeRate, double dropRate, int maxHp) {
this->name = name;
this->attack = attack;
this->defense = defense;
this->criticalRate = criticalRate;
this->dodgeRate = dodgeRate;
this->dropRate = dropRate;
this->maxHp = maxHp;
this->hp = maxHp;
}
virtual ~Role() {}
string getName() { return name; }
int getAttack() { return attack; }
int getDefense() { return defense; }
double getCriticalRate() { return criticalRate; }
double getDodgeRate() { return dodgeRate; }
double getDropRate() { return dropRate; }
int getHp() { return hp; }
int getMaxHp() { return maxHp; }
bool isDead() { return hp <= 0; }
void attackRole(Role* target) {
if (rand() % 100 < criticalRate * 100) {
cout << name << "发动了暴击!" << endl;
target->takeDamage(attack * 2);
} else {
cout << name << "攻击了" << target->getName() << "!" << endl;
target->takeDamage(attack);
}
}
void takeDamage(int damage) {
int realDamage = damage - defense;
if (realDamage < 0) {
realDamage = 0;
}
if (rand() % 100 < dodgeRate * 100) {
cout << name << "闪避了攻击!" << endl;
} else {
hp -= realDamage;
if (hp < 0) {
hp = 0;
}
cout << name << "受到了" << realDamage << "点伤害!" << endl;
}
}
void addHp(int hp) {
this->hp += hp;
if (this->hp > maxHp) {
this->hp = maxHp;
}
cout << name << "恢复了" << hp << "点生命值!" << endl;
}
bool dropItem() {
return rand() % 100 < dropRate * 100;
}
private:
string name;
int attack;
int defense;
double criticalRate;
double dodgeRate;
double dropRate;
int maxHp;
int hp;
};
class Player : public Role {
public:
Player(string name, int attack, int defense, double criticalRate, double dodgeRate, double dropRate, int maxHp, int level)
: Role(name, attack, defense, criticalRate, dodgeRate, dropRate, maxHp) {
this->level = level;
}
void levelUp() {
level++;
attack += 5;
defense += 5;
maxHp += 10;
hp = maxHp;
cout << getName() << "升到了" << level << "级!" << endl;
}
private:
int level;
};
class Monster : public Role {
public:
Monster(string name, int attack, int defense, double criticalRate, double dodgeRate, double dropRate, int maxHp, int exp)
: Role(name, attack, defense, criticalRate, dodgeRate, dropRate, maxHp) {
this->exp = exp;
}
int getExp() { return exp; }
private:
int exp;
};
int main() {
// 创建玩家和怪物
Player* player = new Player("玩家", 20, 10, 0.1, 0.1, 0.1, 100, 1);
Monster* monster = new Monster("史莱姆", 10, 5, 0.05, 0.05, 0.3, 50, 10);
// 玩家攻击怪物直到怪物死亡
while (!monster->isDead()) {
player->attackRole(monster);
if (monster->isDead()) {
break;
}
monster->attackRole(player);
}
// 如果怪物死亡,掉落物品
if (monster->isDead()) {
if (monster->dropItem()) {
cout << monster->getName() << "掉落了一件物品!" << endl;
} else {
cout << monster->getName() << "没有掉落任何物品。" << endl;
}
// 增加经验值
player->addHp(10);
int exp = monster->getExp();
cout << player->getName() << "获得了" << exp << "点经验值!" << endl;
// 判断是否升级
while (true) {
int levelUpExp = player->getMaxHp() * 2;
if (exp >= levelUpExp) {
exp -= levelUpExp;
player->levelUp();
} else {
break;
}
}
} else {
cout << player->getName() << "被" << monster->getName() << "打败了。" << endl;
}
delete player;
delete monster;
return 0;
}
```
在这个RPG游戏中,有两个角色类:`Player`和`Monster`,它们都继承自`Role`类。`Role`类中包含了各种角色共有的属性和方法,如攻击力、防御力、暴击率、闪避率、掉落率、血量等。`Player`类中还有一个`level`属性,表示玩家的等级,以及一个`levelUp`方法,用于将玩家升级。`Monster`类中有一个`exp`属性,表示杀死这个怪物可以获得的经验值。
在`main`函数中,我们创建了一个玩家和一个怪物,然后进行了一系列交战操作,直到其中一个角色死亡。如果怪物死亡,我们会判断是否掉落物品、增加经验值、升级等操作。最后,我们需要手动释放动态内存,因为我们在堆上创建了`Player`和`Monster`对象。
阅读全文