地狱闯关游戏3.0
2026-02-15 10:28:28
发布于:浙江
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
#include <iomanip>
using namespace std;
const int WIDTH = 15;
const int HEIGHT = 15;
const int MAX_FLOORS = 5; // 最大层数
const int INVENTORY_SIZE = 20; // 背包容量
// 枚举地形类型
enum Terrain {
GRASS = '.', // 草地
TREE = '#', // 树木
WATER = '~', // 水
WALL = '|', // 墙
TREASURE = '$', // 宝藏
STAIRS_DOWN = '>', // 向下的楼梯
STAIRS_UP = '<' // 向上的楼梯
};
// 怪物类型
enum MonsterType { GOBLIN, ORC, DRAGON, SLIME, ZOMBIE, MAX_TYPES };
// 物品类型
enum ItemType {
POTION, // 药水
WEAPON, // 武器
ARMOR, // 防具
TREASURE_ITEM, // 宝藏
KEY // 钥匙
};
// 物品结构体
struct Item {
int id;
string name;
int healthBonus;
int attackBonus;
int defenseBonus;
int price;
char symbol;
ItemType type;
int durability; // 耐久度,-1表示无限
int quantity; // 数量,用于消耗品
Item(int id, string n, int h, int a, int d, int p, char s, ItemType t, int dur = -1)
: id(id), name(n), healthBonus(h), attackBonus(a), defenseBonus(d),
price(p), symbol(s), type(t), durability(dur), quantity(1) {}
void display() const {
cout << name << " ";
if (healthBonus > 0) cout << "生命+" << healthBonus << " ";
if (attackBonus > 0) cout << "攻击+" << attackBonus << " ";
if (defenseBonus > 0) cout << "防御+" << defenseBonus << " ";
if (durability > 0) cout << "耐久:" << durability << " ";
if (type == POTION) cout << "数量:" << quantity;
}
};
// 怪物结构体
struct Monster {
int x, y;
int floor;
int health;
int maxHealth;
int attack;
int defense;
int expReward;
int goldReward;
string name;
char symbol;
MonsterType type;
Monster(int x, int y, int floor, MonsterType type) {
this->x = x;
this->y = y;
this->floor = floor;
switch(type) {
case GOBLIN:
health = maxHealth = 25 + floor * 5;
attack = 6 + floor;
defense = 2 + floor;
expReward = 25 + floor * 5;
goldReward = rand() % 20 + 5 + floor * 3;
name = "哥布林";
symbol = 'g';
break;
case ORC:
health = maxHealth = 40 + floor * 8;
attack = 9 + floor * 2;
defense = 4 + floor;
expReward = 40 + floor * 8;
goldReward = rand() % 30 + 10 + floor * 5;
name = "兽人";
symbol = 'o';
break;
case DRAGON:
health = maxHealth = 100 + floor * 20;
attack = 15 + floor * 3;
defense = 8 + floor * 2;
expReward = 100 + floor * 20;
goldReward = rand() % 100 + 50 + floor * 20;
name = "龙";
symbol = 'D';
break;
case SLIME:
health = maxHealth = 15 + floor * 3;
attack = 4 + floor;
defense = 1 + floor;
expReward = 15 + floor * 3;
goldReward = rand() % 10 + 2 + floor * 2;
name = "史莱姆";
symbol = 's';
break;
case ZOMBIE:
health = maxHealth = 30 + floor * 6;
attack = 7 + floor * 2;
defense = 3 + floor;
expReward = 30 + floor * 6;
goldReward = rand() % 15 + 5 + floor * 3;
name = "僵尸";
symbol = 'z';
break;
}
this->type = type;
}
bool isAlive() const {
return health > 0;
}
};
// 玩家结构体
struct Player {
int x, y;
int floor;
int health;
int maxHealth;
int baseAttack;
int baseDefense;
int attack;
int defense;
int level;
int exp;
int expToNextLevel;
int gold;
vector<Item> inventory;
Item* equippedWeapon;
Item* equippedArmor;
int keys;
Player() : x(0), y(0), floor(1), health(100), maxHealth(100),
baseAttack(10), baseDefense(5), level(1), exp(0),
expToNextLevel(100), gold(0), equippedWeapon(nullptr),
equippedArmor(nullptr), keys(0) {
updateStats();
}
void updateStats() {
attack = baseAttack;
defense = baseDefense;
if (equippedWeapon) {
attack += equippedWeapon->attackBonus;
}
if (equippedArmor) {
defense += equippedArmor->defenseBonus;
}
}
bool addItemToInventory(const Item& item) {
if (inventory.size() >= INVENTORY_SIZE) {
return false;
}
// 如果是消耗品,检查是否已存在
if (item.type == POTION) {
for (auto& invItem : inventory) {
if (invItem.id == item.id) {
invItem.quantity += item.quantity;
return true;
}
}
}
inventory.push_back(item);
return true;
}
void removeItem(int index) {
if (index >= 0 && index < inventory.size()) {
inventory.erase(inventory.begin() + index);
}
}
Item* getItem(int index) {
if (index >= 0 && index < inventory.size()) {
return &inventory[index];
}
return nullptr;
}
void useItem(int index) {
if (index < 0 || index >= inventory.size()) return;
Item& item = inventory[index];
switch(item.type) {
case POTION:
health = min(health + item.healthBonus, maxHealth);
cout << "使用了 " << item.name << ",恢复 " << item.healthBonus << " 点生命值" << endl;
item.quantity--;
if (item.quantity <= 0) {
removeItem(index);
}
break;
case WEAPON:
if (equippedWeapon) {
cout << "卸下了 " << equippedWeapon->name << endl;
}
equippedWeapon = &item;
updateStats();
cout << "装备了 " << item.name << ",攻击力 +" << item.attackBonus << endl;
break;
case ARMOR:
if (equippedArmor) {
cout << "卸下了 " << equippedArmor->name << endl;
}
equippedArmor = &item;
updateStats();
cout << "装备了 " << item.name << ",防御力 +" << item.defenseBonus << endl;
break;
case TREASURE_ITEM:
gold += item.price;
cout << "卖出了 " << item.name << ",获得 " << item.price << " 金币" << endl;
removeItem(index);
break;
}
}
void showInventory() {
cout << "╔══════════════════════════════════════════════════════════╗" << endl;
cout << "║ 背 包 ║" << endl;
cout << "╚══════════════════════════════════════════════════════════╝" << endl;
cout << "金币: " << gold << " | 钥匙: " << keys << endl;
cout << "当前装备: ";
if (equippedWeapon) cout << "武器: " << equippedWeapon->name << " ";
if (equippedArmor) cout << "防具: " << equippedArmor->name;
cout << endl;
cout << "══════════════════════════════════════════════════════════" << endl;
if (inventory.empty()) {
cout << "背包是空的" << endl;
} else {
for (size_t i = 0; i < inventory.size(); i++) {
cout << setw(2) << (i+1) << ". ";
inventory[i].display();
cout << endl;
}
}
}
void levelUp() {
level++;
exp -= expToNextLevel;
expToNextLevel = static_cast<int>(expToNextLevel * 1.5);
maxHealth += 20;
health = maxHealth;
baseAttack += 5;
baseDefense += 2;
updateStats();
cout << "\n╔══════════════════════════╗" << endl;
cout << "║ 等级提升! ║" << endl;
cout << "║ 现在是 " << level << " 级 ║" << endl;
cout << "║ 生命值 +20 ║" << endl;
cout << "║ 攻击力 +5 ║" << endl;
cout << "║ 防御力 +2 ║" << endl;
cout << "╚══════════════════════════╝" << endl;
cout << "按任意键继续...";
_getch();
}
void gainExp(int amount) {
exp += amount;
cout << "获得 " << amount << " 点经验值" << endl;
if (exp >= expToNextLevel) {
levelUp();
}
}
void heal(int amount) {
health = min(health + amount, maxHealth);
cout << "恢复 " << amount << " 点生命值" << endl;
}
void takeDamage(int damage) {
int actualDamage = max(1, damage - defense);
health -= actualDamage;
cout << "受到 " << actualDamage << " 点伤害" << endl;
}
bool isAlive() const {
return health > 0;
}
};
// 楼层地图类
class FloorMap {
public:
vector<vector<char>> displayMap;
vector<vector<Terrain>> terrainMap;
vector<Monster> monsters;
FloorMap() {
displayMap.resize(HEIGHT, vector<char>(WIDTH, '.'));
terrainMap.resize(HEIGHT, vector<Terrain>(WIDTH, GRASS));
}
};
// 游戏类
class Game {
private:
vector<FloorMap> floors;
vector<Item> allItems;
Player player;
int turnCount;
int nextItemId;
public:
Game() : turnCount(0), nextItemId(1) {
initializeItems();
// 创建所有楼层
for (int i = 0; i < MAX_FLOORS; i++) {
floors.push_back(FloorMap());
initializeFloor(i);
}
player.x = WIDTH / 2;
player.y = HEIGHT / 2;
player.floor = 1;
}
void initializeItems() {
// 药水
allItems.push_back(Item(nextItemId++, "小血瓶", 20, 0, 0, 10, '+', POTION));
allItems.push_back(Item(nextItemId++, "大血瓶", 50, 0, 0, 25, '?', POTION));
allItems.push_back(Item(nextItemId++, "攻击药剂", 0, 5, 0, 30, '!', POTION));
allItems.push_back(Item(nextItemId++, "防御药剂", 0, 0, 3, 30, '!', POTION));
allItems.push_back(Item(nextItemId++, "超级血瓶", 100, 0, 0, 50, '?', POTION));
// 武器
allItems.push_back(Item(nextItemId++, "木棍", 0, 3, 0, 20, '/', WEAPON, 10));
allItems.push_back(Item(nextItemId++, "铁剑", 0, 8, 0, 50, '/', WEAPON, 20));
allItems.push_back(Item(nextItemId++, "钢剑", 0, 12, 0, 100, '/', WEAPON, 30));
allItems.push_back(Item(nextItemId++, "魔法杖", 0, 15, 2, 150, '*', WEAPON, 25));
allItems.push_back(Item(nextItemId++, "圣剑", 0, 20, 5, 300, '!', WEAPON, 50));
// 防具
allItems.push_back(Item(nextItemId++, "布衣", 0, 0, 2, 20, 'o', ARMOR, 10));
allItems.push_back(Item(nextItemId++, "皮甲", 0, 0, 5, 50, 'o', ARMOR, 20));
allItems.push_back(Item(nextItemId++, "铁甲", 0, 0, 8, 100, 'o', ARMOR, 30));
allItems.push_back(Item(nextItemId++, "钢甲", 0, 0, 12, 150, 'o', ARMOR, 40));
allItems.push_back(Item(nextItemId++, "神盾", 0, 0, 15, 300, 'O', ARMOR, 50));
// 宝藏物品
allItems.push_back(Item(nextItemId++, "金币袋", 0, 0, 0, 50, '$', TREASURE_ITEM));
allItems.push_back(Item(nextItemId++, "宝石", 0, 0, 0, 100, '$', TREASURE_ITEM));
allItems.push_back(Item(nextItemId++, "王冠", 0, 0, 0, 200, '$', TREASURE_ITEM));
// 钥匙
allItems.push_back(Item(nextItemId++, "地牢钥匙", 0, 0, 0, 0, 'K', KEY));
}
void initializeFloor(int floorIndex) {
srand(time(0) + floorIndex);
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int r = rand() % 100;
if (r < 10 + floorIndex * 2) {
floors[floorIndex].terrainMap[i][j] = TREE;
floors[floorIndex].displayMap[i][j] = '#';
} else if (r < 20 + floorIndex * 2) {
floors[floorIndex].terrainMap[i][j] = WATER;
floors[floorIndex].displayMap[i][j] = '~';
} else if (r < 25 + floorIndex) {
floors[floorIndex].terrainMap[i][j] = WALL;
floors[floorIndex].displayMap[i][j] = '|';
}
}
}
generateStairs(floorIndex);
generateMonsters(floorIndex, 3 + floorIndex * 2);
generateTreasure(floorIndex, 5 + floorIndex);
if (floorIndex == 0) {
floors[floorIndex].terrainMap[player.y][player.x] = GRASS;
}
}
void generateStairs(int floorIndex) {
int stairsX, stairsY;
if (floorIndex < MAX_FLOORS - 1) {
do {
stairsX = rand() % WIDTH;
stairsY = rand() % HEIGHT;
} while (floors[floorIndex].terrainMap[stairsY][stairsX] != GRASS);
floors[floorIndex].terrainMap[stairsY][stairsX] = STAIRS_DOWN;
floors[floorIndex].displayMap[stairsY][stairsX] = '>';
}
if (floorIndex > 0) {
do {
stairsX = rand() % WIDTH;
stairsY = rand() % HEIGHT;
} while (floors[floorIndex].terrainMap[stairsY][stairsX] != GRASS);
floors[floorIndex].terrainMap[stairsY][stairsX] = STAIRS_UP;
floors[floorIndex].displayMap[stairsY][stairsX] = '<';
}
}
void generateMonsters(int floorIndex, int count) {
FloorMap& currentFloor = floors[floorIndex];
int floor = floorIndex + 1;
for (int i = 0; i < count; i++) {
int x, y;
do {
x = rand() % WIDTH;
y = rand() % HEIGHT;
} while (currentFloor.terrainMap[y][x] != GRASS);
MonsterType type;
int r = rand() % 100;
if (floor >= 5 && r < 20) {
type = DRAGON;
} else if (floor >= 4 && r < 40) {
type = ORC;
} else if (floor >= 3 && r < 60) {
type = ZOMBIE;
} else if (floor >= 2 && r < 80) {
type = GOBLIN;
} else {
type = static_cast<MonsterType>(rand() % MAX_TYPES);
}
currentFloor.monsters.push_back(Monster(x, y, floor, type));
currentFloor.displayMap[y][x] = currentFloor.monsters.back().symbol;
}
}
void generateTreasure(int floorIndex, int count) {
FloorMap& currentFloor = floors[floorIndex];
for (int i = 0; i < count; i++) {
int x, y;
do {
x = rand() % WIDTH;
y = rand() % HEIGHT;
} while (currentFloor.terrainMap[y][x] != GRASS);
currentFloor.terrainMap[y][x] = TREASURE;
currentFloor.displayMap[y][x] = '$';
}
}
void display() {
system("cls");
FloorMap& currentFloor = floors[player.floor - 1];
cout << "╔══════════════════════════════════════════════════════════╗" << endl;
cout << "║ 地下城冒险 - 背包系统 ║" << endl;
cout << "║ 第 " << player.floor << " 层 ║" << endl;
cout << "╚══════════════════════════════════════════════════════════╝" << endl;
// 显示地图
cout << "地图:" << endl;
cout << "╔";
for (int j = 0; j < WIDTH; j++) cout << "══";
cout << "╗" << endl;
for (int i = 0; i < HEIGHT; i++) {
cout << "║";
for (int j = 0; j < WIDTH; j++) {
if (i == player.y && j == player.x) {
cout << "P ";
} else {
cout << currentFloor.displayMap[i][j] << " ";
}
}
cout << "║" << endl;
}
cout << "╚";
for (int j = 0; j < WIDTH; j++) cout << "══";
cout << "╝" << endl;
displayHUD();
// 显示图例
cout << "\n图例: P=玩家";
for (const auto& monster : currentFloor.monsters) {
if (monster.isAlive()) {
cout << ", " << monster.symbol << "=" << monster.name;
}
}
cout << ", #=树木, ~=水, |=墙, $=宝藏, >=下楼, <=上楼, K=钥匙" << endl;
// 显示楼层进度
cout << "楼层进度: ";
for (int i = 0; i < MAX_FLOORS; i++) {
if (i < player.floor) {
cout << "■ ";
} else {
cout << "□ ";
}
}
cout << endl;
}
void displayHUD() {
cout << "\n══════════════════════════════════════════════════════════" << endl;
cout << " 玩家状态:" << endl;
cout << " 生命值: " << player.health << "/" << player.maxHealth;
cout << " | 等级: " << player.level;
cout << " | 经验: " << player.exp << "/" << player.expToNextLevel << endl;
cout << " 攻击力: " << player.attack << " (" << player.baseAttack;
if (player.equippedWeapon) cout << "+" << player.equippedWeapon->attackBonus;
cout << ")" << endl;
cout << " 防御力: " << player.defense << " (" << player.baseDefense;
if (player.equippedArmor) cout << "+" << player.equippedArmor->defenseBonus;
cout << ")" << endl;
cout << " 金币: " << player.gold << " | 钥匙: " << player.keys;
cout << " | 背包: " << player.inventory.size() << "/" << INVENTORY_SIZE << endl;
cout << "══════════════════════════════════════════════════════════" << endl;
}
bool canMoveTo(int x, int y) {
if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
return false;
}
FloorMap& currentFloor = floors[player.floor - 1];
Terrain terrain = currentFloor.terrainMap[y][x];
return terrain == GRASS || terrain == TREASURE ||
terrain == STAIRS_DOWN || terrain == STAIRS_UP;
}
void movePlayer(int dx, int dy) {
int newX = player.x + dx;
int newY = player.y + dy;
if (canMoveTo(newX, newY)) {
FloorMap& currentFloor = floors[player.floor - 1];
Terrain terrain = currentFloor.terrainMap[newY][newX];
if (terrain == STAIRS_DOWN && player.floor < MAX_FLOORS) {
changeFloor(1);
} else if (terrain == STAIRS_UP && player.floor > 1) {
changeFloor(-1);
} else {
player.x = newX;
player.y = newY;
turnCount++;
checkMonsterEncounter();
checkTreasurePickup();
moveMonsters();
}
} else {
cout << "无法移动到此位置!" << endl;
_getch();
}
}
void changeFloor(int direction) {
system("cls");
cout << "╔══════════════════════════════════════════╗" << endl;
if (direction > 0) {
cout << "║ 进入第 " << (player.floor + 1) << " 层 ║" << endl;
} else {
cout << "║ 返回第 " << (player.floor - 1) << " 层 ║" << endl;
}
cout << "╚══════════════════════════════════════════╝" << endl;
// 找到楼梯位置
FloorMap& currentFloor = floors[player.floor - 1];
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (direction > 0 && currentFloor.terrainMap[i][j] == STAIRS_DOWN) {
player.x = j;
player.y = i;
break;
} else if (direction < 0 && currentFloor.terrainMap[i][j] == STAIRS_UP) {
player.x = j;
player.y = i;
break;
}
}
}
player.floor += direction;
cout << "按任意键继续...";
_getch();
if (player.floor == MAX_FLOORS) {
checkVictory();
}
}
void checkMonsterEncounter() {
FloorMap& currentFloor = floors[player.floor - 1];
for (auto& monster : currentFloor.monsters) {
if (monster.isAlive() && monster.x == player.x && monster.y == player.y) {
startCombat(monster);
break;
}
}
}
void checkTreasurePickup() {
FloorMap& currentFloor = floors[player.floor - 1];
if (currentFloor.terrainMap[player.y][player.x] == TREASURE) {
int floorMultiplier = player.floor;
int choice = rand() % 100;
if (choice < 30) {
// 金币
int goldFound = (rand() % 50 + 20) * floorMultiplier;
player.gold += goldFound;
system("cls");
cout << "╔══════════════════════════════════════════╗" << endl;
cout << "║ 发现宝箱! ║" << endl;
cout << "║ 第" << player.floor << "层 ║" << endl;
cout << "╚══════════════════════════════════════════╝" << endl;
cout << "获得金币: " << goldFound << endl;
} else if (choice < 80) {
// 随机物品
int itemIndex = rand() % allItems.size();
Item foundItem = allItems[itemIndex];
if (foundItem.type == POTION) {
foundItem.quantity = rand() % 3 + 1; // 1-3个
}
system("cls");
cout << "╔══════════════════════════════════════════╗" << endl;
cout << "║ 发现宝箱! ║" << endl;
cout << "║ 第" << player.floor << "层 ║" << endl;
cout << "╚══════════════════════════════════════════╝" << endl;
cout << "获得物品: ";
foundItem.display();
cout << endl;
if (player.addItemToInventory(foundItem)) {
cout << "物品已放入背包" << endl;
} else {
cout << "背包已满,无法拾取物品!" << endl;
}
} else {
// 钥匙
player.keys++;
system("cls");
cout << "╔══════════════════════════════════════════╗" << endl;
cout << "║ 发现宝箱! ║" << endl;
cout << "║ 第" << player.floor << "层 ║" << endl;
cout << "╚══════════════════════════════════════════╝" << endl;
cout << "获得钥匙 x1" << endl;
cout << "当前钥匙: " << player.keys << endl;
}
// 移除宝藏
currentFloor.terrainMap[player.y][player.x] = GRASS;
currentFloor.displayMap[player.y][player.x] = '.';
cout << "按任意键继续...";
_getch();
}
}
void startCombat(Monster& monster) {
system("cls");
cout << "╔══════════════════════════════════════╗" << endl;
cout << "║ 遭遇战斗! ║" << endl;
cout << "║ 第" << player.floor << "层 " << monster.name << " ║" << endl;
cout << "╚══════════════════════════════════════╝" << endl;
int round = 1;
while (monster.isAlive() && player.isAlive()) {
cout << "\n第 " << round << " 回合:" << endl;
cout << "══════════════════════════════════" << endl;
cout << "你的生命值: " << player.health << "/" << player.maxHealth << endl;
cout << monster.name << "的生命值: " << monster.health << "/" << monster.maxHealth << endl;
cout << "══════════════════════════════════" << endl;
cout << "\n选择行动:" << endl;
cout << "1. 攻击" << endl;
cout << "2. 防御(减少50%伤害)" << endl;
cout << "3. 使用物品" << endl;
cout << "4. 逃跑(50%成功率)" << endl;
cout << "选择: ";
int choice;
cin >> choice;
bool defending = false;
switch (choice) {
case 1: { // 攻击
int damage = max(1, player.attack - monster.defense / 2);
monster.health -= damage;
cout << "你对" << monster.name << "造成了 " << damage << " 点伤害!" << endl;
// 装备耐久度减少
if (player.equippedWeapon && player.equippedWeapon->durability > 0) {
player.equippedWeapon->durability--;
if (player.equippedWeapon->durability <= 0) {
cout << "你的" << player.equippedWeapon->name << "损坏了!" << endl;
player.equippedWeapon = nullptr;
player.updateStats();
}
}
break;
}
case 2: { // 防御
defending = true;
cout << "你进入防御姿态!" << endl;
break;
}
case 3: { // 使用物品
useItemMenu();
continue; // 重新显示菜单
}
case 4: { // 逃跑
if (rand() % 100 < 50) {
cout << "成功逃跑!" << endl;
cout << "按任意键继续...";
_getch();
return;
} else {
cout << "逃跑失败!" << endl;
}
break;
}
default:
cout << "无效选择,自动攻击!" << endl;
monster.health -= player.attack;
break;
}
// 怪物行动
if (monster.isAlive()) {
int monsterDamage = max(1, monster.attack - player.defense);
if (defending) {
monsterDamage = max(1, monsterDamage / 2);
cout << monster.name << "的攻击被格挡!" << endl;
}
player.takeDamage(monsterDamage);
if (!player.isAlive()) {
cout << "\n你被" << monster.name << "击败了!" << endl;
break;
}
}
round++;
if (player.isAlive() && monster.isAlive()) {
cout << "\n按任意键继续下一回合...";
_getch();
system("cls");
}
}
if (player.isAlive()) {
cout << "\n══════════════════════════════════" << endl;
cout << "胜利!你击败了" << monster.name << "!" << endl;
player.gainExp(monster.expReward);
player.gold += monster.goldReward;
cout << "获得 " << monster.goldReward << " 金币" << endl;
// 20%概率掉落物品
if (rand() % 100 < 20) {
int itemIndex = rand() % allItems.size();
Item droppedItem = allItems[itemIndex];
if (droppedItem.type == POTION) {
droppedItem.quantity = 1;
}
cout << monster.name << "掉落了: ";
droppedItem.display();
cout << endl;
if (player.addItemToInventory(droppedItem)) {
cout << "物品已放入背包" << endl;
} else {
cout << "背包已满,无法拾取物品!" << endl;
}
}
// 移除死亡的怪物
floors[player.floor - 1].displayMap[monster.y][monster.x] = '.';
}
cout << "按任意键继续...";
_getch();
}
void useItemMenu() {
system("cls");
cout << "╔══════════════════════════════════════════╗" << endl;
cout << "║ 使用物品 ║" << endl;
cout << "╚══════════════════════════════════════════╝" << endl;
if (player.inventory.empty()) {
cout << "背包是空的!" << endl;
cout << "按任意键继续...";
_getch();
return;
}
player.showInventory();
cout << "\n选择物品编号使用 (0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= player.inventory.size()) {
player.useItem(choice - 1);
}
cout << "按任意键继续...";
_getch();
}
void showInventoryMenu() {
system("cls");
player.showInventory();
cout << "\n══════════════════════════════════════════" << endl;
cout << "1. 使用物品" << endl;
cout << "2. 丢弃物品" << endl;
cout << "3. 查看商店" << endl;
cout << "0. 返回游戏" << endl;
cout << "选择: ";
int choice;
cin >> choice;
switch(choice) {
case 1:
useItemMenu();
break;
case 2:
discardItemMenu();
break;
case 3:
showShopMenu();
break;
}
}
void discardItemMenu() {
system("cls");
cout << "╔══════════════════════════════════════════╗" << endl;
cout << "║ 丢弃物品 ║" << endl;
cout << "╚══════════════════════════════════════════╝" << endl;
if (player.inventory.empty()) {
cout << "背包是空的!" << endl;
cout << "按任意键继续...";
_getch();
return;
}
player.showInventory();
cout << "\n选择物品编号丢弃 (0返回): ";
int choice;
cin >> choice;
if (choice > 0 && choice <= player.inventory.size()) {
Item* item = player.getItem(choice - 1);
cout << "确定丢弃 " << item->name << " 吗?(y/n): ";
char confirm;
cin >> confirm;
if (confirm == 'y' || confirm == 'Y') {
// 如果是装备中的物品,先卸载
if (player.equippedWeapon == item) {
player.equippedWeapon = nullptr;
}
if (player.equippedArmor == item) {
player.equippedArmor = nullptr;
}
player.removeItem(choice - 1);
player.updateStats();
cout << "物品已丢弃" << endl;
}
}
cout << "按任意键继续...";
_getch();
}
void showShopMenu() {
system("cls");
cout << "╔══════════════════════════════════════════════════════════╗" << endl;
cout << "║ 商 店 ║" << endl;
cout << "╚══════════════════════════════════════════════════════════╝" << endl;
cout << "金币: " << player.gold << " | 背包: " << player.inventory.size() << "/" << INVENTORY_SIZE << endl;
cout << "══════════════════════════════════════════════════════════" << endl;
cout << "物品列表 (按数字购买):" << endl;
for (size_t i = 0; i < allItems.size(); i++) {
cout << setw(2) << (i+1) << ". ";
allItems[i].display();
cout << " - " << allItems[i].price << "金币" << endl;
}
cout << "\n══════════════════════════════════════════════════════════" << endl;
cout << "0. 返回" << endl;
cout << "选择: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= allItems.size()) {
Item& item = allItems[choice - 1];
if (player.gold >= item.price) {
if (player.inventory.size() >= INVENTORY_SIZE) {
cout << "背包已满,无法购买!" << endl;
} else {
player.gold -= item.price;
Item newItem = item;
if (newItem.type == POTION) {
newItem.quantity = 1; // 商店购买默认1个
}
player.addItemToInventory(newItem);
cout << "购买了 " << item.name << "!" << endl;
if (item.type == WEAPON || item.type == ARMOR) {
cout << "要立即装备吗?(y/n): ";
char equipChoice;
cin >> equipChoice;
if (equipChoice == 'y' || equipChoice == 'Y') {
player.useItem(player.inventory.size() - 1);
}
}
}
} else {
cout << "金币不足!" << endl;
}
cout << "按任意键继续...";
_getch();
}
}
void moveMonsters() {
FloorMap& currentFloor = floors[player.floor - 1];
for (auto& monster : currentFloor.monsters) {
if (monster.isAlive()) {
int moveChance = 10 + player.floor * 2;
if (moveChance > 30) moveChance = 30;
if (rand() % 100 < moveChance) {
int dx = (rand() % 3) - 1;
int dy = (rand() % 3) - 1;
int newX = monster.x + dx;
int newY = monster.y + dy;
if (canMoveTo(newX, newY) && !(newX == player.x && newY == player.y)) {
currentFloor.displayMap[monster.y][monster.x] = '.';
monster.x = newX;
monster.y = newY;
currentFloor.displayMap[monster.y][monster.x] = monster.symbol;
}
}
}
}
}
void saveGame() {
ofstream file("savegame.txt");
if (file.is_open()) {
file << player.x << " " << player.y << " "
<< player.floor << " "
<< player.health << " " << player.maxHealth << " "
<< player.baseAttack << " " << player.baseDefense << " "
<< player.level << " " << player.exp << " "
<< player.expToNextLevel << " " << player.gold << " "
<< player.keys << " "
<< turnCount << endl;
// 保存装备
int weaponId = player.equippedWeapon ? player.equippedWeapon->id : -1;
int armorId = player.equippedArmor ? player.equippedArmor->id : -1;
file << weaponId << " " << armorId << endl;
// 保存背包
file << player.inventory.size() << endl;
for (const auto& item : player.inventory) {
file << item.id << " " << item.quantity << endl;
}
file.close();
cout << "游戏已保存!" << endl;
} else {
cout << "保存失败!" << endl;
}
cout << "按任意键继续...";
_getch();
}
void loadGame() {
ifstream file("savegame.txt");
if (file.is_open()) {
// 加载玩家基本数据
file >> player.x >> player.y
>> player.floor
>> player.health >> player.maxHealth
>> player.baseAttack >> player.baseDefense
>> player.level >> player.exp
>> player.expToNextLevel >> player.gold
>> player.keys
>> turnCount;
player.updateStats();
// 加载装备
int weaponId, armorId;
file >> weaponId >> armorId;
player.equippedWeapon = nullptr;
player.equippedArmor = nullptr;
// 加载背包
player.inventory.clear();
int inventorySize;
file >> inventorySize;
for (int i = 0; i < inventorySize; i++) {
int itemId, quantity;
file >> itemId >> quantity;
// 查找物品
for (const auto& item : allItems) {
if (item.id == itemId) {
Item loadedItem = item;
loadedItem.quantity = quantity;
player.inventory.push_back(loadedItem);
// 恢复装备
if (weaponId == itemId) {
player.equippedWeapon = &player.inventory.back();
}
if (armorId == itemId) {
player.equippedArmor = &player.inventory.back();
}
break;
}
}
}
player.updateStats();
cout << "游戏已加载!" << endl;
} else {
cout << "没有找到保存文件!" << endl;
}
cout << "按任意键继续...";
_getch();
}
void checkVictory() {
FloorMap& currentFloor = floors[player.floor - 1];
bool allMonstersDead = true;
for (const auto& monster : currentFloor.monsters) {
if (monster.isAlive()) {
allMonstersDead = false;
break;
}
}
if (allMonstersDead && player.floor == MAX_FLOORS) {
system("cls");
cout << "╔══════════════════════════════════════════════════════════╗" << endl;
cout << "║ 恭喜通关! ║" << endl;
cout << "║ 你征服了所有楼层! ║" << endl;
cout << "║ 击败了地下城的魔王! ║" << endl;
cout << "╚══════════════════════════════════════════════════════════╝" << endl;
cout << "最终等级: " << player.level << endl;
cout << "获得金币: " << player.gold << endl;
cout << "背包物品: " << player.inventory.size() << " 件" << endl;
if (player.equippedWeapon) cout << "装备武器: " << player.equippedWeapon->name << endl;
if (player.equippedArmor) cout << "装备防具: " << player.equippedArmor->name << endl;
cout << "游戏回合: " << turnCount << endl;
cout << "══════════════════════════════════════════════════════════" << endl;
cout << "\n按任意键退出...";
_getch();
exit(0);
}
}
void showHelp() {
system("cls");
cout << "╔══════════════════════════════════════════════════════════╗" << endl;
cout << "║ 游戏帮助 ║" << endl;
cout << "╚══════════════════════════════════════════════════════════╝" << endl;
cout << "移动: W(上), A(左), S(下), D(右)" << endl;
cout << "背包: I (查看背包/使用物品)" << endl;
cout << "商店: B (打开商店购买物品)" << endl;
cout << "保存: V (保存游戏)" << endl;
cout << "加载: L (加载游戏)" << endl;
cout << "帮助: H (显示帮助)" << endl;
cout << "退出: Q (退出游戏)" << endl;
cout << "══════════════════════════════════════════════════════════" << endl;
cout << "背包系统说明:" << endl;
cout << "- 最多可携带 " << INVENTORY_SIZE << " 件物品" << endl;
cout << "- 消耗品可叠加数量" << endl;
cout << "- 装备有耐久度,为0时会损坏" << endl;
cout << "- 可同时装备武器和防具" << endl;
cout << "══════════════════════════════════════════════════════════" << endl;
cout << "物品类型说明:" << endl;
cout << "药水(POTION): 可恢复生命值或提供临时属性加成" << endl;
cout << "武器(WEAPON): 装备后增加攻击力,有耐久度限制" << endl;
cout << "防具(ARMOR): 装备后增加防御力,有耐久度限制" << endl;
cout << "宝藏(TREASURE): 可出售获得金币" << endl;
cout << "钥匙(KEY): 特殊物品,可能有特殊用途" << endl;
cout << "══════════════════════════════════════════════════════════" << endl;
cout << "按任意键返回游戏...";
_getch();
}
void run() {
char input;
bool running = true;
while (running && player.isAlive()) {
display();
cout << "\n移动(WASD) | 背包(I) | 商店(B) | 保存(V) | 加载(L) | 帮助(H) | 退出(Q): ";
cin >> input;
switch (tolower(input)) {
case 'w':
movePlayer(0, -1);
break;
case 's':
movePlayer(0, 1);
break;
case 'a':
movePlayer(-1, 0);
break;
case 'd':
movePlayer(1, 0);
break;
case 'i':
showInventoryMenu();
break;
case 'b':
showShopMenu();
break;
case 'v':
saveGame();
break;
case 'l':
loadGame();
break;
case 'h':
showHelp();
break;
case 'q':
running = false;
cout << "确定退出游戏吗?(y/n): ";
cin >> input;
if (tolower(input) != 'y') running = true;
break;
default:
cout << "无效输入!" << endl;
cout << "按任意键继续...";
_getch();
break;
}
// 随机生成新怪物
FloorMap& currentFloor = floors[player.floor - 1];
if (turnCount % 10 == 0 && currentFloor.monsters.size() < 5 + player.floor * 2) {
MonsterType type = static_cast<MonsterType>(rand() % MAX_TYPES);
int x, y;
do {
x = rand() % WIDTH;
y = rand() % HEIGHT;
} while ((x == player.x && y == player.y) ||
currentFloor.terrainMap[y][x] != GRASS ||
currentFloor.displayMap[y][x] != '.');
currentFloor.monsters.push_back(Monster(x, y, player.floor, type));
currentFloor.displayMap[y][x] = currentFloor.monsters.back().symbol;
}
}
if (!player.isAlive()) {
system("cls");
cout << "╔══════════════════════════════════════════════════════════╗" << endl;
cout << "║ 游戏结束! ║" << endl;
cout << "║ 你被击败了... ║" << endl;
cout << "║ 到达第 " << player.floor << " 层 ║" << endl;
cout << "╚══════════════════════════════════════════════════════════╝" << endl;
cout << "最终等级: " << player.level << endl;
cout << "获得金币: " << player.gold << endl;
cout << "背包物品: " << player.inventory.size() << " 件" << endl;
cout << "游戏回合: " << turnCount << endl;
}
cout << "\n按任意键退出...";
_getch();
}
};
int main() {
srand(static_cast<unsigned>(time(0)));
Game game;
game.run();
return 0;
}
全部评论 2
d
6天前 来自 浙江
2d
6天前 来自 浙江
1























有帮助,赞一个