Game

· · 个人记录

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <iomanip>
#include <cstring>
#include <algorithm>
using namespace std;

#define COLOR_WHITE 7
#define COLOR_RED 12
#define COLOR_GREEN 10
#define COLOR_BLUE 9
#define COLOR_YELLOW 14
#define COLOR_PURPLE 13

void setColor(int color) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hConsole != INVALID_HANDLE_VALUE)
        SetConsoleTextAttribute(hConsole, color);
}

struct Food {
    string name;
    long long price;
    int count;
    int recover_hunger;
    int recover_hp;
};

struct Weapon {
    string name;
    long long price;
    int count;
    int base_damage;
    string ammo_type;
    int ammo_per_use;
    bool is_auto;
};

struct Enemy {
    string name;
    string desc;
    int min_hp;
    int max_hp;
    int min_atk;
    int max_atk;
    int exp_val;
    long long min_money;
    long long max_money;
};

struct Map {
    string name;
    string desc;
    int enemy_rate;
    int money_rate;
    bool is_unlocked;
    vector<Enemy> enemies;
    Enemy boss;
    bool has_boss;
};

struct Task {
    char id[20];
    string type;
    string title;
    string target;
    long long reward_money;
    int reward_hp;
    int reward_food;
    int reward_bullet;
    int reward_shell;
    bool is_completed;
    bool is_received;
};

// 全局变量
Food food;
vector<Weapon> weapon;
vector<Map> maps;
vector<Task> tasks;

int hp = 1000, hp_max = 1000;
int hunger = 0, hunger_max = 100;
long long money = 1000;
int day = 1;
string period = "上午";
string weather;
int current_map = 0;
string weathers[6] = {"晴天", "多云", "小雨", "风沙", "薄雾", "暴雨"};

int ammo_bullet = 300;
int ammo_shell = 20;
int current_weapon = 0;

// 等级系统(修复exp变量名冲突)
int level = 1;
int player_exp = 0;
int exp_need = 100;

// 渐变文字打印
void gradientPrint(string text, int color1 = COLOR_GREEN, int color2 = COLOR_YELLOW) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hConsole == INVALID_HANDLE_VALUE) { cout << text << endl; return; }
    int step = (color2 - color1) / max(1, (int)text.size());
    int current = color1;
    for (char c : text) {
        SetConsoleTextAttribute(hConsole, current);
        cout << c; Sleep(25);
        current = min(color2, max(color1, current + step));
    }
    SetConsoleTextAttribute(hConsole, COLOR_WHITE); cout << endl;
}

// 闪烁文字打印
void flashPrint(string text, int color = COLOR_RED, int bg = COLOR_WHITE) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hConsole == INVALID_HANDLE_VALUE) { cout << text << endl; return; }
    for (int i = 0; i < 2; i++) {
        SetConsoleTextAttribute(hConsole, color); cout << text << "\r"; Sleep(200);
        SetConsoleTextAttribute(hConsole, bg); cout << string(text.size(), ' ') << "\r"; Sleep(200);
    }
    SetConsoleTextAttribute(hConsole, COLOR_WHITE); cout << text << endl;
}

// 清屏
void clr() { system("cls"); }

// 类型转换
string llToStr(long long num) {
    char buf[30];
    sprintf(buf, "%lld", num);
    return buf;
}
string intToStr(int num) {
    char buf[20];
    sprintf(buf, "%d", num);
    return buf;
}

// 真实伤害计算(等级加成)
int getRealDamage(Weapon& w) {
    return w.base_damage + level * 5;
}

// 升级逻辑
void checkLevelUp() {
    bool up = false;
    while (player_exp >= exp_need) {
        player_exp -= exp_need;
        level++;
        exp_need = (int)(exp_need * 1.3); // 强制转int避免浮点问题
        hp_max += 100;
        hp = hp_max;
        up = true;
    }
    if (up) {
        gradientPrint("恭喜你升到 " + intToStr(level) + " 级!武器伤害提升!", COLOR_GREEN);
        system("pause");
    }
}

