跳到正文

算法·理论

florr.ioc++版本

TMXtmx20121001约 59 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月21日
篇幅
约 59 分钟

版本

  1. v1当前
C++
#include <windows.h>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
#include <string>
#include <conio.h>
#include <iostream>

// 游戏常量
const int WIDTH = 80;
const int HEIGHT = 24;
const float PI = 3.1415926535f;


// 枚举定义
enum Rarity { COMMON, UNUSUAL, RARE, EPIC, LEGENDARY, MYTHIC, ULTRA, SUPER };
enum PetalType { BASIC, STINGER, WING, ROCK, LEAF, ROSE, LIGHT, BUBBLE };
enum MapType { GARDEN, DESERT, OCEAN, JUNGLE, ANT_HELL };

// 控制台颜色
const int COLOR_GREEN = 2;
const int COLOR_YELLOW = 6;
const int COLOR_BLUE = 1;
const int COLOR_MAGENTA = 5;
const int COLOR_RED = 4;
const int COLOR_CYAN = 3;
const int COLOR_WHITE = 7;

// 天赋树结构
struct TalentTree {
    int loadout = 0;      // 卡槽数量
    int reload = 0;       // 冷却速度
    int rotation = 0;     // 旋转速度
    int health = 0;       // 最大生命值
    int medic = 0;        // 回血速度
    int summoner = 0;     // 召唤物强度
    int sharpEdges = 0;   // 碰撞伤害
};

// 花瓣类
class Petal {
public:
    PetalType type;
    Rarity rarity;
    int damage;
    float angle;
    float distance;
    bool isAttacking;
    int level;

    Petal(PetalType t, Rarity r, int dmg)
        : type(t), rarity(r), damage(dmg), angle(0), distance(5), isAttacking(false), level(1) {}

    // 获取显示字符
    char getSymbol() const {
        switch(type) {
            case BASIC: return '*';
            case STINGER: return '>';
            case WING: return 'W';
            case ROCK: return 'O';
            case LEAF: return 'L';
            case ROSE: return 'R';
            case LIGHT: return 'T';
            case BUBBLE: return 'B';
            default: return '*';
        }
    }

    // 获取颜色
    int getColor() const {
        switch(rarity) {
            case COMMON: return COLOR_GREEN;
            case UNUSUAL: return COLOR_YELLOW;
            case RARE: return COLOR_BLUE;
            case EPIC: return COLOR_MAGENTA;
            case LEGENDARY: return COLOR_RED;
            case MYTHIC: return COLOR_CYAN;
            case ULTRA: return COLOR_MAGENTA;
            case SUPER: return COLOR_WHITE;
            default: return COLOR_GREEN;
        }
    }

    void update(float rotationBoost) {
        angle += 0.1f + (rotationSpeed() * 0.05f * rotationBoost);
        if (angle > 2 * PI) angle -= 2 * PI;
    }

    float rotationSpeed() const {
        return 1.0f + (level * 0.1f);
    }

    // 获取屏幕坐标
    void getPosition(int centerX, int centerY, int &x, int &y) {
        float effectiveDist = isAttacking ? distance * 1.5f : distance;
        x = centerX + static_cast<int>(cos(angle) * effectiveDist);
        y = centerY + static_cast<int>(sin(angle) * effectiveDist);
    }
};

// 敌人类
class Enemy {
public:
    int x, y;
    int health;
    int maxHealth;
    int damage;
    char symbol;
    int color;
    int expValue;
    PetalType dropType;
    Rarity dropRarity;
    int moveCooldown;
    int attackCooldown;

    Enemy(int posX, int posY, int hp, int dmg, char sym, int col, int exp,
          PetalType drop, Rarity rarity)
        : x(posX), y(posY), health(hp), maxHealth(hp), damage(dmg), symbol(sym),
          color(col), expValue(exp), dropType(drop), dropRarity(rarity),
          moveCooldown(0), attackCooldown(0) {}

    void update(int playerX, int playerY) {
        if (moveCooldown <= 0) {
            // 简单AI:向玩家移动
            if (x < playerX) x++;
            else if (x > playerX) x--;
            if (y < playerY) y++;
            else if (y > playerY) y--;

            moveCooldown = 10; // 移动冷却
        } else {
            moveCooldown--;
        }
    }
};

