我的世界C++游戏
2026-03-11 19:51:04
发布于:浙江
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cstring>
#include <chrono>
#include <thread>
#include <windows.h>
using namespace std;
// ===================== 全局常量 =====================
// 基础属性
const int MAX_BASE_HEALTH = 20; // 初始最大生命值
const int MAX_HUNGER = 20; // 最大饥饿值
const int MAX_SATURATION = 5; // 初始最大饱和度
const int EXP_PER_LEVEL = 100; // 每级基础经验
const int DAY_DURATION = 10; // 白天时长(游戏回合)
const int NIGHT_DURATION = 5; // 夜晚时长(游戏回合)
// 物品类型枚举(扩展版,补充缺失的枚举)
enum ItemType {
// 基础资源
ITEM_WOOD, ITEM_STONE, ITEM_IRON_ORE, ITEM_GOLD_ORE, ITEM_DIAMOND_ORE,
ITEM_COAL, ITEM_REDSTONE, ITEM_LAPIS, ITEM_EMERALD, ITEM_CLAY, ITEM_SAND, ITEM_WOOL,
ITEM_STRING, ITEM_FLINT, ITEM_FEATHER, ITEM_LEATHER,
// 加工资源
ITEM_PLANK, ITEM_STICK, ITEM_IRON_INGOT, ITEM_GOLD_INGOT, ITEM_DIAMOND,
ITEM_GLASS, ITEM_BRICK, ITEM_OBSIDIAN, ITEM_COBBLESTONE,
// 食物
ITEM_APPLE, ITEM_BREAD, ITEM_COOKED_BEEF, ITEM_RAW_BEEF, ITEM_CARROT,
ITEM_POTATO, ITEM_COOKED_POTATO, ITEM_MELON, ITEM_SWEET_BERRIES,
ITEM_ROTTEN_FLESH, // 补充缺失的腐肉枚举
// 工具(镐/斧/锹/锄)
TOOL_WOODEN_PICKAXE, TOOL_STONE_PICKAXE, TOOL_IRON_PICKAXE, TOOL_DIAMOND_PICKAXE,
TOOL_WOODEN_AXE, TOOL_STONE_AXE, TOOL_IRON_AXE, TOOL_DIAMOND_AXE,
TOOL_WOODEN_SHOVEL, TOOL_STONE_SHOVEL, TOOL_IRON_SHOVEL, TOOL_DIAMOND_SHOVEL,
TOOL_WOODEN_HOE, TOOL_STONE_HOE, TOOL_IRON_HOE, TOOL_DIAMOND_HOE,
// 武器
WEAPON_WOODEN_SWORD, WEAPON_STONE_SWORD, WEAPON_IRON_SWORD, WEAPON_DIAMOND_SWORD,
WEAPON_BOW, WEAPON_ARROW, WEAPON_TRIDENT,
// 防具
ARMOR_IRON_HELMET, ARMOR_IRON_CHESTPLATE, ARMOR_IRON_LEGGINGS, ARMOR_IRON_BOOTS,
ARMOR_DIAMOND_HELMET, ARMOR_DIAMOND_CHESTPLATE, ARMOR_DIAMOND_LEGGINGS, ARMOR_DIAMOND_BOOTS,
// 建筑方块
BLOCK_WOOD, BLOCK_STONE, BLOCK_BRICK, BLOCK_GLASS, BLOCK_OBSIDIAN,
BLOCK_CHEST, BLOCK_FURNACE, BLOCK_CRAFTING_TABLE, BLOCK_BED,
// 特殊物品
ITEM_POTION_HEAL, ITEM_POTION_SPEED, ITEM_ENCHANTMENT_TABLE, ITEM_ANVIL,
ITEM_GUNPOWDER, // 补充缺失的火药枚举
ITEM_DRAGON_EGG, // 补充缺失的龙蛋枚举
// 任务物品
ITEM_QUEST_EMERALD, ITEM_QUEST_DIAMOND,
// 占位符
ITEM_UNKNOWN
};
// 生物类型枚举
enum EntityType {
// 被动生物
ENTITY_COW, ENTITY_PIG, ENTITY_SHEEP, ENTITY_CHICKEN, ENTITY_VILLAGER, ENTITY_WOLF,
// 敌对生物
ENTITY_ZOMBIE, ENTITY_SKELETON, ENTITY_CREEPER, ENTITY_SPIDER, ENTITY_ENDERMAN,
// BOSS
ENTITY_ENDER_DRAGON,
// 中立生物
ENTITY_IRON_GOLEM
};
// 生物群系枚举
enum BiomeType {
BIOME_FOREST, BIOME_PLAINS, BIOME_MOUNTAINS, BIOME_DESERT, BIOME_JUNGLE,
BIOME_SWAMP, BIOME_TAIGA, BIOME_SNOWY_TAIGA, BIOME_OCEAN, BIOME_NETHER, BIOME_END
};
// 天气类型枚举
enum WeatherType {
WEATHER_CLEAR, WEATHER_RAIN, WEATHER_THUNDER, WEATHER_SNOW
};
// 难度枚举
enum Difficulty {
DIFFICULTY_PEACEFUL, DIFFICULTY_EASY, DIFFICULTY_NORMAL, DIFFICULTY_HARD
};
// 成就枚举
enum AchievementType {
ACHIEVEMENT_FIRST_WOOD, ACHIEVEMENT_FIRST_STONE, ACHIEVEMENT_FIRST_IRON,
ACHIEVEMENT_FIRST_DIAMOND, ACHIEVEMENT_BUILD_SHELTER, ACHIEVEMENT_DEFEAT_ZOMBIE,
ACHIEVEMENT_DEFEAT_ENDER_DRAGON, ACHIEVEMENT_MAX
};
// 任务状态枚举
enum QuestStatus {
QUEST_NOT_STARTED, QUEST_IN_PROGRESS, QUEST_COMPLETED, QUEST_FAILED
};
// ===================== 工具函数 =====================
// 物品类型转字符串
string itemTypeToString(ItemType type) {
switch (type) {
case ITEM_WOOD: return "木头";
case ITEM_STONE: return "石头";
case ITEM_IRON_ORE: return "铁矿石";
case ITEM_GOLD_ORE: return "金矿石";
case ITEM_DIAMOND_ORE: return "钻石矿";
case ITEM_COAL: return "煤炭";
case ITEM_PLANK: return "木板";
case ITEM_STICK: return "木棍";
case ITEM_IRON_INGOT: return "铁锭";
case ITEM_DIAMOND: return "钻石";
case ITEM_APPLE: return "苹果";
case ITEM_RAW_BEEF: return "生牛肉";
case ITEM_COOKED_BEEF: return "熟牛肉";
case ITEM_ROTTEN_FLESH: return "腐肉"; // 补充腐肉字符串转换
case ITEM_GUNPOWDER: return "火药"; // 补充火药字符串转换
case ITEM_DRAGON_EGG: return "龙蛋"; // 补充龙蛋字符串转换
case TOOL_WOODEN_PICKAXE: return "木镐";
case TOOL_STONE_PICKAXE: return "石镐";
case TOOL_IRON_PICKAXE: return "铁镐";
case TOOL_DIAMOND_PICKAXE: return "钻石镐";
case TOOL_WOODEN_AXE: return "木斧";
case TOOL_IRON_AXE: return "铁斧";
case WEAPON_WOODEN_SWORD: return "木剑";
case WEAPON_IRON_SWORD: return "铁剑";
case WEAPON_DIAMOND_SWORD: return "钻石剑";
case ARMOR_IRON_HELMET: return "铁头盔";
case ARMOR_IRON_CHESTPLATE: return "铁胸甲";
case BLOCK_CRAFTING_TABLE: return "工作台";
case BLOCK_FURNACE: return "熔炉";
case BLOCK_BED: return "床";
default: return "未知物品";
}
}
// 生物类型转字符串
string entityTypeToString(EntityType type) {
switch (type) {
case ENTITY_COW: return "牛";
case ENTITY_PIG: return "猪";
case ENTITY_SHEEP: return "羊";
case ENTITY_ZOMBIE: return "僵尸";
case ENTITY_SKELETON: return "骷髅";
case ENTITY_CREEPER: return "爬行者";
case ENTITY_ENDER_DRAGON: return "末影龙";
default: return "未知生物";
}
}
// 补充:生物群系转字符串(缺失的函数)
string biomeTypeToString(BiomeType type) {
switch (type) {
case BIOME_FOREST: return "森林";
case BIOME_PLAINS: return "平原";
case BIOME_MOUNTAINS: return "山地";
case BIOME_DESERT: return "沙漠";
case BIOME_JUNGLE: return "丛林";
case BIOME_SWAMP: return "沼泽";
case BIOME_TAIGA: return "针叶林";
case BIOME_SNOWY_TAIGA: return "积雪针叶林";
case BIOME_OCEAN: return "海洋";
case BIOME_NETHER: return "下界";
case BIOME_END: return "末地";
default: return "未知生物群系";
}
}
// 补充:天气类型转字符串(缺失的函数)
string weatherTypeToString(WeatherType type) {
switch (type) {
case WEATHER_CLEAR: return "晴朗";
case WEATHER_RAIN: return "下雨";
case WEATHER_THUNDER: return "雷雨";
case WEATHER_SNOW: return "下雪";
default: return "未知天气";
}
}
// 随机数生成
int randomInt(int min, int max) {
return rand() % (max - min + 1) + min;
}
// 延迟函数(模拟游戏动画)
void wait(int ms) {
this_threadsleep_for(chronomilliseconds(ms));
}
// 清屏函数(跨平台)
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// ===================== 物品结构体 =====================
struct Item {
string name; // 名称
ItemType type; // 类型
int durability; // 当前耐久
int maxDurability; // 最大耐久
int quantity; // 数量
int stackSize; // 最大堆叠数
bool isEnchanted; // 是否附魔
string enchantment; // 附魔效果
int damage; // 伤害值
int defense; // 防御值
int hungerRestore; // 恢复饥饿值
int healthRestore; // 恢复生命值
bool isEdible; // 是否可食用
bool isCraftable; // 是否可制作
bool isSmeltable; // 是否可烧制
// 构造函数
Item() : name("未知物品"), type(ITEM_UNKNOWN), durability(0), maxDurability(0),
quantity(1), stackSize(64), isEnchanted(false), enchantment("无"),
damage(0), defense(0), hungerRestore(0), healthRestore(0),
isEdible(false), isCraftable(false), isSmeltable(false) {}
Item(string n, ItemType t, int dur = 0, int maxDur = 0, int qty = 1, int stack = 64)
: name(n), type(t), durability(dur), maxDurability(maxDur),
quantity(qty), stackSize(stack), isEnchanted(false), enchantment("无"),
damage(0), defense(0), hungerRestore(0), healthRestore(0),
isEdible(false), isCraftable(true), isSmeltable(false) {
initProperties();
}
// 初始化物品属性
void initProperties() {
switch (type) {
// 工具(镐)
case TOOL_WOODEN_PICKAXE:
maxDurability = 60; durability = 60; stackSize = 1; damage = 2; break;
case TOOL_STONE_PICKAXE:
maxDurability = 132; durability = 132; stackSize = 1; damage = 3; break;
case TOOL_IRON_PICKAXE:
maxDurability = 251; durability = 251; stackSize = 1; damage = 4; break;
case TOOL_DIAMOND_PICKAXE:
maxDurability = 1561; durability = 1561; stackSize = 1; damage = 5; break;
// 武器(剑)
case WEAPON_WOODEN_SWORD:
maxDurability = 60; durability = 60; stackSize = 1; damage = 4; break;
case WEAPON_IRON_SWORD:
maxDurability = 251; durability = 251; stackSize = 1; damage = 6; break;
case WEAPON_DIAMOND_SWORD:
maxDurability = 1561; durability = 1561; stackSize = 1; damage = 7; break;
// 防具
case ARMOR_IRON_HELMET:
maxDurability = 251; durability = 251; stackSize = 1; defense = 3; break;
case ARMOR_IRON_CHESTPLATE:
maxDurability = 251; durability = 251; stackSize = 1; defense = 6; break;
// 食物
case ITEM_APPLE:
stackSize = 64; isEdible = true; hungerRestore = 4; break;
case ITEM_COOKED_BEEF:
stackSize = 64; isEdible = true; hungerRestore = 8; healthRestore = 1; break;
case ITEM_RAW_BEEF:
stackSize = 64; isEdible = true; hungerRestore = 3; isSmeltable = true; break;
// 资源
case ITEM_IRON_ORE:
stackSize = 64; isSmeltable = true; break;
default: break;
}
}
// 使用物品(消耗耐久/数量)
void use() {
if (stackSize == 1 && maxDurability > 0) {
durability--;
if (durability <= 0) {
quantity = 0;
cout << name << " 损坏了!" << endl;
}
} else {
quantity--;
}
}
// 附魔物品
void enchant(const string& ench, int level) {
isEnchanted = true;
enchantment = ench + " " + to_string(level);
if (ench == "锋利") damage += level;
if (ench == "保护") defense += level;
if (ench == "耐久") maxDurability *= (1 + level * 0.2);
cout << name << " 附魔成功:" << enchantment << "!" << endl;
}
// 显示物品信息
void showInfo() const {
cout << "[" << name << "] 数量:" << quantity << "/" << stackSize;
if (maxDurability > 0) cout << " 耐久:" << durability << "/" << maxDurability;
if (damage > 0) cout << " 伤害:" << damage;
if (defense > 0) cout << " 防御:" << defense;
if (isEdible) cout << " 恢复饥饿值:" << hungerRestore;
cout << endl;
}
};
// ===================== 配方结构体 =====================
struct CraftingRecipe {
Item result; // 制作结果
map<ItemType, int> ingredients; // 原料(类型+数量)
bool isShapeless; // 是否无序配方
CraftingRecipe() : isShapeless(true) {}
CraftingRecipe(Item res, map<ItemType, int> ing, bool shapeless = true)
: result(res), ingredients(ing), isShapeless(shapeless) {}
// 检查原料是否足够
bool hasEnoughIngredients(const vector<Item>& inv) const {
for (const auto& ing : ingredients) {
int required = ing.second;
int available = 0;
for (const auto& item : inv) {
if (item.type == ing.first) available += item.quantity;
}
if (available < required) return false;
}
return true;
}
// 消耗原料
void consumeIngredients(vector<Item>& inv) const {
for (const auto& ing : ingredients) {
int required = ing.second;
for (auto& item : inv) {
if (item.type == ing.first) {
int take = min(required, item.quantity);
item.quantity -= take;
required -= take;
if (required <= 0) break;
}
}
}
}
// 显示配方
void showRecipe() const {
cout << "制作 " << result.name << " 需要:";
for (const auto& ing : ingredients) {
cout << itemTypeToString(ing.first) << " x" << ing.second << " ";
}
cout << endl;
}
};
// ===================== 烧制配方结构体 =====================
struct SmeltingRecipe {
ItemType input; // 输入物品
ItemType output; // 输出物品
int inputQty; // 输入数量
int outputQty; // 输出数量
int cookTime; // 烧制时间(秒)
SmeltingRecipe() : input(ITEM_UNKNOWN), output(ITEM_UNKNOWN),
inputQty(1), outputQty(1), cookTime(10) {}
SmeltingRecipe(ItemType in, ItemType out, int inQty = 1, int outQty = 1, int time = 10)
: input(in), output(out), inputQty(inQty), outputQty(outQty), cookTime(time) {}
};
// ===================== 物品管理器 =====================
class ItemManager {
private:
vector<CraftingRecipe> craftingRecipes; // 制作配方
vector<SmeltingRecipe> smeltingRecipes; // 烧制配方
public:
ItemManager() {
initCraftingRecipes();
initSmeltingRecipes();
}
// 初始化制作配方
void initCraftingRecipes() {
// 木板:4木头 → 4木板
map<ItemType, int> plankIng; plankIng[ITEM_WOOD] = 4;
craftingRecipes.push_back(CraftingRecipe(Item("木板", ITEM_PLANK, 0, 0, 4), plankIng));
// 木棍:2木板 → 4木棍
map<ItemType, int> stickIng; stickIng[ITEM_PLANK] = 2;
craftingRecipes.push_back(CraftingRecipe(Item("木棍", ITEM_STICK, 0, 0, 4), stickIng));
// 木镐:3木板 + 2木棍
map<ItemType, int> woodPickIng; woodPickIng[ITEM_PLANK] = 3; woodPickIng[ITEM_STICK] = 2;
craftingRecipes.push_back(CraftingRecipe(Item("木镐", TOOL_WOODEN_PICKAXE), woodPickIng));
// 铁镐:3铁锭 + 2木棍
map<ItemType, int> ironPickIng; ironPickIng[ITEM_IRON_INGOT] = 3; ironPickIng[ITEM_STICK] = 2;
craftingRecipes.push_back(CraftingRecipe(Item("铁镐", TOOL_IRON_PICKAXE), ironPickIng));
// 铁剑:2铁锭 + 1木棍
map<ItemType, int> ironSwordIng; ironSwordIng[ITEM_IRON_INGOT] = 2; ironSwordIng[ITEM_STICK] = 1;
craftingRecipes.push_back(CraftingRecipe(Item("铁剑", WEAPON_IRON_SWORD), ironSwordIng));
// 工作台:4木板
map<ItemType, int> tableIng; tableIng[ITEM_PLANK] = 4;
craftingRecipes.push_back(CraftingRecipe(Item("工作台", BLOCK_CRAFTING_TABLE), tableIng));
// 熔炉:8石头
map<ItemType, int> furnaceIng; furnaceIng[ITEM_STONE] = 8;
craftingRecipes.push_back(CraftingRecipe(Item("熔炉", BLOCK_FURNACE), furnaceIng));
// 铁头盔:5铁锭
map<ItemType, int> ironHelmetIng; ironHelmetIng[ITEM_IRON_INGOT] = 5;
craftingRecipes.push_back(CraftingRecipe(Item("铁头盔", ARMOR_IRON_HELMET), ironHelmetIng));
}
// 初始化烧制配方
void initSmeltingRecipes() {
smeltingRecipes.push_back(SmeltingRecipe(ITEM_IRON_ORE, ITEM_IRON_INGOT)); // 铁矿石→铁锭
smeltingRecipes.push_back(SmeltingRecipe(ITEM_RAW_BEEF, ITEM_COOKED_BEEF, 1, 1, 5)); // 生牛肉→熟牛肉
smeltingRecipes.push_back(SmeltingRecipe(ITEM_SAND, ITEM_GLASS)); // 沙子→玻璃
}
// 制作物品
bool craftItem(ItemType type, vector<Item>& inv) {
for (const auto& recipe : craftingRecipes) {
if (recipe.result.type == type) {
if (!recipe.hasEnoughIngredients(inv)) {
recipe.showRecipe();
cout << "原料不足!" << endl;
return false;
}
recipe.consumeIngredients(inv);
// 添加成品到背包
bool found = false;
for (auto& item : inv) {
if (item.type == type) {
item.quantity += recipe.result.quantity;
found = true;
break;
}
}
if (!found) inv.push_back(recipe.result);
cout << "成功制作 " << recipe.result.name << " x" << recipe.result.quantity << "!" << endl;
return true;
}
}
cout << "无该物品配方!" << endl;
return false;
}
// 烧制物品
bool smeltItem(ItemType type, vector<Item>& inv) {
for (const auto& recipe : smeltingRecipes) {
if (recipe.input == type) {
// 检查原料
int available = 0;
for (const auto& item : inv) {
if (item.type == type) available += item.quantity;
}
if (available < recipe.inputQty) {
cout << "需要 " << itemTypeToString(type) << " x" << recipe.inputQty << endl;
return false;
}
// 消耗原料
for (auto& item : inv) {
if (item.type == type) {
item.quantity -= recipe.inputQty;
break;
}
}
// 模拟烧制
cout << "烧制中..." << endl;
wait(recipe.cookTime * 100);
// 添加成品
Item output(itemTypeToString(recipe.output), recipe.output);
output.quantity = recipe.outputQty;
bool found = false;
for (auto& item : inv) {
if (item.type == recipe.output) {
item.quantity += recipe.outputQty;
found = true;
break;
}
}
if (!found) inv.push_back(output);
cout << "成功烧制 " << itemTypeToString(recipe.output) << " x" << recipe.outputQty << "!" << endl;
return true;
}
}
cout << "无该物品烧制配方!" << endl;
return false;
}
// 显示所有配方
void showAllRecipes() const {
cout << "\n=== 制作配方列表 ===" << endl;
for (int i = 0; i < craftingRecipes.size(); i++) {
cout << i+1 << ". ";
craftingRecipes[i].showRecipe();
}
cout << "\n=== 烧制配方列表 ===" << endl;
for (int i = 0; i < smeltingRecipes.size(); i++) {
cout << i+1 << ". " << itemTypeToString(smeltingRecipes[i].input)
<< " → " << itemTypeToString(smeltingRecipes[i].output) << endl;
}
}
// 补充:获取制作配方列表(缺失的函数)
vector<CraftingRecipe> getCraftingRecipes() const {
return craftingRecipes;
}
};
// ===================== 玩家类 =====================
class Player {
private:
string username; // 用户名
int health; // 当前生命值
int maxHealth; // 最大生命值
int hunger; // 饥饿值
int saturation; // 饱和度
int experience; // 经验值
int level; // 等级
Difficulty difficulty; // 难度
bool hasShelter; // 是否有庇护所
int daysSurvived; // 生存天数
// 装备栏
Item helmet; // 头盔
Item chestplate; // 胸甲
Item leggings; // 护腿
Item boots; // 靴子
Item mainHand; // 主手
Item offHand; // 副手
// 成就与任务
bool achievements[ACHIEVEMENT_MAX];
map<int, QuestStatus> quests;
public:
vector<Item> inventory; // 背包
// 构造函数
Player(string name = "玩家") : username(name), health(MAX_BASE_HEALTH), maxHealth(MAX_BASE_HEALTH),
hunger(MAX_HUNGER), saturation(MAX_SATURATION),
experience(0), level(1), difficulty(DIFFICULTY_NORMAL),
hasShelter(false), daysSurvived(0) {
// 初始化成就
memset(achievements, false, sizeof(achievements));
// 初始化任务
initQuests();
// 初始物品
addInitialItems();
}
// 初始化初始物品
void addInitialItems() {
inventory.push_back(Item("木剑", WEAPON_WOODEN_SWORD));
inventory.push_back(Item("木镐", TOOL_WOODEN_PICKAXE));
inventory.push_back(Item("木斧", TOOL_WOODEN_AXE));
inventory.push_back(Item("苹果", ITEM_APPLE, 0, 0, 3));
inventory.push_back(Item("木板", ITEM_PLANK, 0, 0, 10));
inventory.push_back(Item("木棍", ITEM_STICK, 0, 0, 5));
}
// 初始化任务
void initQuests() {
quests[1] = QUEST_IN_PROGRESS; // 初始任务:收集10个木头
quests[2] = QUEST_NOT_STARTED; // 任务2:建造庇护所
quests[3] = QUEST_NOT_STARTED; // 任务3:击败僵尸
}
// 核心属性操作
int getHealth() const { return health; }
int getHunger() const { return hunger; }
int getLevel() const { return level; }
bool isAlive() const { return health > 0; }
int getTotalDefense() const {
int def = 0;
if (helmet.type != ITEM_UNKNOWN) def += helmet.defense;
if (chestplate.type != ITEM_UNKNOWN) def += chestplate.defense;
if (leggings.type != ITEM_UNKNOWN) def += leggings.defense;
if (boots.type != ITEM_UNKNOWN) def += boots.defense;
return def;
}
// 补充:获取主手物品伤害(缺失的函数)
int getMainHandDamage() const {
return mainHand.damage > 0 ? mainHand.damage : 1; // 无武器时基础伤害1
}
// 补充:获取生存天数(缺失的公开函数)
int getDaysSurvived() const {
return daysSurvived;
}
// 受伤
void takeDamage(int damage) {
int def = getTotalDefense();
int actualDamage = max(1, damage - def);
health -= actualDamage;
if (health < 0) health = 0;
cout << username << " 受到 " << actualDamage << " 点伤害!生命值:" << health << "/" << maxHealth << endl;
}
// 治疗
void heal(int amount) {
health += amount;
if (health > maxHealth) health = maxHealth;
cout << "恢复 " << amount << " 点生命值!当前:" << health << "/" << maxHealth << endl;
}
// 进食
void eat(ItemType foodType) {
for (auto& item : inventory) {
if (item.type == foodType && item.isEdible && item.quantity > 0) {
// 恢复饥饿值
hunger += item.hungerRestore;
if (hunger > MAX_HUNGER) hunger = MAX_HUNGER;
// 恢复生命值(部分食物)
if (item.healthRestore > 0) heal(item.healthRestore);
// 增加饱和度
saturation += item.hungerRestore / 2;
if (saturation > MAX_SATURATION) saturation = MAX_SATURATION;
// 消耗食物
item.use();
cout << "你吃了一个 " << item.name << "!饥饿值:" << hunger << "/" << MAX_HUNGER << endl;
return;
}
}
cout << "没有该食物!" << endl;
}
// 饥饿值消耗
void consumeHunger() {
if (saturation > 0) {
saturation--;
} else {
hunger--;
if (hunger < 0) hunger = 0;
}
// 饥饿值为0时掉血
if (hunger <= 0) {
takeDamage(1);
cout << "你太饿了,生命值在下降!" << endl;
}
}
// 添加经验
void addExperience(int exp) {
experience += exp;
cout << "获得 " << exp << " 点经验!" << endl;
// 升级逻辑
while (experience >= EXP_PER_LEVEL * level) {
experience -= EXP_PER_LEVEL * level;
level++;
maxHealth += 2; // 升级增加生命值
heal(5); // 升级回血
cout << "恭喜升级到 " << level << " 级!最大生命值提升至 " << maxHealth << "!" << endl;
}
}
// 收集资源
void collectResource(ItemType type, int quantity = 1) {
// 检查工具
bool hasTool = (mainHand.type == TOOL_WOODEN_PICKAXE || mainHand.type == TOOL_WOODEN_AXE ||
mainHand.type == TOOL_IRON_PICKAXE || mainHand.type == TOOL_IRON_AXE);
// 工具影响收集效率
int actualQty = hasTool ? quantity : max(1, quantity / 2);
// 消耗工具耐久
if (hasTool) mainHand.use();
// 添加到背包
addItem(type, actualQty);
cout << "收集到 " << actualQty << " 个 " << itemTypeToString(type) << "!" << endl;
// 解锁成就
if (type == ITEM_WOOD) unlockAchievement(ACHIEVEMENT_FIRST_WOOD);
if (type == ITEM_STONE) unlockAchievement(ACHIEVEMENT_FIRST_STONE);
if (type == ITEM_IRON_ORE) unlockAchievement(ACHIEVEMENT_FIRST_IRON);
if (type == ITEM_DIAMOND_ORE) unlockAchievement(ACHIEVEMENT_FIRST_DIAMOND);
// 更新任务进度
updateQuests(type, actualQty);
}
// 添加物品到背包
void addItem(ItemType type, int quantity = 1) {
for (auto& item : inventory) {
if (item.type == type) {
int add = min(quantity, item.stackSize - item.quantity);
item.quantity += add;
quantity -= add;
if (quantity <= 0) return;
}
}
// 背包无该物品,新增
while (quantity > 0) {
Item newItem(itemTypeToString(type), type);
int add = min(quantity, newItem.stackSize);
newItem.quantity = add;
inventory.push_back(newItem);
quantity -= add;
}
}
// 装备物品
void equipItem(ItemType type) {
// 查找物品
int idx = -1;
for (int i = 0; i < inventory.size(); i++) {
if (inventory[i].type == type && inventory[i].quantity > 0) {
idx = i;
break;
}
}
if (idx == -1) {
cout << "背包中无该物品!" << endl;
return;
}
// 装备到对应栏位
Item toEquip = inventory[idx];
switch (type) {
case ARMOR_IRON_HELMET:
case ARMOR_DIAMOND_HELMET:
if (helmet.type != ITEM_UNKNOWN) addItem(helmet.type, 1);
helmet = toEquip;
break;
case ARMOR_IRON_CHESTPLATE:
case ARMOR_DIAMOND_CHESTPLATE:
if (chestplate.type != ITEM_UNKNOWN) addItem(chestplate.type, 1);
chestplate = toEquip;
break;
case TOOL_IRON_PICKAXE:
case WEAPON_IRON_SWORD:
if (mainHand.type != ITEM_UNKNOWN) addItem(mainHand.type, 1);
mainHand = toEquip;
break;
default:
cout << "该物品无法装备!" << endl;
return;
}
// 消耗背包物品
inventory[idx].quantity--;
cout << "成功装备 " << toEquip.name << "!" << endl;
}
// 解锁成就
void unlockAchievement(AchievementType ach) {
if (!achievements[ach]) {
achievements[ach] = true;
cout << "解锁成就:";
switch (ach) {
case ACHIEVEMENT_FIRST_WOOD: cout << "伐木累"; break;
case ACHIEVEMENT_FIRST_STONE: cout << "采石场"; break;
case ACHIEVEMENT_FIRST_IRON: cout << "钢铁意志"; break;
case ACHIEVEMENT_FIRST_DIAMOND: cout << "钻石大亨"; break;
case ACHIEVEMENT_BUILD_SHELTER: cout << "有家可归"; break;
case ACHIEVEMENT_DEFEAT_ZOMBIE: cout << "击败僵尸"; break;
case ACHIEVEMENT_DEFEAT_ENDER_DRAGON: cout << "屠龙勇士"; break;
default: break;
}
cout << "!" << endl;
addExperience(20); // 成就奖励经验
}
}
// 更新任务进度
void updateQuests(ItemType type, int quantity) {
// 任务1:收集10个木头
if (quests[1] == QUEST_IN_PROGRESS && type == ITEM_WOOD) {
static int woodCollected = 0;
woodCollected += quantity;
if (woodCollected >= 10) {
quests[1] = QUEST_COMPLETED;
cout << "任务完成:收集10个木头!奖励:20经验 + 5木板" << endl;
addExperience(20);
addItem(ITEM_PLANK, 5);
quests[2] = QUEST_IN_PROGRESS; // 解锁下一个任务
}
}
// 任务2:建造庇护所
if (quests[2] == QUEST_IN_PROGRESS && hasShelter) {
quests[2] = QUEST_COMPLETED;
cout << "任务完成:建造庇护所!奖励:50经验 + 1床" << endl;
addExperience(50);
addItem(BLOCK_BED, 1);
unlockAchievement(ACHIEVEMENT_BUILD_SHELTER);
}
}
// 建造庇护所
void buildShelter() {
// 检查材料:10木板 + 5木头
int plank = 0, wood = 0;
for (const auto& item : inventory) {
if (item.type == ITEM_PLANK) plank += item.quantity;
if (item.type == ITEM_WOOD) wood += item.quantity;
}
if (plank >= 10 && wood >= 5) {
// 消耗材料
for (auto& item : inventory) {
if (item.type == ITEM_PLANK) { item.quantity -= 10; break; }
}
for (auto& item : inventory) {
if (item.type == ITEM_WOOD) { item.quantity -= 5; break; }
}
hasShelter = true;
cout << "成功建造庇护所!夜晚不再被怪物攻击!" << endl;
updateQuests(ITEM_UNKNOWN, 0); // 触发任务更新
} else {
cout << "建造庇护所需要:10木板 + 5木头!当前:" << plank << "木板 + " << wood << "木头" << endl;
}
}
// 显示玩家状态
void showStatus() const {
cout << "\n=== " << username << " 状态 ===" << endl;
cout << "生命值:" << health << "/" << maxHealth << endl;
cout << "饥饿值:" << hunger << "/" << MAX_HUNGER << endl;
cout << "等级:" << level << " 经验:" << experience << "/" << EXP_PER_LEVEL * level << endl;
cout << "生存天数:" << daysSurvived << endl;
cout << "庇护所:" << (hasShelter ? "有" : "无") << endl;
cout << "防御值:" << getTotalDefense() << endl;
// 显示装备
cout << "\n=== 装备栏 ===" << endl;
cout << "头盔:" << (helmet.type != ITEM_UNKNOWN ? helmet.name : "无") << endl;
cout << "胸甲:" << (chestplate.type != ITEM_UNKNOWN ? chestplate.name : "无") << endl;
cout << "主手:" << (mainHand.type != ITEM_UNKNOWN ? mainHand.name : "无") << endl;
// 显示成就
cout << "\n=== 已解锁成就 ===" << endl;
bool hasAch = false;
for (int i = 0; i < ACHIEVEMENT_MAX; i++) {
if (achievements[i]) {
hasAch = true;
switch (i) {
case ACHIEVEMENT_FIRST_WOOD: cout << "✓ 伐木累" << endl; break;
case ACHIEVEMENT_FIRST_STONE: cout << "✓ 采石场" << endl; break;
case ACHIEVEMENT_FIRST_IRON: cout << "✓ 钢铁意志" << endl; break;
case ACHIEVEMENT_FIRST_DIAMOND: cout << "✓ 钻石大亨" << endl; break;
case ACHIEVEMENT_BUILD_SHELTER: cout << "✓ 有家可归" << endl; break;
case ACHIEVEMENT_DEFEAT_ZOMBIE: cout << "✓ 击败僵尸" << endl; break;
case ACHIEVEMENT_DEFEAT_ENDER_DRAGON: cout << "✓ 屠龙勇士" << endl; break;
default: break;
}
}
}
if (!hasAch) cout << "暂无解锁成就" << endl;
// 显示任务
cout << "\n=== 任务进度 ===" << endl;
for (const auto& q : quests) {
cout << "任务" << q.first << ":";
switch (q.second) {
case QUEST_IN_PROGRESS: cout << "进行中"; break;
case QUEST_COMPLETED: cout << "已完成"; break;
default: cout << "未开始"; break;
}
if (q.first == 1) cout << "(收集10个木头)";
if (q.first == 2) cout << "(建造庇护所)";
if (q.first == 3) cout << "(击败僵尸)";
cout << endl;
}
}
// 显示背包
void showInventory() const {
cout << "\n=== 背包物品 ===" << endl;
if (inventory.empty()) {
cout << "背包为空!" << endl;
return;
}
for (const auto& item : inventory) {
if (item.quantity > 0) item.showInfo();
}
}
// 生存天数+1
void addDay() { daysSurvived++; }
// 夜晚安全检查(庇护所)
bool isSafeAtNight() const { return hasShelter; }
// 存档
void saveGame(const string& filename) const {
ofstream file(filename);
if (!file) {
cout << "存档失败!" << endl;
return;
}
// 保存基础信息
file << username << endl;
file << health << " " << maxHealth << endl;
file << hunger << " " << saturation << endl;
file << experience << " " << level << endl;
file << daysSurvived << endl;
file << hasShelter << endl;
// 保存背包
file << inventory.size() << endl;
for (const auto& item : inventory) {
file << (int)item.type << " " << item.quantity << " " << item.durability << endl;
}
file.close();
cout << "存档成功!文件:" << filename << endl;
}
// 读档
bool loadGame(const string& filename) {
ifstream file(filename);
if (!file) {
cout << "读档失败!文件不存在!" << endl;
return false;
}
// 读取基础信息
file >> username;
file >> health >> maxHealth;
file >> hunger >> saturation;
file >> experience >> level;
file >> daysSurvived;
file >> hasShelter;
// 读取背包
int invSize;
file >> invSize;
inventory.clear();
for (int i = 0; i < invSize; i++) {
int type, qty, dur;
file >> type >> qty >> dur;
Item item(itemTypeToString((ItemType)type), (ItemType)type);
item.quantity = qty;
item.durability = dur;
inventory.push_back(item);
}
file.close();
cout << "读档成功!欢迎回来," << username << "!" << endl;
return true;
}
};
// ===================== 生物类 =====================
class Entity {
private:
EntityType type; // 生物类型
int health; // 生命值
int maxHealth; // 最大生命值
int damage; // 伤害值
bool isHostile; // 是否敌对
bool isDead; // 是否死亡
public:
Entity(EntityType t) : type(t), isDead(false) {
// 初始化生物属性
switch (type) {
case ENTITY_COW:
maxHealth = 10; damage = 0; isHostile = false; break;
case ENTITY_ZOMBIE:
maxHealth = 20; damage = 3; isHostile = true; break;
case ENTITY_SKELETON:
maxHealth = 20; damage = 4; isHostile = true; break;
case ENTITY_CREEPER:
maxHealth = 20; damage = 6; isHostile = true; break;
case ENTITY_ENDER_DRAGON:
maxHealth = 200; damage = 10; isHostile = true; break;
default:
maxHealth = 10; damage = 0; isHostile = false; break;
}
health = maxHealth;
}
// 受到攻击
void takeDamage(int damage) {
health -= damage;
if (health <= 0) {
health = 0;
isDead = true;
cout << entityTypeToString(type) << " 被击败了!" << endl;
} else {
cout << entityTypeToString(type) << " 受到 " << damage << " 点伤害!剩余生命值:" << health << endl;
}
}
// 攻击玩家
void attack(Player& player) {
if (isDead || !isHostile) return;
player.takeDamage(damage);
cout << entityTypeToString(type) << " 攻击了你!" << endl;
}
// 掉落物品
void dropLoot(Player& player) {
if (!isDead) return;
switch (type) {
case ENTITY_COW:
player.addItem(ITEM_RAW_BEEF, randomInt(1, 2));
player.addItem(ITEM_LEATHER, randomInt(0, 1));
cout << "获得:生牛肉 x" << (randomInt(1,2)) << "、皮革 x" << (randomInt(0,1)) << endl;
break;
case ENTITY_ZOMBIE:
player.addItem(ITEM_ROTTEN_FLESH, randomInt(1, 2)); // 修复:已定义ITEM_ROTTEN_FLESH
if (randomInt(1, 10) == 1) player.addItem(ITEM_IRON_INGOT, 1);
cout << "获得:腐肉 x" << (randomInt(1,2)) << endl;
player.unlockAchievement(ACHIEVEMENT_DEFEAT_ZOMBIE);
break;
case ENTITY_CREEPER:
player.addItem(ITEM_GUNPOWDER, randomInt(1, 2)); // 修复:已定义ITEM_GUNPOWDER
cout << "获得:火药 x" << (randomInt(1,2)) << endl;
break;
case ENTITY_ENDER_DRAGON:
player.addItem(ITEM_DRAGON_EGG, 1); // 修复:已定义ITEM_DRAGON_EGG
player.addItem(ITEM_DIAMOND, randomInt(3, 5));
cout << "恭喜击败末影龙!获得:龙蛋 x1、钻石 x" << (randomInt(3,5)) << endl;
player.unlockAchievement(ACHIEVEMENT_DEFEAT_ENDER_DRAGON);
break;
default:
player.addItem(ITEM_UNKNOWN, 1);
break;
}
player.addExperience(randomInt(10, 50));
}
// 获取状态
bool isHostileEntity() const { return isHostile; }
bool isDeadEntity() const { return isDead; }
EntityType getType() const { return type; }
};
// ===================== 世界类 =====================
class World {
private:
int day; // 当前天数
bool isNight; // 是否夜晚
int timeCounter; // 时间计数器
WeatherType weather; // 当前天气
BiomeType currentBiome; // 当前生物群系
vector<Entity> entities; // 当前生物列表
public:
World() : day(1), isNight(false), timeCounter(0), weather(WEATHER_CLEAR) {
// 初始化生物群系
currentBiome = (BiomeType)randomInt(0, 5);
// 生成初始生物
spawnEntities();
}
// 生成生物
void spawnEntities() {
entities.clear();
int entityCount = randomInt(2, 5);
for (int i = 0; i < entityCount; i++) {
if (isNight) {
// 夜晚生成敌对生物
EntityType hostileType = (EntityType)randomInt(ENTITY_ZOMBIE, ENTITY_CREEPER);
entities.push_back(Entity(hostileType));
} else {
// 白天生成被动生物
EntityType passiveType = (EntityType)randomInt(ENTITY_COW, ENTITY_SHEEP);
entities.push_back(Entity(passiveType));
}
}
cout << "当前生物群系:" << biomeTypeToString(currentBiome) << endl; // 修复:已实现biomeTypeToString
cout << "生成生物:" << entityCount << " 个" << endl;
}
// 切换昼夜
void toggleDayNight() {
isNight = !isNight;
timeCounter = 0;
if (isNight) {
cout << "\n=== 夜晚降临 ===" << endl;
cout << "敌对生物开始生成!小心!" << endl;
} else {
day++;
cout << "\n=== 新的一天 ===" << endl;
cout << "当前天数:" << day << endl;
}
spawnEntities();
}
// 更新时间
void updateTime() {
timeCounter++;
int duration = isNight ? NIGHT_DURATION : DAY_DURATION;
if (timeCounter >= duration) {
toggleDayNight();
}
// 随机切换天气
if (randomInt(1, 20) == 1) {
weather = (WeatherType)randomInt(0, 3);
cout << "天气变化:" << weatherTypeToString(weather) << endl; // 修复:已实现weatherTypeToString
}
}
// 探索世界
void explore(Player& player) {
cout << "\n你开始探索 " << biomeTypeToString(currentBiome) << "..." << endl; // 修复:已实现biomeTypeToString
wait(500);
// 不同生物群系资源不同
switch (currentBiome) {
case BIOME_FOREST:
player.collectResource(ITEM_WOOD, randomInt(2, 4));
if (randomInt(1, 5) == 1) player.collectResource(ITEM_APPLE, 1);
break;
case BIOME_MOUNTAINS:
player.collectResource(ITEM_STONE, randomInt(2, 4));
if (randomInt(1, 10) == 1) player.collectResource(ITEM_IRON_ORE, 1);
break;
case BIOME_DESERT:
player.collectResource(ITEM_SAND, randomInt(3, 5));
if (randomInt(1, 15) == 1) player.collectResource(ITEM_GOLD_ORE, 1);
break;
case BIOME_JUNGLE:
player.collectResource(ITEM_WOOD, randomInt(3, 5));
player.collectResource(ITEM_MELON, randomInt(1, 2));
break;
default:
player.collectResource(ITEM_WOOD, randomInt(1, 3));
break;
}
// 随机遇到生物
if (!entities.empty()) {
int idx = randomInt(0, entities.size() - 1);
Entity& entity = entities[idx];
cout << "你遇到了 " << entityTypeToString(entity.getType()) << "!" << endl;
if (entity.isHostileEntity() && isNight && !player.isSafeAtNight()) {
// 敌对生物攻击
entity.attack(player);
// 玩家反击
cout << "你发起反击!" << endl;
int playerDamage = player.getMainHandDamage(); // 修复:已实现getMainHandDamage
entity.takeDamage(playerDamage);
if (entity.isDeadEntity()) {
entity.dropLoot(player);
entities.erase(entities.begin() + idx);
}
} else if (!entity.isHostileEntity()) {
// 被动生物,可选择攻击
cout << "是否攻击?(1-是/2-否):";
int choice;
cin >> choice;
if (choice == 1) {
int playerDamage = player.getMainHandDamage(); // 修复:已实现getMainHandDamage
entity.takeDamage(playerDamage);
if (entity.isDeadEntity()) {
entity.dropLoot(player);
entities.erase(entities.begin() + idx);
}
} else {
cout << "你绕开了 " << entityTypeToString(entity.getType()) << endl;
}
}
}
// 探索消耗饥饿值
player.consumeHunger();
updateTime();
player.addDay();
}
// 显示世界状态
void showWorldStatus() const {
cout << "\n=== 世界状态 ===" << endl;
cout << "当前天数:" << day << endl;
cout << "时间:" << (isNight ? "夜晚" : "白天") << endl;
cout << "天气:" << weatherTypeToString(weather) << endl; // 修复:已实现weatherTypeToString
cout << "生物群系:" << biomeTypeToString(currentBiome) << endl; // 修复:已实现biomeTypeToString
cout << "当前生物数量:" << entities.size() << endl;
}
// 夜晚事件
void nightEvent(Player& player) {
if (!isNight) return;
if (!player.isSafeAtNight()) {
// 无庇护所,随机遇到怪物
if (randomInt(1, 3) == 1) {
Entity zombie(ENTITY_ZOMBIE);
zombie.attack(player);
// 玩家反击
int playerDamage = player.getMainHandDamage(); // 修复:已实现getMainHandDamage
zombie.takeDamage(playerDamage);
if (zombie.isDeadEntity()) {
zombie.dropLoot(player);
}
}
} else {
cout << "你在庇护所中安全度过夜晚!" << endl;
}
}
};
// ===================== 游戏主类 =====================
class MinecraftGame {
private:
Player player; // 玩家
World world; // 世界
ItemManager itemManager; // 物品管理器
bool isRunning; // 游戏运行状态
public:
MinecraftGame(string username = "玩家") : player(username), isRunning(true) {
srand((unsigned int)time(0)); // 初始化随机数
cout << "欢迎来到我的世界生存游戏!" << endl;
cout << "你的目标是生存下去,收集资源,击败末影龙!" << endl;
}
// 显示主菜单
void showMainMenu() const {
clearScreen();
cout << "\n========== 我的世界生存游戏 ==========" << endl;
cout << "1. 收集资源" << endl;
cout << "2. 探索世界" << endl;
cout << "3. 制作物品" << endl;
cout << "4. 烧制物品" << endl;
cout << "5. 建造庇护所" << endl;
cout << "6. 装备物品" << endl;
cout << "7. 吃食物" << endl;
cout << "8. 查看状态" << endl;
cout << "9. 查看背包" << endl;
cout << "10. 查看配方" << endl;
cout << "11. 存档/读档" << endl;
cout << "12. 退出游戏" << endl;
cout << "=====================================" << endl;
cout << "请选择操作 (1-12):";
}
// 制作物品菜单
void craftMenu() {
cout << "\n=== 制作物品 ===" << endl;
itemManager.showAllRecipes();
cout << "\n请选择要制作的物品(输入制作配方序号,0返回):";
int choice;
cin >> choice;
if (choice == 0) return;
// 查找对应配方
vector<CraftingRecipe> recipes = itemManager.getCraftingRecipes(); // 修复:已实现getCraftingRecipes
if (choice < 1 || choice > recipes.size()) {
cout << "无效选择!" << endl;
return;
}
ItemType type = recipes[choice-1].result.type;
itemManager.craftItem(type, player.inventory);
}
// 烧制物品菜单
void smeltMenu() {
cout << "\n=== 烧制物品 ===" << endl;
cout << "可烧制物品:" << endl;
cout << "1. 铁矿石 → 铁锭" << endl;
cout << "2. 生牛肉 → 熟牛肉" << endl;
cout << "3. 沙子 → 玻璃" << endl;
cout << "请选择(0返回):";
int choice;
cin >> choice;
switch (choice) {
case 1: itemManager.smeltItem(ITEM_IRON_ORE, player.inventory); break;
case 2: itemManager.smeltItem(ITEM_RAW_BEEF, player.inventory); break;
case 3: itemManager.smeltItem(ITEM_SAND, player.inventory); break;
case 0: return;
default: cout << "无效选择!" << endl;
}
}
// 装备物品菜单
void equipMenu() {
cout << "\n=== 装备物品 ===" << endl;
cout << "请选择要装备的物品类型:" << endl;
cout << "1. 铁头盔" << endl;
cout << "2. 铁胸甲" << endl;
cout << "3. 铁镐" << endl;
cout << "4. 铁剑" << endl;
cout << "0. 返回" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: player.equipItem(ARMOR_IRON_HELMET); break;
case 2: player.equipItem(ARMOR_IRON_CHESTPLATE); break;
case 3: player.equipItem(TOOL_IRON_PICKAXE); break;
case 4: player.equipItem(WEAPON_IRON_SWORD); break;
case 0: return;
default: cout << "无效选择!" << endl;
}
}
// 吃食物菜单
void eatMenu() {
cout << "\n=== 吃食物 ===" << endl;
cout << "可食用物品:" << endl;
cout << "1. 苹果" << endl;
cout << "2. 熟牛肉" << endl;
cout << "0. 返回" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1: player.eat(ITEM_APPLE); break;
case 2: player.eat(ITEM_COOKED_BEEF); break;
case 0: return;
default: cout << "无效选择!" << endl;
}
}
// 存档读档菜单
void saveLoadMenu() {
cout << "\n=== 存档/读档 ===" << endl;
cout << "1. 存档" << endl;
cout << "2. 读档" << endl;
cout << "0. 返回" << endl;
int choice;
cin >> choice;
string filename = "savegame.txt";
switch (choice) {
case 1: player.saveGame(filename); break;
case 2: player.loadGame(filename); break;
case 0: return;
default: cout << "无效选择!" << endl;
}
}
// 运行游戏
void run() {
while (isRunning) {
showMainMenu();
int choice;
cin >> choice;
switch (choice) {
case 1: // 收集资源
player.collectResource((ItemType)randomInt(ITEM_WOOD, ITEM_DIAMOND_ORE), randomInt(1, 3));
player.consumeHunger();
break;
case 2: // 探索世界
world.explore(player);
break;
case 3: // 制作物品
craftMenu();
break;
case 4: // 烧制物品
smeltMenu();
break;
case 5: // 建造庇护所
player.buildShelter();
break;
case 6: // 装备物品
equipMenu();
break;
case 7: // 吃食物
eatMenu();
break;
case 8: // 查看状态
player.showStatus();
world.showWorldStatus();
break;
case 9: // 查看背包
player.showInventory();
break;
case 10: // 查看配方
itemManager.showAllRecipes();
break;
case 11: // 存档读档
saveLoadMenu();
break;
case 12: // 退出游戏
isRunning = false;
cout << "感谢游玩!再见!" << endl;
break;
default:
cout << "无效选择!请输入1-12之间的数字。" << endl;
}
// 检查游戏结束
if (!player.isAlive()) {
cout << "\n=== 游戏结束 ===" << endl;
cout << "你生存了 " << player.getDaysSurvived() << " 天!" << endl; // 修复:已实现getDaysSurvived
isRunning = false;
}
// 夜晚事件
world.nightEvent(player);
// 暂停一下,便于阅读
if (isRunning) {
cout << "\n按回车键继续...";
cin.ignore();
cin.get();
}
}
}
};
// ===================== 主函数 =====================
int main() {
SetConsoleOutputCP(65001); // 加上这一行
SetConsoleCP(65001); // 再加这一行
cout << "请输入你的用户名:";
string username;
cin >> username;
MinecraftGame game(username);
game.run();
return 0;
}
全部评论 4
顶
1小时前 来自 北京
01
1小时前 来自 浙江
01
1
1
11小时前 来自 浙江
0193行修改为this_threadsleep_for(chronomilliseconds(ms));
4天前 来自 浙江
0直接复制就行
4天前 来自 浙江
0



























有帮助,赞一个