// 时间推进
void next_half() {
    if (period == "上午") period = "下午";
    else if (period == "下午") period = "晚上";
    else {
        period = "上午"; day++;
        hunger++;
        if (hunger >= hunger_max) {
            clr(); gradientPrint("你因饥饿倒下了!", COLOR_YELLOW, COLOR_RED);
            system("pause"); exit(0);
        }
        weather = weathers[rand()%6];
    }
}

// 任务完成检测
void checkTaskComplete() {
    for (auto& t : tasks) {
        if (t.is_completed || !t.is_received) continue;

        if (strcmp(t.id, "主线01") == 0 && day >= 3 && hunger <= 80) {
            t.is_completed = true;
            money += t.reward_money;
            hp = min(hp_max, hp + t.reward_hp);
            food.count += t.reward_food;
            ammo_bullet += t.reward_bullet;
            ammo_shell += t.reward_shell;
            maps[1].is_unlocked = true;

            clr(); gradientPrint("【任务完成】" + t.title, COLOR_GREEN);
            cout << "解锁:废弃都市废墟\n";
            system("pause");
        }
        if (strcmp(t.id, "主线02") == 0 && money >= 5000) {
            t.is_completed = true;
            money += t.reward_money;
            hp = min(hp_max, hp + t.reward_hp);
            food.count += t.reward_food;
            ammo_bullet += t.reward_bullet;
            ammo_shell += t.reward_shell;
            maps[2].is_unlocked = true;

            clr(); gradientPrint("【任务完成】" + t.title, COLOR_GREEN);
            cout << "解锁:军事要塞外围\n";
            system("pause");
        }
    }
}

// 初始化游戏数据
void init() {
    srand(time(0));
    weather = weathers[rand()%6];

    // 食物初始化
    food.name = "压缩干粮";
    food.price = 50;
    food.count = 8;
    food.recover_hunger = 20;
    food.recover_hp = 50;

    // 武器初始化
    weapon.push_back({"军用匕首",        80,    1,   60,  "无",    0, false});
    weapon.push_back({"弹弓",            10,    0,   25,  "无",    0, false});
    weapon.push_back({"弓箭",            30,    0,   40,  "无",    0, false});
    weapon.push_back({"M1911手枪",      500,    0,   80,  "子弹",  1, false});
    weapon.push_back({"AK-47",         1500,    0,  110,  "子弹",  1, true});
    weapon.push_back({"95式步枪",      1800,    0,  120,  "子弹",  1, true});
    weapon.push_back({"AWM狙击枪",     3200,    0,  250,  "子弹",  1, false});
    weapon.push_back({"M249机枪",      2600,    0,  140,  "子弹",  1, true});
    weapon.push_back({"火箭筒",         4000,    0,  350,  "炮弹",  1, false});
    weapon.push_back({"手雷",           120,    0,  200,  "无",    0, false});
    weapon.push_back({"烟幕弹",          80,    0,    0,  "无",    0, false});
    weapon.push_back({"TNT炸药",       5500,    0,  500,  "无",    0, false});
    weapon.push_back({"医疗包",         100,    0,    0,  "无",    0, false});
    weapon.push_back({"通用子弹1000发",  50,    0,    0,  "子弹",  0, false});
    weapon.push_back({"通用炮弹20发",   200,    0,    0,  "炮弹",  0, false});

    // 地图和敌人初始化
    vector<Enemy> wild = {
        {"流浪匪徒", "持铁棍歹徒", 180,250,25,40,20,80,150},
        {"野狗群", "疯狂扑咬", 120,200,20,35,15,50,120}
    };
    Enemy wild_boss = {"荒野屠夫", "手持巨斧的BOSS", 800,800, 80,80, 200, 800,1500};
    maps.push_back({"荒野荒原", "杂草丛生,废弃车辆遍地", 40,1,true,wild,wild_boss,true});

    vector<Enemy> city = {
        {"武装劫匪","持枪歹徒",300,400,40,60,40,200,350},
        {"废墟拾荒者","凶狠好斗",250,350,35,50,35,150,250}
    };
    Enemy city_boss = {"废墟霸主","全副武装的BOSS",1500,1500,120,120,350,1500,3000};
    maps.push_back({"废弃都市废墟","断壁残垣,危机四伏",70,2,false,city,city_boss,true});

    vector<Enemy> military = {
        {"重装守卫","护甲士兵",500,700,60,90,60,300,500},
        {"机械哨兵","自动机器人",600,800,70,100,70,400,600}
    };
    Enemy military_boss = {"机甲战神","终极防御机甲BOSS",3000,3000,200,200,500,3000,6000};
    maps.push_back({"军事要塞外围","重兵把守,极度危险",90,3,false,military,military_boss,true});

    // 任务初始化
    Task t1; strcpy(t1.id, "主线01");
    t1.type = "主线"; t1.title = "生存启蒙";
    t1.target = "存活3天,饥饿≤80";
    t1.reward_money = 500; t1.reward_hp = 200; t1.reward_food = 5;
    t1.reward_bullet = 500; t1.reward_shell = 20;
    t1.is_completed = false; t1.is_received = true;
    tasks.push_back(t1);

    Task t2; strcpy(t2.id, "主线02");
    t2.type = "主线"; t2.title = "积累财富";
    t2.target = "金钱≥5000";
    t2.reward_money = 1500; t2.reward_hp = 400; t2.reward_food = 10;
    t2.reward_bullet = 1000; t2.reward_shell = 40;
    t2.is_completed = false; t2.is_received = true;
    tasks.push_back(t2);
}