// 玩家类
class Player {
public:
    int x, y;
    int health;
    int maxHealth;
    int level;
    int exp;
    int expToNext;
    std::vector<Petal> petals;
    std::vector<Petal> inventory;
    TalentTree talents;
    int equippedSlots;
    int currentMap;
    int attackCooldown;
    int talentPoints;

    Player() : x(WIDTH/2), y(HEIGHT/2), health(100), maxHealth(100),
               level(1), exp(0), expToNext(100), equippedSlots(2),
               currentMap(0), attackCooldown(0), talentPoints(0) {
        // 初始花瓣
        addPetal(BASIC, COMMON, 10);
        addPetal(BASIC, COMMON, 10);
        equipPetal(0);
        equipPetal(1);
    }

    void addPetal(PetalType type, Rarity rarity, int damage) {
        inventory.push_back(Petal(type, rarity, damage));
    }

    void equipPetal(int index) {
        if (petals.size() < equippedSlots && index < inventory.size()) {
            petals.push_back(inventory[index]);
            inventory.erase(inventory.begin() + index);
        }
    }

    void unequipPetal(int index) {
        if (index < petals.size()) {
            inventory.push_back(petals[index]);
            petals.erase(petals.begin() + index);
        }
    }

    void move(int dx, int dy) {
        x = std::max(1, std::min(WIDTH - 2, x + dx));
        y = std::max(1, std::min(HEIGHT - 2, y + dy));
    }

    void attack() {
        if (attackCooldown <= 0) {
            for (auto &petal : petals) {
                petal.isAttacking = true;
            }
            attackCooldown = 20 - (talents.reload * 2); // 天赋减少冷却
        }
    }

    void stopAttack() {
        for (auto &petal : petals) {
            petal.isAttacking = false;
        }
    }

    void update() {
        if (attackCooldown > 0) attackCooldown--;

        float rotationBoost = 1.0f + (talents.rotation * 0.2f);
        for (auto &petal : petals) {
            petal.update(rotationBoost);
        }

        // 天赋回血
        if (talents.medic > 0 && health < maxHealth) {
            health = std::min(maxHealth, health + talents.medic);
        }
    }

    void gainExp(int amount) {
        exp += amount;
        if (exp >= expToNext) {
            levelUp();
        }
    }

    void levelUp() {
        level++;
        talentPoints++;
        exp -= expToNext;
        expToNext = static_cast<int>(expToNext * 1.5f);
        maxHealth += 20 + (talents.health * 5);
        health = maxHealth;
    }

    void applyTalent(int talentType) {
        if (talentPoints <= 0) return;

        talentPoints--;
        switch(talentType) {
            case 0:
                talents.loadout++;
                equippedSlots++;
                break;
            case 1:
                talents.reload++;
                break;
            case 2:
                talents.rotation++;
                break;
            case 3:
                talents.health++;
                maxHealth += 25;
                health = maxHealth;
                break;
            case 4:
                talents.medic++;
                break;
            case 5:
                talents.summoner++;
                break;
            case 6:
                talents.sharpEdges++;
                break;
        }
    }
};

// 控制台管理器
class ConsoleManager {
private:
    HANDLE hConsole;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    COORD cursorPos;

public:
    ConsoleManager() {
        hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hConsole, &csbi);
        cursorPos = {0, 0};
    }

    // 设置光标位置
    void setCursorPosition(int x, int y) {
        cursorPos.X = static_cast<SHORT>(x);
        cursorPos.Y = static_cast<SHORT>(y);
        SetConsoleCursorPosition(hConsole, cursorPos);
    }

    // 设置文本颜色
    void setTextColor(int color) {
        SetConsoleTextAttribute(hConsole, color);
    }

    // 重置颜色
    void resetColor() {
        SetConsoleTextAttribute(hConsole, csbi.wAttributes);
    }

    // 清屏
    void clearScreen() {
        COORD topLeft = {0, 0};
        DWORD written;
        DWORD consoleSize = csbi.dwSize.X * csbi.dwSize.Y;
        FillConsoleOutputCharacter(hConsole, ' ', consoleSize, topLeft, &written);
        FillConsoleOutputAttribute(hConsole, csbi.wAttributes, consoleSize, topLeft, &written);
        setCursorPosition(0, 0);
    }

    // 隐藏光标
    void hideCursor() {
        CONSOLE_CURSOR_INFO cursorInfo;
        GetConsoleCursorInfo(hConsole, &cursorInfo);
        cursorInfo.bVisible = false;
        SetConsoleCursorInfo(hConsole, &cursorInfo);
    }

    // 显示光标
    void showCursor() {
        CONSOLE_CURSOR_INFO cursorInfo;
        GetConsoleCursorInfo(hConsole, &cursorInfo);
        cursorInfo.bVisible = true;
        SetConsoleCursorInfo(hConsole, &cursorInfo);
    }
};

