创意 C++ 文本冒险战斗游戏代码
这款游戏融合了回合制战斗与剧情分支选择,通过 C++ 面向对象特性实现角色与敌人的交互。玩家将扮演冒险者在迷雾森林中遭遇神秘敌人,不同选择会触发差异化战斗体验,代码中嵌入了随机事件系统增强可玩性。
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
// 物品类:用于战斗中的道具系统
class Item {
private:
string name;
int effect; // 正数为治疗,负数为额外伤害
public:
Item(string n, int e) : name(n), effect(e) {}
string getName() { return name; }
int getEffect() { return effect; }
};
// 角色基类
class Character {
protected:
string name;
int hp;
int attack;
int defense;
public:
Character(string n, int h, int a, int d) : name(n), hp(h), attack(a), defense(d) {}
virtual void takeDamage(int damage) {
int actualDamage = max(1, damage - defense); // 至少承受1点伤害
hp = max(0, hp - actualDamage);
cout << name << "受到" << actualDamage << "点伤害!" << endl;
}
int getHP() { return hp; }
string getName() { return name; }
int getAttack() { return attack; }
bool isAlive() { return hp > 0; }
};
// 玩家类(继承角色类)
class Player : public Character {
private:
vector<Item> inventory;
public:
Player(string n) : Character(n, 100, 15, 5) {
// 初始物品
inventory.push_back(Item("治疗药剂", 30));
inventory.push_back(Item("火焰卷轴", -20));
}
void showInventory() {
cout << "\n【物品栏】" << endl;
for (int i = 0; i < inventory.size(); i++) {
cout << i+1 << "." << inventory[i].getName()
<< (inventory[i].getEffect() > 0 ? "(治疗" : "(造成")
<< abs(inventory[i].getEffect()) << (inventory[i].getEffect() > 0 ? "点HP)" : "点伤害)") << endl;
}
}
void useItem(int index, Character& target) {
if (index < 1 || index > inventory.size()) {
cout << "无效的选择!" << endl;
return;
}
Item item = inventory[index-1];
if (item.getEffect() > 0) {
hp = min(100, hp + item.getEffect()); // 生命值上限100
cout << name << "使用了" << item.getName() << ",恢复了" << item.getEffect() << "点HP!" << endl;
} else {
target.takeDamage(abs(item.getEffect()));
cout << name << "使用了" << item.getName() << ",对敌人造成" << abs(item.getEffect()) << "点伤害!" << endl;
}
inventory.erase(inventory.begin() + index - 1); // 使用后移除物品
}
};
// 敌人类(继承角色类)
class Enemy : public Character {
private:
string type;
public:
Enemy(string t, int h, int a, int d) : Character(t, h, a, d), type(t) {}
void specialAttack(Player& player) {
int specialDamage = attack * 1.5;
player.takeDamage(specialDamage);
cout << "⚠️ " << name << "发动了特殊攻击!" << endl;
}
};
// 场景管理器
class SceneManager {
public:
static void startGame(Player& player) {
cout << "\n=== 迷雾森林冒险 ===" << endl;
cout << "你踏入了弥漫着魔法气息的森林,远处传来奇怪的嘶吼声..." << endl;
choosePath(player);
}
static void choosePath(Player& player) {
int choice;
cout << "\n请选择前进方向:1. 左侧幽暗小径 2. 右侧发光溪流" << endl;
cin >> choice;
if (choice == 1) {
encounterEnemy(player, Enemy("暗影狼", 40, 12, 3));
} else {
bool foundTreasure = (rand() % 2 == 0); // 50%概率发现宝箱
if (foundTreasure) {
cout << "你在溪边发现了宝箱,获得了【强效治疗药剂】!" << endl;
player.useItem(1, Enemy("dummy", 0, 0, 0)); // 临时物品处理
}
encounterEnemy(player, Enemy("水元素", 50, 10, 5));
}
}
static void encounterEnemy(Player& player, Enemy enemy) {
cout << "\n—— 遭遇了" << enemy.getName() << "!——" << endl;
battle(player, enemy);
}
static void battle(Player& player, Enemy enemy) {
while (player.isAlive() && enemy.isAlive()) {
cout << "\n【你的状态】HP: " << player.getHP() << " 【敌人状态】HP: " << enemy.getHP() << endl;
cout << "请选择行动:1. 普通攻击 2. 使用物品 3. 尝试逃跑" << endl;
int action;
cin >> action;
// 玩家行动
if (action == 1) {
enemy.takeDamage(player.getAttack());
} else if (action == 2) {
if (player.getHP() < 30) {
cout << "(提示:你的生命值偏低,建议使用治疗物品)" << endl;
}
player.showInventory();
int itemChoice;
cout << "请选择要使用的物品(输入序号):";
cin >> itemChoice;
player.useItem(itemChoice, enemy);
} else if (action == 3) {
if (rand() % 3 == 0) { // 33%概率逃跑成功
cout << "你成功逃离了战斗!" << endl;
choosePath(player);
return;
} else {
cout << "逃跑失败!敌人抓住了反击的机会!" << endl;
}
}
// 敌人行动
if (enemy.isAlive()) {
if (enemy.getHP() < 20 && rand() % 2 == 0) { // 低血量时50%概率使用特殊攻击
enemy.specialAttack(player);
} else {
player.takeDamage(enemy.getAttack());
cout << enemy.getName() << "对你发起了攻击!" << endl;
}
}
}
// 战斗结果判定
if (player.isAlive() && !enemy.isAlive()) {
cout << "\n🎉 你击败了" << enemy.getName() << "!" << endl;
cout << "森林深处传来更强烈的能量波动,似乎有更大的挑战在等待..." << endl;
// 可以扩展后续场景
} else if (!player.isAlive()) {
cout << "\n💀 冒险结束了...你被" << enemy.getName() << "击败了" << endl;
cout << "是否重新开始?1. 是 2. 否" << endl;
int restart;
cin >> restart;
if (restart == 1) {
Player newPlayer(player.getName());
startGame(newPlayer);
}
}
}
};
int main() {
srand(time(0)); // 初始化随机数种子
string playerName;
cout << "请输入你的名字:";
cin >> playerName;
Player player(playerName);
SceneManager::startGame(player);
return 0;
}
代码创意亮点解析
- 动态剧情系统:通过路径选择触发不同敌人遭遇和随机事件,50% 概率获得隐藏道具,增强游戏重玩价值。
- 战术战斗设计:敌人拥有特殊攻击机制,玩家可通过物品系统实现治疗与爆发伤害的策略选择,低血量时会触发战斗提示。
- 面向对象架构:使用类继承实现角色系统,Enemy 类与 Player 类共享基础属性但拥有差异化能力,便于扩展更多角色类型。
- 沉浸式体验:通过场景描述和视觉符号(⚠️、🎉)增强文本表现力,战斗状态实时更新让玩家直观掌握局势。
可进一步扩展的方向:添加技能树系统、多结局剧情分支、存档功能,或结合 SFML 库实现简单图形界面。编译运行后,你将获得一个充满变数的文本冒险体验!