// 显示状态面板
void show_state() {
    cout << "===== 第" << day << "天 " << period << " | 天气:" << weather << " =====\n";
    cout << "地图:" << maps[current_map].name << "  遇敌:" << maps[current_map].enemy_rate << "%\n";
    cout << "生命:" << hp << "/" << hp_max << " 饥饿:" << hunger << "/" << hunger_max << "\n";
    cout << "金钱:" << money << "  等级:" << level << "  经验:" << player_exp << "/" << exp_need << "\n";
    cout << "子弹:" << ammo_bullet << "  炮弹:" << ammo_shell << "\n";
    cout << "武器:" << weapon[current_weapon].name
    << " 伤害:" << getRealDamage(weapon[current_weapon]);
    if (weapon[current_weapon].is_auto) cout << " 【连发】";
    cout << "\n=========================================\n";
}

// 欢迎界面
void welcome() {
    clr();
    cout << "=========================================\n";
    gradientPrint("      末日生存:荒野战线 BOSS版", COLOR_GREEN, COLOR_YELLOW);
    cout << "=========================================\n";
    flashPrint("            按任意键开始", COLOR_YELLOW);
    cin.ignore(); cin.get();
}

// 主菜单
void menu() {
    clr(); show_state();
    cout << "1.外出探索  2.睡觉      3.买食物    4.武器店\n";
    cout << "5.背包      6.遇敌战斗  7.换地图    8.任务\n";
    cout << "9.吃食物    10.医疗包   11.挑战BOSS\n";
    cout << "=========================================\n";
    cout << "请选择:";
}

