#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 玩家类
class Player {
public:
string name;
int health;
int attack_power;
Player(string name) {
this->name = name;
this->health = 100;
this->attack_power = 10;
}
void take_damage(int damage) {
health -= damage;
cout << name << " 受到了 " << damage << " 点伤害。剩余生命值: " << health << endl;
}
void heal(int heal_points) {
health += heal_points;
cout << name << " 恢复了 " << heal_points << " 点生命值。当前生命值: " << health << endl;
}
bool is_alive() {
return health > 0;
}
};
// 敌人类
class Enemy {
public:
string name;
int health;
int attack_power;
Enemy(string name, int health, int attack_power) {
this->name = name;
this->health = health;
this->attack_power = attack_power;
}
void take_damage(int damage) {
health -= damage;
cout << name << " 受到了 " << damage << " 点伤害。剩余生命值: " << health << endl;
}
bool is_alive() {
return health > 0;
}
};
// 物品系统
void find_item(Player& player) {
cout << "你发现了一个神秘的盒子!里面有一个治疗药水。" << endl;
player.heal(20);
cout << player.name << " 使用了治疗药水,恢复了 20 点生命值。" << endl;
}
// 战斗过程
bool battle(Player& player, Enemy& enemy) {
cout << player.name << " 和 " << enemy.name << " 开始战斗!" << endl;
while (player.is_alive() && enemy.is_alive()) {
string action;
cout << "你想攻击还是防守?(请输入 攻击/防守): ";
cin >> action;
if (action == "攻击") {
enemy.take_damage(player.attack_power);
cout << player.name << " 攻击了 " << enemy.name << "!" << endl;
} else if (action == "防守") {
cout << player.name << " 选择了防守,恢复了 10 点生命。" << endl;
player.heal(10);
} else {
cout << "无效的选择,请重新选择。" << endl;
continue;
}
if (enemy.is_alive()) {
// 敌人反击
cout << enemy.name << " 反击!" << endl;
player.take_damage(enemy.attack_power);
}
}
if (player.is_alive()) {
cout << player.name << " 打败了 " << enemy.name << "!" << endl;
return true;
} else {
cout << player.name << " 被 " << enemy.name << " 打败了,游戏结束。" << endl;
return false;
}
}
void forest(Player& player) {
cout << "你进入了森林,四周一片寂静。" << endl;
cout << "突然,你听到一声咆哮,一个巨大的狼出现了!" << endl;
Enemy wolf("狼", 50, 15);
if (!battle(player, wolf)) {
return;
}
cout << "你继续走在森林中,突然发现了一个隐藏的宝箱。" << endl;
find_item(player);
}
void town(Player& player) {
cout << "你来到小镇,镇上有许多商店和居民。" << endl;
cout << "你听说镇上的老屋藏有一份秘密的宝藏。" << endl;
string choice;
cout << "你想去探索老屋还是去商店买装备?(请输入老屋/商店): ";
cin >> choice;
if (choice == "老屋") {
cout << "你进入老屋,发现了一个隐藏的地下室!" << endl;
cout << "地下室里有许多金币,你变得富有!" << endl;
cout << "你决定回家休息,过上了富足的生活。恭喜你!游戏结束。" << endl;
} else if (choice == "商店") {
cout << "你在商店里买了些装备,准备继续冒险。" << endl;
cout << "然而,镇上的居民突然被怪物袭击,你需要做出选择!" << endl;
Enemy monster("怪物", 80, 20);
if (!battle(player, monster)) {
return;
}
cout << "你成功保卫了小镇,成为了英雄!恭喜你!游戏结束。" << endl;
} else {
cout << "无效的选择,请输入‘老屋’或‘商店’。" << endl;
town(player);
}
}
// 游戏开始
void start_game() {
cout << "欢迎来到冒险游戏!" << endl;
string name;
cout << "请输入你的角色名字: ";
cin >> name;
Player player(name);
cout << "你站在一个分岔路口前。" << endl;
cout << "左边是黑暗的森林,右边是通往小镇的路。" << endl;
string choice;
cout << "你想走左边的森林还是右边的小镇?(请输入左/右): ";
cin >> choice;
if (choice == "左") {
forest(player);
} else if (choice == "右") {
town(player);
} else {
cout << "无效的选择,请输入‘左’或‘右’。" << endl;
start_game();
}
}
int main() {
srand(time(0)); // 用于生成随机数
start_game();
return 0;
}