C++1.基于面向对象思想,分二级建立王者荣耀游戏中的英雄类(抽象的英雄、特定类型的英雄)。 2.在类中对英雄的属性(比如英雄的种类、魔法值、生命值、物理攻击值)及行为(比如魔法攻击、物理攻击)进行具体描述。 3.
时间: 2024-03-14 14:47:21 浏览: 193
好的,我可以为您提供一个示例代码,实现这三个需求。请参考下面的代码:
```c++
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 英雄类的抽象基类
class Hero {
public:
Hero(string name, int max_hp, int max_mp, int physical_attack, int magical_attack) {
name_ = name;
max_hp_ = max_hp;
max_mp_ = max_mp;
physical_attack_ = physical_attack;
magical_attack_ = magical_attack;
hp_ = max_hp;
mp_ = max_mp;
}
string GetName() const {
return name_;
}
int GetMaxHP() const {
return max_hp_;
}
int GetMaxMP() const {
return max_mp_;
}
int GetHP() const {
return hp_;
}
int GetMP() const {
return mp_;
}
int GetPhysicalAttack() const {
return physical_attack_;
}
int GetMagicalAttack() const {
return magical_attack_;
}
virtual void PhysicalAttack(Hero& target) = 0;
virtual void MagicalAttack(Hero& target) = 0;
virtual void BePhysicalAttacked(int damage) = 0;
virtual void BeMagicalAttacked(int damage) = 0;
protected:
string name_;
int max_hp_;
int max_mp_;
int hp_;
int mp_;
int physical_attack_;
int magical_attack_;
};
// 物理攻击英雄类
class PhysicalHero : public Hero {
public:
PhysicalHero(string name, int max_hp, int max_mp, int physical_attack, int magical_attack)
: Hero(name, max_hp, max_mp, physical_attack, magical_attack) {}
void PhysicalAttack(Hero& target) {
int damage = physical_attack_;
target.BePhysicalAttacked(damage);
cout << name_ << "攻击了" << target.GetName() << ",造成了" << damage << "点物理伤害!" << endl;
}
void MagicalAttack(Hero& target) {
cout << name_ << "不会魔法攻击!" << endl;
}
void BePhysicalAttacked(int damage) {
hp_ -= damage;
if (hp_ < 0) {
hp_ = 0;
}
cout << name_ << "受到了" << damage << "点物理伤害,剩余血量:" << hp_ << endl;
}
void BeMagicalAttacked(int damage) {
cout << name_ << "受到了" << damage << "点魔法伤害,但是没有受到伤害!" << endl;
}
};
// 魔法攻击英雄类
class MagicalHero : public Hero {
public:
MagicalHero(string name, int max_hp, int max_mp, int physical_attack, int magical_attack)
: Hero(name, max_hp, max_mp, physical_attack, magical_attack) {}
void PhysicalAttack(Hero& target) {
cout << name_ << "不会物理攻击!" << endl;
}
void MagicalAttack(Hero& target) {
int damage = magical_attack_;
target.BeMagicalAttacked(damage);
cout << name_ << "释放了魔法攻击,攻击了" << target.GetName() << ",造成了" << damage << "点魔法伤害!" << endl;
}
void BePhysicalAttacked(int damage) {
cout << name_ << "受到了" << damage << "点物理伤害,但是没有受到伤害!" << endl;
}
void BeMagicalAttacked(int damage) {
hp_ -= damage;
if (hp_ < 0) {
hp_ = 0;
}
cout << name_ << "受到了" << damage << "点魔法伤害,剩余血量:" << hp_ << endl;
}
};
// 随机生成一个英雄
Hero* RandomHero() {
int type = rand() % 2; // 随机生成英雄类型
string name;
int max_hp, max_mp, physical_attack, magical_attack;
switch (type) {
case 0:
name = "物理英雄";
max_hp = 200;
max_mp = 0;
physical_attack = 50;
magical_attack = 0;
return new PhysicalHero(name, max_hp, max_mp, physical_attack, magical_attack);
case 1:
name = "魔法英雄";
max_hp = 150;
max_mp = 100;
physical_attack = 0;
magical_attack = 80;
return new MagicalHero(name, max_hp, max_mp, physical_attack, magical_attack);
}
}
// PK规则
void PK(Hero* hero1, Hero* hero2) {
int round = 1;
cout << "PK开始!" << endl;
while (hero1->GetHP() > 0 && hero2->GetHP() > 0) {
cout << "第" << round << "回合:" << endl;
if (rand() % 2 == 0) {
hero1->PhysicalAttack(*hero2);
} else {
hero1->MagicalAttack(*hero2);
}
if (hero2->GetHP() <= 0) {
cout << hero2->GetName() << "被打败了!" << endl;
break;
}
if (rand() % 2 == 0) {
hero2->PhysicalAttack(*hero1);
} else {
hero2->MagicalAttack(*hero1);
}
if (hero1->GetHP() <= 0) {
cout << hero1->GetName() << "被打败了!" << endl;
break;
}
round++;
}
}
int main() {
srand(time(NULL));
Hero* hero1 = RandomHero();
Hero* hero2 = RandomHero();
cout << "英雄1:" << hero1->GetName() << ",物理攻击:" << hero1->GetPhysicalAttack() << ",魔法攻击:"
<< hero1->GetMagicalAttack() << ",最大血量:" << hero1->GetMaxHP() << endl;
cout << "英雄2:" << hero2->GetName() << ",物理攻击:" << hero2->GetPhysicalAttack() << ",魔法攻击:"
<< hero2->GetMagicalAttack() << ",最大血量:" << hero2->GetMaxHP() << endl;
PK(hero1, hero2);
delete hero1;
delete hero2;
return 0;
}
```
上面的代码中,我们定义了一个Hero类作为所有英雄类的抽象基类,这个类包含了英雄的基本属性和行为。然后我们又通过继承的方式,分别定义了PhysicalHero和MagicalHero两个特定类型的英雄类,这两个类分别对物理攻击和魔法攻击进行了具体描述。在PK函数中,我们随机生成两个英雄,并让它们进行PK,直到有一方的血量降为0。在PK过程中,我们还输出了每回合的攻击结果和双方的状态变化。
当然,这只是一个示例代码,您可以根据自己的需求进行修改和扩展。
阅读全文