// 普通战斗
void battle() {
    clr();
    Map& m = maps[current_map];
    Enemy e = m.enemies[rand() % m.enemies.size()];
    int e_hp = e.min_hp + rand() % (e.max_hp - e.min_hp + 1);
    int e_atk = e.min_atk + rand() % (e.max_atk - e.min_atk + 1);

    cout << "【环境】" << m.desc << " | 天气:" << weather << "\n";
    gradientPrint("遭遇:" + e.name, COLOR_YELLOW);
    cout << e.desc << "  血量:" << e_hp << "\n";
    system("pause");

    while (e_hp > 0 && hp > 0) {
        clr(); show_state();
        cout << "敌人:" << e.name << "  血量:" << e_hp << "\n";
        cout << "1.攻击  2.吃食物  3.医疗包  4.换枪  5.逃跑\n";
        int c; cin >> c;

        if (c == 1) {
            Weapon& w = weapon[current_weapon];
            if (w.count == 0 && w.ammo_type != "无") {
                flashPrint("无武器"); system("pause"); continue;
            }
            bool aok = true;
            if (w.ammo_type == "子弹" && ammo_bullet < w.ammo_per_use) aok = false;
            if (w.ammo_type == "炮弹" && ammo_shell < w.ammo_per_use) aok = false;
            if (!aok) {
                flashPrint("弹药不足"); system("pause"); continue;
            }

            int cnt = w.is_auto ? 3 : 1;
            int total = 0;
            for (int i = 0; i < cnt; i++) {
                if (e_hp <= 0 || hp <= 0) break;
                if (w.ammo_type == "子弹") ammo_bullet -= w.ammo_per_use;
                if (w.ammo_type == "炮弹") ammo_shell -= w.ammo_per_use;
                int d = getRealDamage(w) + rand() % 20;
                e_hp -= d; total += d;
                if (w.is_auto) {
                    setColor(COLOR_GREEN);
                    cout << w.name << " 连发!伤害:" << d << "\n";
                    setColor(COLOR_WHITE);
                }
            }
            if (w.is_auto) {
                gradientPrint("总计伤害:" + intToStr(total));
            } else {
                gradientPrint(w.name + " 打出 " + intToStr(total) + " 点伤害!");
            }
            system("pause");

            if (e_hp > 0) {
                hp -= e_atk;
                flashPrint(e.name + " 反击!受伤:" + intToStr(e_atk));
                system("pause");
            }
        }
        else if (c == 2) {
            if (food.count > 0) {
                hp = min(hp_max, hp + food.recover_hp);
                hunger = max(0, hunger - 5);
                food.count--;
                gradientPrint("食用干粮,生命恢复!");
            } else {
                flashPrint("无食物");
            }
            system("pause");
        }
        else if (c == 3) {
            for (auto& w : weapon) {
                if (w.name == "医疗包" && w.count > 0) {
                    hp = min(hp_max, hp + 300);
                    w.count--;
                    gradientPrint("医疗包使用!生命+300");
                    system("pause");
                    goto end3;
                }
            }
            flashPrint("无医疗包"); system("pause");
            end3:;
        }
        else if (c == 4) {
            clr(); cout << "【切换武器】\n";
            for (int i = 0; i < weapon.size(); i++) {
                if (weapon[i].count > 0 || weapon[i].ammo_type == "无") {
                    cout << i + 1 << ". " << weapon[i].name << " 伤害:" << getRealDamage(weapon[i]);
                    if (weapon[i].is_auto) cout << " 连发";
                    cout << "\n";
                }
            }
            cout << "0.返回\n选择:"; int w; cin >> w;
            if (w > 0 && w <= (int)weapon.size() && (weapon[w - 1].count > 0 || weapon[w - 1].ammo_type == "无")) {
                current_weapon = w - 1;
            }
            system("pause");
        }
        else if (c == 5) {
            flashPrint("成功逃跑!"); system("pause");
            next_half(); return;
        }

        if (hp <= 0) {
            clr(); flashPrint("你被击败了!"); system("pause"); exit(0);
        }
    }

    player_exp += e.exp_val;
    long long mon = e.min_money + rand() % (e.max_money - e.min_money + 1);
    money += mon;
    gradientPrint("胜利!获得经验:" + intToStr(e.exp_val) + " 金钱:" + llToStr(mon));
    checkLevelUp();
    next_half();
}

