使用C++写一个回合制卡牌游戏,有一个简单的界面,要使用封装、继承和多态,有基本的Role类,派生出Hero类和Monster类,要有名字、攻击力、防御力、血量、最大血量、暴击率、闪避率、掉落率
时间: 2023-10-20 19:24:43 浏览: 116
好的,我来为您提供一个简单的回合制卡牌游戏的代码示例,满足您的要求。
```c++
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 基类 Role
class Role {
public:
Role(string name, int attack, int defense, int hp, int maxHp, float critRate, float dodgeRate, float dropRate) :
m_name(name), m_attack(attack), m_defense(defense), m_hp(hp), m_maxHp(maxHp),
m_critRate(critRate), m_dodgeRate(dodgeRate), m_dropRate(dropRate) {}
// 获取名字
string getName() const {
return m_name;
}
// 获取攻击力
int getAttack() const {
return m_attack;
}
// 获取防御力
int getDefense() const {
return m_defense;
}
// 获取血量
int getHp() const {
return m_hp;
}
// 获取最大血量
int getMaxHp() const {
return m_maxHp;
}
// 获取暴击率
float getCritRate() const {
return m_critRate;
}
// 获取闪避率
float getDodgeRate() const {
return m_dodgeRate;
}
// 获取掉落率
float getDropRate() const {
return m_dropRate;
}
// 攻击对手
virtual void attack(Role* opponent) {}
// 显示状态
virtual void showStatus() {}
protected:
string m_name; // 名字
int m_attack; // 攻击力
int m_defense; // 防御力
int m_hp; // 血量
int m_maxHp; // 最大血量
float m_critRate; // 暴击率
float m_dodgeRate; // 闪避率
float m_dropRate; // 掉落率
};
// 派生类 Hero
class Hero : public Role {
public:
Hero(string name, int attack, int defense, int hp, int maxHp, float critRate, float dodgeRate, float dropRate) :
Role(name, attack, defense, hp, maxHp, critRate, dodgeRate, dropRate) {}
// 攻击对手
virtual void attack(Role* opponent) {
cout << "英雄 " << m_name << " 攻击 " << opponent->getName() << endl;
if (rand() % 100 < m_critRate * 100) { // 判断是否暴击
int damage = m_attack * 2 - opponent->getDefense();
if (damage < 0) {
damage = 0;
}
opponent->takeDamage(damage);
cout << "暴击!造成 " << damage << " 点伤害" << endl;
}
else if (rand() % 100 < opponent->getDodgeRate() * 100) { // 判断是否闪避
cout << opponent->getName() << " 闪避了攻击" << endl;
}
else {
int damage = m_attack - opponent->getDefense();
if (damage < 0) {
damage = 0;
}
opponent->takeDamage(damage);
cout << "造成 " << damage << " 点伤害" << endl;
}
}
// 显示状态
virtual void showStatus() {
cout << "英雄 " << m_name << ":" << endl;
cout << "攻击力:" << m_attack << endl;
cout << "防御力:" << m_defense << endl;
cout << "血量:" << m_hp << "/" << m_maxHp << endl;
cout << "暴击率:" << m_critRate << endl;
cout << "闪避率:" << m_dodgeRate << endl;
cout << "掉落率:" << m_dropRate << endl;
}
protected:
// 扣除伤害
void takeDamage(int damage) {
m_hp -= damage;
if (m_hp < 0) {
m_hp = 0;
}
}
};
// 派生类 Monster
class Monster : public Role {
public:
Monster(string name, int attack, int defense, int hp, int maxHp, float critRate, float dodgeRate, float dropRate) :
Role(name, attack, defense, hp, maxHp, critRate, dodgeRate, dropRate) {}
// 攻击对手
virtual void attack(Role* opponent) {
cout << "怪物 " << m_name << " 攻击 " << opponent->getName() << endl;
if (rand() % 100 < m_critRate * 100) { // 判断是否暴击
int damage = m_attack * 2 - opponent->getDefense();
if (damage < 0) {
damage = 0;
}
opponent->takeDamage(damage);
cout << "暴击!造成 " << damage << " 点伤害" << endl;
}
else if (rand() % 100 < opponent->getDodgeRate() * 100) { // 判断是否闪避
cout << opponent->getName() << " 闪避了攻击" << endl;
}
else {
int damage = m_attack - opponent->getDefense();
if (damage < 0) {
damage = 0;
}
opponent->takeDamage(damage);
cout << "造成 " << damage << " 点伤害" << endl;
}
}
// 显示状态
virtual void showStatus() {
cout << "怪物 " << m_name << ":" << endl;
cout << "攻击力:" << m_attack << endl;
cout << "防御力:" << m_defense << endl;
cout << "血量:" << m_hp << "/" << m_maxHp << endl;
cout << "暴击率:" << m_critRate << endl;
cout << "闪避率:" << m_dodgeRate << endl;
cout << "掉落率:" << m_dropRate << endl;
}
protected:
// 扣除伤害
void takeDamage(int damage) {
m_hp -= damage;
if (m_hp < 0) {
m_hp = 0;
}
}
};
// 简单的界面实现
void showMenu() {
cout << "====================" << endl;
cout << "1. 开始游戏" << endl;
cout << "2. 退出游戏" << endl;
cout << "====================" << endl;
}
int main() {
srand(time(NULL)); // 随机数种子
Hero hero("小明", 20, 10, 100, 100, 0.1, 0.1, 0.5); // 创建英雄
Monster monster("小怪", 10, 5, 50, 50, 0.05, 0.05, 0.2); // 创建怪物
int choice;
do {
showMenu();
cout << "请选择:";
cin >> choice;
switch (choice) {
case 1:
while (hero.getHp() > 0 && monster.getHp() > 0) { // 战斗循环
hero.attack(&monster);
if (monster.getHp() > 0) { // 怪物还活着
monster.attack(&hero);
}
}
if (hero.getHp() > 0) { // 英雄胜利
cout << "战斗胜利!获得 " << monster.getDropRate() * 100 << "% 掉落" << endl;
}
else { // 怪物胜利
cout << "战斗失败!" << endl;
}
hero.showStatus(); // 显示英雄状态
monster.showStatus(); // 显示怪物状态
break;
case 2:
cout << "再见!" << endl;
break;
default:
cout << "输入错误,请重新选择!" << endl;
break;
}
} while (choice != 2);
return 0;
}
```
这个示例中,我们定义了一个基类 `Role`,并派生出了 `Hero` 和 `Monster` 两个类。它们都具有名字、攻击力、防御力、血量、最大血量、暴击率、闪避率和掉落率等属性,同时还实现了攻击对手和显示状态两个虚函数。这里我们使用了多态,使得在战斗中可以直接调用基类的攻击函数,而不必关心是英雄还是怪物。
在主函数中,我们创建了一个英雄和一个怪物,然后通过简单的界面实现,让玩家选择是否开始游戏。在游戏开始后,我们让英雄和怪物互相攻击,直到其中一方血量归零为止。战斗结束后,我们根据胜利方显示对应的信息,并输出英雄和怪物的状态。
阅读全文