2
2026-08-16 14:56:48
发布于:浙江
// 撤离点
void evacuation_point() {
stdcout << "\n----- 撤离点 -----\n";
stdcout << "你可以选择在此撤离,获得当前所有收益;或继续探索。\n";
stdcout << "当前收益:\n";
stdcout << "金钱:" << player.money << "\n";
stdcout << "变卖物价值:" << player.collectibles_value << "\n";
stdcout << "总收益:" << player.money + player.collectibles_value << "\n";
stdcout << "\n1. 撤离(胜利)\n";
stdcout << "2. 继续探索\n";
std::cout << "请选择: ";
int choice;
std::cin >> choice;
if (choice == 1) {
current_state = GameState::VICTORY;
} else {
std::cout << "你决定继续探索,寻找更多高价值物品!\n";
press_any_key();
}
}
// 根据总收益获取撤离评价
std::string get_evac_evaluation(long long total_earn)
{
if (total_earn >= 8000000) {
SetColor(4,0);
return "传奇收获!你带出了天文数字的物资,行动载入史册!";
}
else if (total_earn >= 5000000) {
SetColor(6,0);
return "史诗收获!巨额战利品,所有人都会记住你的战绩!";
}
else if (total_earn >= 2000000) {
SetColor(5,0);
return "优秀收获,高价值物资丰厚,是一次成功的行动。";
}
else if (total_earn >= 800000) {
SetColor(3,0);
return "不错的收获,小有盈利,平稳完成任务。";
}
else if (total_earn >= 200000){
SetColor(10,0);
return "基础收益,勉强回本,还有很大提升空间。";
}
else if (total_earn >= 100000) {
SetColor(8,0);
return "微薄收益,物资稀少,下次尽量搜寻更多容器。";
}
else {
SetColor(8,0);
return "空手而归,没有带出任何有价值的东西。";
}
}
// 丰富黑市商品,每次进入商店随机刷新商品(替换原有generate_shop_items)
void generate_shop_items() {
// 清空现有商品
for (Item* item : shop_items) {
delete item;
}
shop_items.clear();
std::vector<Item*> all_goods_pool;
// 消耗品池
all_goods_pool.push_back(new Consumable("高级急救包", "恢复50点生命值", 50, 0, 50000, 2));
all_goods_pool.push_back(new Consumable("止痛药", "恢复20点生命值并获得临时防御", 20, 1, 10000, 1));
all_goods_pool.push_back(new Consumable("肾上腺素", "恢复10点生命值并获得临时暴击", 10, 2, 12000, 1));
all_goods_pool.push_back(new Consumable("绷带", "恢复15点生命值", 15, 0, 8000, 1));
all_goods_pool.push_back(new Consumable("医疗喷雾", "恢复40点生命值", 40, 0, 45000, 1));
// 弹药池
all_goods_pool.push_back(new Ammo("手枪子弹(大量)", "适用于大多数手枪", AmmoType::PISTOL, 50, 2000, 2));
all_goods_pool.push_back(new Ammo("步枪子弹(大量)", "适用于大多数步枪", AmmoType::RIFLE, 50, 3000, 3));
all_goods_pool.push_back(new Ammo("特殊弹药", "适用于特殊武器", AmmoType::SPECIAL, 20, 5000, 2));
all_goods_pool.push_back(new Ammo("狙击专用弹药", "高精度狙击弹", AmmoType::RIFLE, 20, 8000, 2));
// 防具池
all_goods_pool.push_back(new HeadArmor("轻型头盔", "基础头部防护", 3, 5, 2, 50000 + rand() % 10000, 2));
all_goods_pool.push_back(new BodyArmor("轻型防弹衣", "基础身体防护", 8, 3, 5, 100000 + rand() % 20000, 4));
all_goods_pool.push_back(new HeadArmor("重型头盔", "强大的头部防护", 5, 0, 5, 100000 + rand() % 20000, 3));
all_goods_pool.push_back(new BodyArmor("重型防弹衣", "强大的身体防护", 12, 0, 10, 200000 + rand() % 30000, 6));
all_goods_pool.push_back(new HeadArmor("战术头盔", "战术头部防护", 4, 4, 2, 80000 + rand() % 10000, 3));
all_goods_pool.push_back(new BodyArmor("战术防弹衣", "战术身体防护", 9, 3, 7, 170000 + rand() % 20000, 5));
all_goods_pool.push_back(new HeadArmor("特种头盔", "凯夫拉纤维制成", 4, 4, 15, 150000 + rand() % 20000, 3));
all_goods_pool.push_back(new BodyArmor("特种防弹衣", "凯夫拉纤维制成", 10, 3, 25, 250000 + rand() % 20000, 5));
// 背包池
all_goods_pool.push_back(new Backpack("中型背包", "容量更大的背包", 18, 50000 + rand() % 10000, 4));
all_goods_pool.push_back(new Backpack("大型背包", "容量超大的背包", 25, 100000 + rand() % 20000, 6));
all_goods_pool.push_back(new Backpack("巨型背包", "人体最大承载量!", 30, 250000 + rand() % 50000, 8));
// 武器池
all_goods_pool.push_back(new Weapon("改良手枪", "威力提升的手枪", 15, 15, 70, WeaponType::PISTOL, AmmoType::PISTOL, 20, 30000 + rand() % 10000, 2));
all_goods_pool.push_back(new Weapon("改装MP5", "稳定冲锋枪", 19, 22, 70, WeaponType::RIFLE, AmmoType::RIFLE, 30, 95000 + rand() % 20000, 3));
all_goods_pool.push_back(new Weapon("猎枪", "近距离威力巨大", 32, 40, 70, WeaponType::RIFLE, AmmoType::RIFLE, 8, 120000 + rand() % 20000, 3));
// 随机挑选12~16件商品上架黑市
int goods_count = 12 + rand() % 5;
while (!all_goods_pool.empty() && (int)shop_items.size() < goods_count)
{
int random_idx = rand() % all_goods_pool.size();
Item* select_item = all_goods_pool[random_idx];
shop_items.push_back(select_item);
all_goods_pool.erase(all_goods_pool.begin() + random_idx);
}
// 删除未被选中的商品,防止内存泄漏
for (Item* remain : all_goods_pool)
{
delete remain;
}
}
// 显示商店
void show_shop() {
clear_screen();
stdcout << "----- 黑市商店 -----\n";
stdcout << "你的金钱:" << player.money << "\n";
stdcout << "\n商品列表:\n";
if (shop_items.empty()) {
generate_shop_items();
}
// 显示商品
for (size_t i = 0; i < shop_items.size(); ++i) {
Item* item = shop_items[i];
stdcout << i+1 << ". " << item->name << " - " << item->description << " (价格:" << item->value << ")\n";
}
std::cout << "\n操作选项:\n";
std::cout << "0. 离开商店\n";
std::cout << "99. 变卖物品\n";
std::cout << "请选择商品序号购买,或0离开,99变卖物品:";
}
// 处理商店输入
void handle_shop_input() {
int choice;
std::cin >> choice;
if (choice == 0) {
std::cout << "你离开了黑市商店。\n";
press_any_key();
current_state = GameState::EXPLORING;
return;
} else if (choice == 99) {
// 变卖物品
clear_screen();
std::cout << "----- 变卖物品 -----\n";
std::cout << "你的金钱:" << player.money << "\n";
std::cout << "\n可变卖的物品:\n";
std::vector<Item*> sellable;
for (Item* item : player.inventory) {
// 除了装备中的物品,其他都可变卖
bool is_equipped = (item->type == ItemType::WEAPON &&
(item == player.main_weapon || item == player.secondary_weapon ||
item == player.pistol_weapon || item == player.knife_weapon)) ||
(item->type == ItemType::ARMOR_HEAD && item == player.equipped_head) ||
(item->type == ItemType::ARMOR_BODY && item == player.equipped_body) ||
(item->type == ItemType::BACKPACK && item == player.equipped_backpack);
if (!is_equipped) {
sellable.push_back(item);
}
}
if (sellable.empty()) {
std::cout << "没有可变卖的物品!\n";
press_any_key();
return;
}
for (size_t i = 0; i < sellable.size(); ++i) {
Item* item = sellable[i];
std::cout << i+1 << ". " << item->name << " (价值:" << item->value << ")\n";
}
std::cout << "\n0. 取消\n";
std::cout << "请选择要变卖的物品序号:";
int sell_choice;
std::cin >> sell_choice;
if (sell_choice > 0 && sell_choice <= (int)sellable.size()) {
player.sell_item(sellable[sell_choice-1]);
} else if (sell_choice != 0) {
std::cout << "无效的选择!\n";
}
press_any_key();
return;
} else if (choice > 0 && choice <= (int)shop_items.size()) {
Item* item = shop_items[choice-1];
if (player.money >= item->value) {
// 尝试添加物品到背包
Item* new_item = nullptr;
if (item->type == ItemType::CONSUMABLE) {
Consumable* c = dynamic_cast<Consumable*>(item);
new_item = new Consumable(c->name, c->description, c->health_restore, c->status_effect, c->value, c->size);
} else if (item->type == ItemType::AMMO) {
Ammo* a = dynamic_cast<Ammo*>(item);
new_item = new Ammo(a->name, a->description, a->ammo_type, a->quantity, a->value, a->size);
} else if (item->type == ItemType::WEAPON) {
Weapon* w = dynamic_cast<Weapon*>(item);
new_item = new Weapon(w->name, w->description, w->damage, w->crit_rate, w->hit_rate, w->weapon_type, w->ammo_type, w->max_ammo, w->value, w->size);
} else if (item->type == ItemType::ARMOR_HEAD) {
HeadArmor* h = dynamic_cast<HeadArmor*>(item);
new_item = new HeadArmor(h->name, h->description, h->defense, h->dodge_rate, h->damage_reduction, h->value, h->size);
} else if (item->type == ItemType::ARMOR_BODY) {
BodyArmor* b = dynamic_cast<BodyArmor*>(item);
new_item = new BodyArmor(b->name, b->description, b->defense, b->dodge_rate, b->damage_reduction, b->value, b->size);
} else if (item->type == ItemType::BACKPACK) {
Backpack* bp = dynamic_cast<Backpack*>(item);
new_item = new Backpack(bp->name, bp->description, bp->capacity, bp->value, bp->size);
}
if (new_item && player.add_item(new_item)) {
player.money -= item->value;
std::cout << "你购买了 " << item->name << "!\n";
} else {
delete new_item;
std::cout << "无法购买该物品(背包空间不足)!\n";
}
} else {
std::cout << "金钱不足,无法购买!\n";
}
press_any_key();
} else {
std::cout << "无效的选择!\n";
press_any_key();
}
}
// 游戏结束
void game_over(bool victory) {
if (victory) {
//新增:胜利撤离,把红色变卖物存入本地仓库
SetColor(6,0);
stdcout << "\n===== 任务成功!=====\n";
SetColor(7,0);
Sleep(500);
stdcout << "你成功撤离,获得了总计 " << player.money + player.collectibles_value << " 的收益!\n";
Sleep(500);
stdcout << "行动评价:" << get_evac_evaluation(player.money + player.collectibles_value) << "\n";
Sleep(500);
SetColor(7,0);
store_player_red_to_vault();
} else {
stdcout << "\n===== 任务失败!=====\n";
Sleep(100);
stdcout << "你在战斗中倒下了,任务失败。\n";
Sleep(100);
stdcout << "行动评价:" << "时运不济" << "\n";
Sleep(100);
}
std::cout << "感谢游玩!\n";
}
// 清屏(跨平台简易实现)
void clear_screen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// 按任意键继续
void press_any_key() {
Sleep(100);
stdcout << "\n按任意键继续...";
stdcin.clear();
stdcin.ignore(stdnumeric_limitsstd::streamsizemax(), '\n');
(void)_getch(); // 真正读取键盘按键,不会被换行干扰
}
//void clear_save_file(const stdstring& filename) {
// // 方式 A:直接删除文件(最彻底)
// // stdremove(filename.c_str());
//
// // 方式 B:将文件清空为 0 字节(保留空文件,不容易因找不到文件报错)
// stdofstream outfile(filename, stdiostrunc);
//
// if (outfile.is_open()) {
// outfile.close();
// ShowCombatText("存档已成功清空!重新开始你的冒险吧。\n", 10); // 绿色提示
// } else {
// ShowCombatText("错误:未能成功清空存档。\n", 12); // 红色提示
// }
//}
// 主函数
int main() {
srand(time(0));
red_vault.load_from_file(VAULT_FILE);
player.health = 2000000000;
player.max_health = 2000000000;
player.money = 2000000000;
player.level = 1000;
player.exp = 9999999;
bool quit_game = false;
while (!quit_game) // 外层循环:反复玩多局
{
player.health = 2000000000;
player.max_health = 2000000000;
player.money = 2000000000;
player.level = 1000;
player.exp = 9999999;
current_state = GameState::MAIN_MENU;
game_loop(); // 运行完整一局游戏,结束后current_state是GAME_OVER/VICTORY
// ==========一局结束后处理==========
std::cout << "\n本局已结束,按任意键返回主菜单...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
cleanup_game_session();
// 回到主菜单状态
current_state = GameState::MAIN_MENU;
}
// clear_save_file(VAULT_FILE);
return 0;
}
这里空空如也




















有帮助,赞一个