// BOSS战斗
void battleBoss() {
    clr();
    Map& m = maps[current_map];
    if (!m.has_boss) {
        flashPrint("该地图无BOSS"); system("pause"); return;
    }
    Enemy e = m.boss;
    int e_hp = e.min_hp;
    int e_atk = e.min_atk;

    gradientPrint("警告:进入BOSS战:" + e.name, COLOR_RED);
    cout << e.desc << "  BOSS血量:" << e_hp << "\n";
    system("pause");

    while (e_hp > 0 && hp > 0) {
        clr(); show_state();
        cout << "BOSS:" << e.name << "  血量:" << e_hp << "\n";
        cout << "1.猛攻  2.食物  3.医疗包  4.换枪  5.逃跑\n";
        int c; cin >> c;

        if (c == 1) {
            Weapon& w = weapon[current_weapon];
            if (w.count == 0 && w.ammo_type != "无") {
                flashPrint("无武器"); system("pause"); continue;
            }
            bool aok = true;
            if (w.ammo_type == "子弹" && ammo_bullet < w.ammo_per_use) aok = false;
            if (w.ammo_type == "炮弹" && ammo_shell < w.ammo_per_use) aok = false;
            if (!aok) {
                flashPrint("弹药不足"); system("pause"); continue;
            }

            int cnt = w.is_auto ? 3 : 1;
            int total = 0;
            for (int i = 0; i < cnt; i++) {
                if (e_hp <= 0 || hp <= 0) break;
                if (w.ammo_type == "子弹") ammo_bullet -= w.ammo_per_use;
                if (w.ammo_type == "炮弹") ammo_shell -= w.ammo_per_use;
                int d = getRealDamage(w) + rand() % 20;
                e_hp -= d; total += d;
                if (w.is_auto) {
                    setColor(COLOR_GREEN);
                    cout << w.name << " 连发!伤害:" << d << "\n";
                    setColor(COLOR_WHITE);
                }
            }
            if (w.is_auto) {
                gradientPrint("总伤害:" + intToStr(total));
            } else {
                gradientPrint(w.name + " 打出 " + intToStr(total) + " 伤害!");
            }
            system("pause");

            if (e_hp > 0) {
                hp -= e_atk;
                flashPrint("BOSS反击!受到" + intToStr(e_atk) + "伤害!");
                system("pause");
            }
        }
        else if (c == 2) {
            if (food.count > 0) {
                hp = min(hp_max, hp + food.recover_hp);
                hunger = max(0, hunger - 5);
                food.count--;
                gradientPrint("生命恢复!");
            } else {
                flashPrint("无食物");
            }
            system("pause");
        }
        else if (c == 3) {
            for (auto& w : weapon) {
                if (w.name == "医疗包" && w.count > 0) {
                    hp = min(hp_max, hp + 300);
                    w.count--;
                    gradientPrint("医疗包!生命+300");
                    system("pause");
                    goto end3;
                }
            }
            flashPrint("无医疗包"); system("pause");
            end3:;
        }
        else if (c == 4) {
            clr(); cout << "【切换武器】\n";
            for (int i = 0; i < weapon.size(); i++) {
                if (weapon[i].count > 0 || weapon[i].ammo_type == "无") {
                    cout << i + 1 << ". " << weapon[i].name << " 伤害:" << getRealDamage(weapon[i]);
                    if (weapon[i].is_auto) cout << " 连发";
                    cout << "\n";
                }
            }
            cout << "0.返回\n选择:"; int w; cin >> w;
            if (w > 0 && w <= (int)weapon.size() && (weapon[w - 1].count > 0 || weapon[w - 1].ammo_type == "无")) {
                current_weapon = w - 1;
            }
            system("pause");
        }
        else if (c == 5) {
            flashPrint("你逃离了BOSS战场!"); system("pause"); return;
        }

        if (hp <= 0) {
            clr(); flashPrint("你被BOSS击杀!"); system("pause"); exit(0);
        }
    }

    player_exp += e.exp_val;
    long long mon = e.min_money;
    money += mon;
    gradientPrint("恭喜击败BOSS!获得经验:" + intToStr(e.exp_val) + " 金钱:" + llToStr(mon), COLOR_GREEN);
    checkLevelUp();
    next_half();
}

// 外出探索
void work() {
    clr();
    if (rand() % 100 < 60) {
        long long add = (money * (30 + rand() % 70)) / 100 * maps[current_map].money_rate;
        money += add;
        gradientPrint("获得 " + llToStr(add) + " 金钱!", COLOR_GREEN);
    } else {
        flashPrint("一无所获", COLOR_RED);
    }
    system("pause"); next_half(); checkTaskComplete();
}

// 睡觉恢复
void sleep() {
    clr();
    hp = min(hp_max, hp + 600);
    hunger = max(0, hunger - 5);
    gradientPrint("睡觉恢复生命+600,饥饿-5", COLOR_GREEN);
    system("pause"); next_half();
}

