写一个C++修仙小游戏
以下是一个简单的C++修仙小游戏的示例代码:
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class Player {
private:
string name;
int level;
int hp;
int mp;
public:
Player(string n) {
name = n;
level = 1;
hp = 100;
mp = 50;
}
void getInfo() {
cout << "姓名:" << name << endl;
cout << "等级:" << level << endl;
cout << "生命值:" << hp << endl;
cout << "魔法值:" << mp << endl;
}
void meditate() {
mp += 10;
cout << "你开始打坐修炼,恢复了10点魔法值。" << endl;
}
void battle() {
srand(time(nullptr));
int enemyLevel = rand() % level + 1;
int enemyHp = enemyLevel * 50;
int enemyDamage = enemyLevel * 10;
cout << "你遇到了一个等级为 " << enemyLevel << " 的敌人!" << endl;
while (true) {
cout << "选择行动:" << endl;
cout << "1. 攻击" << endl;
cout << "2. 逃跑" << endl;
int choice;
cin >> choice;
if (choice == 1) {
int playerDamage = level * 10;
enemyHp -= playerDamage;
cout << "你对敌人造成了 " << playerDamage << " 点伤害!" << endl;
if (enemyHp <= 0) {
cout << "你战胜了敌人!" << endl;
level++;
hp += level * 10;
mp += level * 5;
break;
}
hp -= enemyDamage;
cout << "敌人对你造成了 " << enemyDamage << " 点伤害!" << endl;
if (hp <= 0) {
cout << "你被敌人击败了!游戏结束。" << endl;
exit(0);
}
} else if (choice == 2) {
cout << "你选择逃跑,成功逃离战斗。" << endl;
break;
} else {
cout << "无效的选择,请重新输入。" << endl;
}
}
}
};
int main() {
cout << "欢迎来到修仙游戏!" << endl;
cout << "请输入你的姓名:";
string name;
cin >> name;
Player player(name);
while (true) {
player.getInfo();
cout << "选择行动:" << endl;
cout << "1. 打坐修炼" << endl;
cout << "2. 寻找敌人战斗" << endl;
cout << "3. 退出游戏" << endl;
int choice;
cin >> choice;
if (choice == 1) {
player.meditate();
} else if (choice == 2) {
player.battle();
} else if (choice == 3) {
cout << "游戏结束。再见!" << endl;
break;
} else {
cout << "无效的选择,请重新输入。" << endl;
}
}
return 0;
}
该小游戏中,玩家可以打坐修炼来恢复魔法值,也可以寻找敌人战斗来提升等级。玩家和敌人的属性通过等级来确定,每次战斗后玩家会根据战斗结果获得经验和奖励。玩家可以选择退出游戏
原文地址: http://www.cveoy.top/t/topic/h70l 著作权归作者所有。请勿转载和采集!