// 游戏主类
class FlorrGame {
private:
    Player player;
    std::vector<Enemy> enemies;
    std::map<int, std::string> mapNames;
    int score;
    bool gameOver;
    bool showInventory;
    bool showTalents;
    bool showMapSelect;
    int selectedInventoryItem;
    int selectedTalent;
    ConsoleManager console;

    // 生成敌人
    void spawnEnemies() {
        enemies.clear();
        int enemyCount = 5 + (player.level / 2);

        // 根据当前地图生成不同敌人
        for (int i = 0; i < enemyCount; i++) {
            int x = rand() % (WIDTH-4) + 2;
            int y = rand() % (HEIGHT-4) + 2;

            // 根据地图类型决定敌人类型
            PetalType dropType = BASIC;
            Rarity dropRarity = COMMON;
            char symbol = 'B';
            int color = COLOR_GREEN;
            int health = 30 + (player.level * 5);
            int damage = 5 + player.level;
            int expValue = 10 + (player.level * 2);

            switch(player.currentMap) {
                case DESERT:
                    dropType = ROCK;
                    dropRarity = RARE;
                    symbol = 'D';
                    color = COLOR_BLUE;
                    health += 20;
                    break;
                case OCEAN:
                    dropType = BUBBLE;
                    dropRarity = EPIC;
                    symbol = 'O';
                    color = COLOR_MAGENTA;
                    health += 40;
                    break;
                case JUNGLE:
                    dropType = LEAF;
                    dropRarity = LEGENDARY;
                    symbol = 'J';
                    color = COLOR_RED;
                    health += 60;
                    break;
                case ANT_HELL:
                    dropType = ROSE;
                    dropRarity = MYTHIC;
                    symbol = 'A';
                    color = COLOR_CYAN;
                    health += 80;
                    break;
            }

            enemies.push_back(Enemy(x, y, health, damage, symbol, color, expValue, dropType, dropRarity));
        }
    }

    // 绘制UI边框
    void drawBorder() {
        console.setTextColor(COLOR_WHITE);
        for (int i = 0; i < WIDTH; i++) {
            console.setCursorPosition(i, 0);
            std::cout << '-';
            console.setCursorPosition(i, HEIGHT-1);
            std::cout << '-';
        }
        for (int i = 0; i < HEIGHT; i++) {
            console.setCursorPosition(0, i);
            std::cout << '|';
            console.setCursorPosition(WIDTH-1, i);
            std::cout << '|';
        }
        console.setCursorPosition(0, 0);
        std::cout << '+';
        console.setCursorPosition(WIDTH-1, 0);
        std::cout << '+';
        console.setCursorPosition(0, HEIGHT-1);
        std::cout << '+';
        console.setCursorPosition(WIDTH-1, HEIGHT-1);
        std::cout << '+';
        console.resetColor();
    }

    // 绘制玩家
    void drawPlayer() {
        console.setTextColor(COLOR_YELLOW);
        console.setCursorPosition(player.x, player.y);
        std::cout << '@';
        console.resetColor();

        // 绘制花瓣
        for (auto &petal : player.petals) {
            int px, py;
            petal.getPosition(player.x, player.y, px, py);
            if (px >= 1 && px < WIDTH-1 && py >= 1 && py < HEIGHT-1) {
                console.setTextColor(petal.getColor());
                console.setCursorPosition(px, py);
                std::cout << petal.getSymbol();
                console.resetColor();
            }
        }
    }