// 购买食物
void buy_food() {
    clr();
    if (money >= food.price) {
        money -= food.price; food.count++;
        gradientPrint("购买食物成功!当前:" + intToStr(food.count), COLOR_GREEN);
    } else flashPrint("钱不够", COLOR_RED);
    system("pause"); next_half();
}

// 武器商店
void shop() {
    while (1) {
        clr(); show_state();
        cout << "【武器商店】\n";
        for (int i = 0; i < weapon.size(); i++) {
            cout << i + 1 << ". " << setw(18) << left << weapon[i].name
            << " ¥" << weapon[i].price << " 伤害:" << getRealDamage(weapon[i]);
            if (weapon[i].is_auto) cout << " 连发";
            cout << " 持有:" << weapon[i].count << "\n";
        }
        cout << "0.返回\n选择:"; int c; cin >> c;
        if (c == 0) break;
        if (c < 1 || c > (int)weapon.size()) continue;
        int id = c - 1;
        if (money >= weapon[id].price) {
            money -= weapon[id].price;
            if (weapon[id].name == "通用子弹1000发") {
                ammo_bullet += 1000;
                gradientPrint("子弹+1000");
            } else if (weapon[id].name == "通用炮弹20发") {
                ammo_shell += 20;
                gradientPrint("炮弹+20");
            } else {
                weapon[id].count++;
                gradientPrint("购买成功!");
            }
        } else {
            flashPrint("金钱不足");
        }
        system("pause");
    }
    next_half();
}

// 背包界面
void bag() {
    clr(); show_state();
    cout << "食物:" << food.count << "\n";
    for (int i = 0; i < weapon.size(); i++) {
        if (weapon[i].count > 0) {
            cout << weapon[i].name << " x" << weapon[i].count << "\n";
        }
    }
    system("pause");
}

// 切换地图
void mapSwitch() {
    clr(); show_state();
    for (int i = 0; i < maps.size(); i++) {
        setColor(maps[i].is_unlocked ? COLOR_GREEN : COLOR_RED);
        cout << i + 1 << ". " << maps[i].name << (maps[i].is_unlocked ? " [已解锁]" : " [锁定]") << "\n";
        setColor(COLOR_WHITE);
    }
    cout << "0.返回\n选择:"; int c; cin >> c;
    if (c > 0 && c <= (int)maps.size() && maps[c - 1].is_unlocked) {
        current_map = c - 1;
    }
    system("pause");
}

// 任务面板
void taskPanel() {
    clr(); show_state();
    for (auto& t : tasks) {
        setColor(t.is_completed ? COLOR_GREEN : COLOR_YELLOW);
        cout << t.id << ". " << t.title << " " << (t.is_completed ? "[已完成]" : "[未完成]") << " 目标:" << t.target << "\n";
        setColor(COLOR_WHITE);
    }
    system("pause");
}

// 吃食物
void eatFood() {
    clr();
    if (food.count > 0) {
        hp = min(hp_max, hp + food.recover_hp);
        hunger = max(0, hunger - 5);
        food.count--;
        gradientPrint("食用成功!");
    } else {
        flashPrint("无食物");
    }
    system("pause"); next_half();
}

// 使用医疗包
void useMed() {
    clr();
    for (auto& w : weapon) {
        if (w.name == "医疗包" && w.count > 0) {
            hp = min(hp_max, hp + 300);
            w.count--;
            gradientPrint("使用医疗包!");
            system("pause");
            return;
        }
    }
    flashPrint("无医疗包");
    system("pause");
}

// 主函数
int main() {
    init();
    welcome();
    while (1) {
        menu();
        int c; cin >> c;
        switch (c) {
            case 1: work(); break;
            case 2: sleep(); break;
            case 3: buy_food(); break;
            case 4: shop(); break;
            case 5: bag(); break;
            case 6: battle(); break;
            case 7: mapSwitch(); break;
            case 8: taskPanel(); break;
            case 9: eatFood(); break;
            case 10: useMed(); break;
            case 11: battleBoss(); break;
            default: flashPrint("无效选项"); system("pause");
        }
        if (hp <= 0) break;
    }
    return 0;
}