    // 绘制敌人
    void drawEnemies() {
        for (auto &enemy : enemies) {
            if (enemy.health > 0) {
                console.setTextColor(enemy.color);
                console.setCursorPosition(enemy.x, enemy.y);
                std::cout << enemy.symbol;
                console.resetColor();

                // 绘制血条
                int barWidth = 5;
                int healthPercent = (enemy.health * barWidth) / enemy.maxHealth;
                console.setCursorPosition(enemy.x - barWidth/2, enemy.y-1);
                for (int i = 0; i < barWidth; i++) {
                    if (i < healthPercent) {
                        console.setTextColor(COLOR_RED);
                        std::cout << '#';
                    } else {
                        console.setTextColor(COLOR_WHITE);
                        std::cout << '_';
                    }
                }
                console.resetColor();
            }
        }
    }

    // 绘制HUD
    void drawHUD() {
        // 生命值条
        int barWidth = 20;
        int healthPercent = static_cast<int>((static_cast<float>(player.health) / player.maxHealth) * barWidth);

        console.setCursorPosition(2, HEIGHT);
        console.setTextColor(COLOR_RED);
        for (int i = 0; i < healthPercent; i++) {
            std::cout << '#';
        }
        console.setTextColor(COLOR_WHITE);
        for (int i = healthPercent; i < barWidth; i++) {
            std::cout << '_';
        }

        console.setCursorPosition(2, HEIGHT);
        std::cout << "HP: " << player.health << "/" << player.maxHealth;

        console.setCursorPosition(30, HEIGHT);
        std::cout << "Lvl: " << player.level;

        console.setCursorPosition(45, HEIGHT);
        std::cout << "Exp: " << player.exp << "/" << player.expToNext;

        console.setCursorPosition(65, HEIGHT);
        std::cout << "Map: " << mapNames[player.currentMap];
    }

    // 绘制物品栏
    void drawInventory() {
        int startX = 5;
        int startY = 3;
        int width = WIDTH - 10;
        int height = HEIGHT - 6;

        // 绘制边框
        console.setTextColor(COLOR_WHITE);
        for (int x = startX; x < startX + width; x++) {
            console.setCursorPosition(x, startY);
            std::cout << '-';
            console.setCursorPosition(x, startY + height);
            std::cout << '-';
        }
        for (int y = startY; y < startY + height; y++) {
            console.setCursorPosition(startX, y);
            std::cout << '|';
            console.setCursorPosition(startX + width, y);
            std::cout << '|';
        }
        console.setCursorPosition(startX, startY);
        std::cout << '+';
        console.setCursorPosition(startX + width, startY);
        std::cout << '+';
        console.setCursorPosition(startX, startY + height);
        std::cout << '+';
        console.setCursorPosition(startX + width, startY + height);
        std::cout << '+';
        console.resetColor();

        // 标题
        console.setCursorPosition(startX + (width - 8) / 2, startY);
        std::cout << "INVENTORY";

        // 显示物品
        int yPos = startY + 2;
        console.setCursorPosition(startX + 2, yPos);
        std::cout << "Equipped Petals:";
        yPos++;

        for (int i = 0; i < player.petals.size(); i++) {
            if (i == selectedInventoryItem) {
                console.setTextColor(COLOR_WHITE | BACKGROUND_BLUE);
            }

            console.setCursorPosition(startX + 4, yPos);
            console.setTextColor(player.petals[i].getColor());
            std::cout << player.petals[i].getSymbol();
            console.setTextColor(COLOR_WHITE);
            std::cout << " - " << getPetalName(player.petals[i].type);

            console.setCursorPosition(startX + 30, yPos);
            std::cout << "Lvl: " << player.petals[i].level << " Dmg: " << player.petals[i].damage;

            if (i == selectedInventoryItem) {
                console.resetColor();
            }
            yPos++;
        }

        yPos += 2;
        console.setCursorPosition(startX + 2, yPos);
        std::cout << "Inventory:";
        yPos++;

        for (int i = 0; i < player.inventory.size(); i++) {
            if (i == selectedInventoryItem - player.petals.size()) {
                console.setTextColor(COLOR_WHITE | BACKGROUND_BLUE);
            }

            console.setCursorPosition(startX + 4, yPos);
            console.setTextColor(player.inventory[i].getColor());
            std::cout << player.inventory[i].getSymbol();
            console.setTextColor(COLOR_WHITE);
            std::cout << " - " << getPetalName(player.inventory[i].type);

            console.setCursorPosition(startX + 30, yPos);
            std::cout << "Lvl: " << player.inventory[i].level << " Dmg: " << player.inventory[i].damage;

            if (i == selectedInventoryItem - player.petals.size()) {
                console.resetColor();
            }
            yPos++;
        }

        // 操作提示
        console.setCursorPosition(startX + 2, startY + height - 1);
        std::cout << "Up/Down:选择  E:装备/卸下  I:关闭  U:升级选中花瓣";
    }

    // 绘制天赋树
    void drawTalentTree() {
        int startX = 5;
        int startY = 3;
        int width = WIDTH - 10;
        int height = HEIGHT - 6;

        // 绘制边框
        console.setTextColor(COLOR_WHITE);
        for (int x = startX; x < startX + width; x++) {
            console.setCursorPosition(x, startY);
            std::cout << '-';
            console.setCursorPosition(x, startY + height);
            std::cout << '-';
        }
        for (int y = startY; y < startY + height; y++) {
            console.setCursorPosition(startX, y);
            std::cout << '|';
            console.setCursorPosition(startX + width, y);
            std::cout << '|';
        }
        console.setCursorPosition(startX, startY);
        std::cout << '+';
        console.setCursorPosition(startX + width, startY);
        std::cout << '+';
        console.setCursorPosition(startX, startY + height);
        std::cout << '+';
        console.setCursorPosition(startX + width, startY + height);
        std::cout << '+';
        console.resetColor();

        // 标题
        console.setCursorPosition(startX + (width - 10) / 2, startY);
        std::cout << "TALENT TREE";
        console.setCursorPosition(startX + 2, startY + 1);
        std::cout << "Available Points: " << player.talentPoints;

        // 天赋列表
        int yPos = startY + 3;
        drawTalentOption(yPos, startX + 5, 0, "Loadout", "增加装备槽位", player.talents.loadout, selectedTalent == 0);
        yPos += 2;
        drawTalentOption(yPos, startX + 5, 1, "Reload", "减少花瓣冷却时间", player.talents.reload, selectedTalent == 1);
        yPos += 2;
        drawTalentOption(yPos, startX + 5, 2, "Rotation", "增加花瓣旋转速度", player.talents.rotation, selectedTalent == 2);
        yPos += 2;
        drawTalentOption(yPos, startX + 5, 3, "Health", "增加最大生命值", player.talents.health, selectedTalent == 3);
        yPos += 2;
        drawTalentOption(yPos, startX + 5, 4, "Medic", "增加生命恢复速度", player.talents.medic, selectedTalent == 4);
        yPos += 2;
        drawTalentOption(yPos, startX + 5, 5, "Summoner", "增强召唤物", player.talents.summoner, selectedTalent == 5);
        yPos += 2;
        drawTalentOption(yPos, startX + 5, 6, "Sharp Edges", "增加碰撞伤害", player.talents.sharpEdges, selectedTalent == 6);

        // 操作提示
        console.setCursorPosition(startX + 2, startY + height - 1);
        std::cout << "Up/Down:选择  Enter:应用天赋  T:关闭";
    }

    void drawTalentOption(int y, int x, int num, const char* name, const char* desc, int level, bool selected) {
        if (selected) {
            console.setTextColor(COLOR_WHITE | BACKGROUND_BLUE);
        }

        console.setCursorPosition(x, y);
        std::cout << num + 1 << ". " << name << " (Lv " << level << ")";
        console.setCursorPosition(x + 4, y + 1);
        std::cout << "- " << desc;

        if (selected) {
            console.resetColor();
        }
    }

    // 绘制地图选择界面
    void drawMapSelect() {
        int startX = 10;
        int startY = 5;
        int width = WIDTH - 20;
        int height = HEIGHT - 10;

        // 绘制边框
        console.setTextColor(COLOR_WHITE);
        for (int x = startX; x < startX + width; x++) {
            console.setCursorPosition(x, startY);
            std::cout << '-';
            console.setCursorPosition(x, startY + height);
            std::cout << '-';
        }
        for (int y = startY; y < startY + height; y++) {
            console.setCursorPosition(startX, y);
            std::cout << '|';
            console.setCursorPosition(startX + width, y);
            std::cout << '|';
        }
        console.setCursorPosition(startX, startY);
        std::cout << '+';
        console.setCursorPosition(startX + width, startY);
        std::cout << '+';
        console.setCursorPosition(startX, startY + height);
        std::cout << '+';
        console.setCursorPosition(startX + width, startY + height);
        std::cout << '+';
        console.resetColor();

        // 标题
        console.setCursorPosition(startX + (width - 12) / 2, startY);
        std::cout << "SELECT MAP";

        // 地图列表
        int yPos = startY + 2;
        for (int i = 0; i < 5; i++) {
            if (i == player.currentMap) {
                console.setTextColor(COLOR_WHITE | BACKGROUND_BLUE);
            }

            console.setCursorPosition(startX + 5, yPos);
            std::cout << mapNames[i];

            if (i == player.currentMap) {
                console.resetColor();
            }
            yPos += 2;
        }

        // 操作提示
        console.setCursorPosition(startX + 2, startY + height - 1);
        std::cout << "Up/Down:选择  Enter:确认  M:关闭";
    }

    // 获取花瓣名称
    std::string getPetalName(PetalType type) {
        switch(type) {
            case BASIC: return "Basic";
            case STINGER: return "Stinger";
            case WING: return "Wing";
            case ROCK: return "Rock";
            case LEAF: return "Leaf";
            case ROSE: return "Rose";
            case LIGHT: return "Light";
            case BUBBLE: return "Bubble";
            default: return "Unknown";
        }
    }

    // 检查碰撞
    void checkCollisions() {
        // 花瓣攻击敌人
        for (auto &enemy : enemies) {
            if (enemy.health <= 0) continue;

            for (auto &petal : player.petals) {
                int px, py;
                petal.getPosition(player.x, player.y, px, py);

                // 简单距离碰撞检测
                if (abs(px - enemy.x) <= 1 && abs(py - enemy.y) <= 1 && petal.isAttacking) {
                    int damage = petal.damage + (player.talents.sharpEdges * 5);
                    enemy.health -= damage;

                    if (enemy.health <= 0) {
                        player.gainExp(enemy.expValue);
                        score += enemy.expValue;

                        // 掉落花瓣
                        if (rand() % 100 < 30) { // 30%掉落率
                            player.addPetal(enemy.dropType, enemy.dropRarity, enemy.damage);
                        }
                    }
                }
            }

            // 敌人攻击玩家
            if (abs(enemy.x - player.x) <= 1 && abs(enemy.y - player.y) <= 1) {
                if (enemy.attackCooldown <= 0) {
                    player.health -= enemy.damage;
                    enemy.attackCooldown = 30;

                    if (player.health <= 0) {
                        gameOver = true;
                    }
                }
            }

            if (enemy.attackCooldown > 0) {
                enemy.attackCooldown--;
            }
        }

        // 移除死亡的敌人
        enemies.erase(std::remove_if(enemies.begin(), enemies.end(),
            [](const Enemy& e) { return e.health <= 0; }), enemies.end());
    }

    // 升级选中的花瓣
    void upgradeSelectedPetal() {
        if (selectedInventoryItem < player.petals.size()) {
            // 升级装备中的花瓣
            player.petals[selectedInventoryItem].level++;
            player.petals[selectedInventoryItem].damage += 5;
        } else if (selectedInventoryItem - player.petals.size() < player.inventory.size()) {
            // 升级库存中的花瓣
            int index = selectedInventoryItem - player.petals.size();
            player.inventory[index].level++;
            player.inventory[index].damage += 5;
        }
    }

public:
    FlorrGame() : score(0), gameOver(false), showInventory(false),
                 showTalents(false), showMapSelect(false),
                 selectedInventoryItem(0), selectedTalent(0) {
        // 初始化地图名称
        mapNames[0] = "Garden";
        mapNames[1] = "Desert";
        mapNames[2] = "Ocean";
        mapNames[3] = "Jungle";
        mapNames[4] = "Ant Hell";

        srand(time(0));
        spawnEnemies();
    }

    void run() {
        console.hideCursor();

        // 主游戏循环
        while (!gameOver) {
            console.clearScreen();

            if (showInventory) {
                drawInventory();
            } else if (showTalents) {
                drawTalentTree();
            } else if (showMapSelect) {
                drawMapSelect();
            } else {
                drawBorder();
                drawEnemies();
                drawPlayer();
                drawHUD();

                // 显示操作提示
                console.setCursorPosition(2, HEIGHT + 1);
                std::cout << "WASD:移动 空格:攻击 I:物品栏 T:天赋树 M:地图 Q:退出";
                console.setCursorPosition(2, HEIGHT + 2);
                std::cout << "分数: " << score;
            }

            // 处理输入
            handleInput();

            if (!showInventory && !showTalents && !showMapSelect) {
                player.update();

                // 更新敌人
                for (auto &enemy : enemies) {
                    enemy.update(player.x, player.y);
                }

                checkCollisions();

                // 敌人数量不足时重新生成
                if (enemies.size() < 3 + (player.level / 2)) {
                    spawnEnemies();
                }
            }

            // 控制帧率
            Sleep(50);
        }

        // 游戏结束
        console.clearScreen();
        console.setCursorPosition((WIDTH-9)/2, HEIGHT/2);
        std::cout << "GAME OVER";
        console.setCursorPosition((WIDTH-10)/2, HEIGHT/2 + 2);
        std::cout << "Score: " << score;
        console.showCursor();
        getch();
    }

    void handleInput() {
        if (_kbhit()) {
            int ch = _getch();

            if (showInventory) {
                handleInventoryInput(ch);
            } else if (showTalents) {
                handleTalentInput(ch);
            } else if (showMapSelect) {
                handleMapSelectInput(ch);
            } else {
                handleGameInput(ch);
            }
        }
    }

    void handleGameInput(int ch) {
        switch(ch) {
            case 'w': case 'W': player.move(0, -1); break;
            case 's': case 'S': player.move(0, 1); break;
            case 'a': case 'A': player.move(-1, 0); break;
            case 'd': case 'D': player.move(1, 0); break;
            case ' ': player.attack(); break;
            case 'i': case 'I':
                showInventory = true;
                selectedInventoryItem = 0;
                break;
            case 't': case 'T':
                showTalents = true;
                selectedTalent = 0;
                break;
            case 'm': case 'M':
                showMapSelect = true;
                break;
            case 'q': case 'Q': gameOver = true; break;
            default: player.stopAttack();
        }
    }

    void handleInventoryInput(int ch) {
        switch(ch) {
            case 'i': case 'I': showInventory = false; break;
            case 72: // 上箭头
                if (selectedInventoryItem > 0) {
                    selectedInventoryItem--;
                }
                break;
            case 80: // 下箭头
                if (selectedInventoryItem < player.petals.size() + player.inventory.size() - 1) {
                    selectedInventoryItem++;
                }
                break;
            case 'e': case 'E':
                if (selectedInventoryItem < player.petals.size()) {
                    player.unequipPetal(selectedInventoryItem);
                } else if (selectedInventoryItem - player.petals.size() < player.inventory.size()) {
                    player.equipPetal(selectedInventoryItem - player.petals.size());
                }
                break;
            case 'u': case 'U':
                upgradeSelectedPetal();
                break;
        }
    }

    void handleTalentInput(int ch) {
        switch(ch) {
            case 't': case 'T': showTalents = false; break;
            case 72: // 上箭头
                if (selectedTalent > 0) {
                    selectedTalent--;
                }
                break;
            case 80: // 下箭头
                if (selectedTalent < 6) {
                    selectedTalent++;
                }
                break;
            case 13: // 回车键
                player.applyTalent(selectedTalent);
                break;
        }
    }

    void handleMapSelectInput(int ch) {
        switch(ch) {
            case 'm': case 'M': showMapSelect = false; break;
            case 72: // 上箭头
                if (player.currentMap > 0) {
                    player.currentMap--;
                }
                break;
            case 80: // 下箭头
                if (player.currentMap < 4) {
                    player.currentMap++;
                }
                break;
            case 13: // 回车键
                showMapSelect = false;
                spawnEnemies();
                break;
        }
    }
};

int main() {
    FlorrGame game;
    game.run();
    return 0;
}
原文