跳到正文

休闲·娱乐

魔塔

TimeLimitTime_tlt约 56 分钟
原文
文章信息与版本

文章信息

收录
2026年8月4日
更新
2026年8月14日
篇幅
约 56 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <algorithm>
#include <fstream>
#include <sstream>
using namespace std;

// ==================== 地图元素定义 ====================
#define EMPTY       0
#define WALL        1
#define YELLOW_DOOR 2
#define RED_DOOR    3
#define MONSTER     4
#define SHOP        5
#define Y_KEY       6
#define R_KEY       7
#define BLOOD       8
#define ATK_GEM     9
#define DEF_GEM     10
#define UP_STAIR    11
#define DOWN_STAIR  12
#define BOSS        13
#define TELEPORT    14
#define IRON_SHIELD 15
#define STEEL_SWORD 16
#define FLIGHT_WING 17

// ==================== 玩家属性 ====================
int hp = 120, atk = 12, def = 6;
int yellowKey = 2, redKey = 0;
int money = 0;
int expVal = 0;
int level = 1;
int nowFloor = 1;
int px = 1, py = 1;
bool needUpdate = true;

// 道具
bool hasIronShield = false;
bool hasSteelSword = false;
int flightStep = 0;

// 仙子
bool fairyMet = false;
bool fairyBuff = false;

// 游戏模式
bool hardMode = false;

// 局部刷新
int lastPx = -1, lastPy = -1;
bool mapDrawn = false;

// 窗口焦点
bool windowFocused = true;

// ==================== 地图数据 ====================
int floorMap[12][13][13] = {
    {},
    // 1层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,1,0,0,0,0,0,0,0,1},
        {1,0,4,0,1,0,8,0,1,0,6,0,1},
        {1,0,0,0,2,0,0,0,1,0,0,0,1},
        {1,1,1,1,1,0,0,0,1,1,1,1,1},
        {1,0,0,0,0,0,4,0,0,0,0,0,1},
        {1,0,7,0,1,1,1,1,1,0,9,0,1},
        {1,0,0,0,1,5,0,0,1,0,0,0,1},
        {1,1,1,0,1,0,0,0,1,1,1,3,1},
        {1,0,0,0,1,0,10,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,4,0,11,1},
        {1,0,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 2层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,2,0,0,1,0,0,0,6,0,1},
        {1,0,4,1,1,0,1,0,4,0,0,0,1},
        {1,0,0,0,1,0,0,0,1,1,1,2,1},
        {1,1,1,0,1,1,1,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,1,0,8,0,1},
        {1,0,IRON_SHIELD,1,1,5,1,0,1,0,0,0,1},
        {1,0,0,1,0,0,1,0,0,0,10,0,1},
        {1,1,2,1,0,0,1,1,1,1,1,1,1},
        {1,0,0,0,0,4,0,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,0,0,11,1},
        {1,0,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 3层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,1,0,7,0,1,0,0,0,1},
        {1,0,4,0,1,0,0,0,1,0,4,0,1},
        {1,0,0,2,1,1,1,1,1,2,0,0,1},
        {1,1,1,0,0,0,0,0,0,0,1,1,1},
        {1,0,0,0,0,4,0,8,4,0,0,0,1},
        {1,0,10,0,1,1,5,1,1,0,9,0,1},
        {1,0,0,0,1,0,0,0,1,0,0,0,1},
        {1,1,1,1,1,0,0,0,1,1,1,3,1},
        {1,0,0,0,0,0,4,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,0,0,11,1},
        {1,0,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 4层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,3,0,0,1,0,0,7,0,0,1},
        {1,0,4,1,1,0,1,0,4,1,0,0,1},
        {1,0,0,0,1,0,0,0,1,0,2,0,1},
        {1,1,2,0,1,1,1,0,1,1,1,0,1},
        {1,0,0,0,0,0,8,0,0,0,0,0,1},
        {1,0,STEEL_SWORD,1,0,5,0,1,0,10,0,0,1},
        {1,0,0,1,0,0,0,1,0,0,4,0,1},
        {1,1,1,1,1,0,0,0,1,1,1,2,1},
        {1,0,0,0,0,4,0,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,0,0,11,1},
        {1,0,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 5层(传送阵在此层)
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,1,0,0,0,1,0,7,0,1},
        {1,0,4,2,1,0,4,0,1,0,0,0,1},
        {1,0,0,0,1,1,1,1,1,2,0,0,1},
        {1,1,1,0,0,0,0,0,0,0,1,1,1},
        {1,0,0,0,0,4,8,4,0,0,0,0,1},
        {1,0,10,0,1,5,0,1,0,9,0,0,1},
        {1,0,0,0,1,0,0,0,1,0,0,0,1},
        {1,1,1,1,1,0,0,0,1,1,1,3,1},
        {1,0,0,0,0,4,0,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,0,TELEPORT,11,1},
        {1,0,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 6层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,2,0,0,1,0,0,6,0,0,1},
        {1,0,4,1,1,0,1,0,4,1,0,0,1},
        {1,0,0,0,1,0,8,0,1,0,3,0,1},
        {1,1,3,0,1,1,1,0,1,1,1,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,9,1,0,5,0,1,0,10,0,0,1},
        {1,0,0,1,0,4,0,1,0,0,4,0,1},
        {1,1,1,1,1,0,0,0,1,1,1,2,1},
        {1,0,0,0,0,4,0,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,0,0,11,1},
        {1,FLIGHT_WING,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 7层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,1,7,0,0,1,0,0,0,1},
        {1,0,4,2,1,0,4,0,1,0,4,0,1},
        {1,0,0,0,1,1,1,1,1,2,0,0,1},
        {1,1,1,0,0,0,8,0,0,0,1,1,1},
        {1,0,0,0,0,4,0,4,0,0,0,0,1},
        {1,0,10,0,1,5,0,1,0,9,0,0,1},
        {1,0,0,0,1,0,0,0,1,0,0,0,1},
        {1,1,1,1,1,0,0,0,1,1,1,3,1},
        {1,0,0,0,0,4,0,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,0,0,11,1},
        {1,0,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 8层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,3,0,0,1,0,6,0,0,0,1},
        {1,0,4,1,1,0,1,0,4,1,0,0,1},
        {1,0,0,0,1,0,8,0,1,0,2,0,1},
        {1,1,2,0,1,1,1,0,1,1,1,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,9,1,0,5,0,1,0,10,0,0,1},
        {1,0,0,1,0,4,0,1,0,0,4,0,1},
        {1,1,1,1,1,0,0,0,1,1,1,2,1},
        {1,0,0,0,0,4,0,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,0,0,11,1},
        {1,0,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 9层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,2,0,0,1,7,0,0,0,0,1},
        {1,0,4,1,1,0,1,0,4,1,0,0,1},
        {1,0,0,0,1,0,8,0,1,0,3,0,1},
        {1,1,3,0,1,1,1,0,1,1,1,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,9,1,0,5,0,1,0,10,0,0,1},
        {1,0,0,1,0,4,0,1,0,0,4,0,1},
        {1,1,1,1,1,0,0,0,1,1,1,2,1},
        {1,0,0,0,0,4,0,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,0,0,11,1},
        {1,0,0,0,0,0,0,0,0,0,0,12,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 10层 BOSS + 传送阵
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,1,0,0,0,1,0,0,0,1},
        {1,0,4,0,1,0,8,0,1,0,6,0,1},
        {1,0,0,2,1,0,0,0,1,0,0,0,1},
        {1,1,1,0,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,7,0,1,1,13,1,1,0,9,0,1},
        {1,0,0,0,1,5,0,0,1,0,0,0,1},
        {1,1,1,0,1,0,0,0,1,1,1,3,1},
        {1,0,0,0,1,0,10,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,4,TELEPORT,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    },
    // 11层 地下隐藏层
    {
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,4,0,0,0,0,0,0,4,0,0,1},
        {1,0,0,1,1,1,0,1,1,1,0,0,1},
        {1,0,8,0,0,0,0,0,0,0,9,0,1},
        {1,0,0,0,10,0,TELEPORT,0,5,0,0,0,1},
        {1,0,0,1,1,1,0,1,1,1,0,0,1},
        {1,0,4,0,0,0,0,0,0,4,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1},
    }
};

// ==================== 怪物结构体 ====================
struct Monster {
    int hp, atk, def, coin;
};

// ==================== 控制台工具函数 ====================
void gotoxy(int x, int y) {
    COORD pos = {(SHORT)x, (SHORT)y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void hideCursor() {
    CONSOLE_CURSOR_INFO c = {1, FALSE};
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &c);
}

void setColor(int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (WORD)color);
}

void clearArea(int startX, int startY, int width, int height) {
    for (int y = startY; y < startY + height; y++) {
        gotoxy(startX, y);
        cout << string(width, ' ');
    }
}

// 检测控制台窗口是否在前台
bool isWindowFocused() {
    HWND hwnd = GetConsoleWindow();
    if (!hwnd) return true;
    return GetForegroundWindow() == hwnd;
}

// ==================== 初始化玩家位置 ====================
void initPlayerPos() {
    for (int y = 0; y < 13; y++) {
        for (int x = 0; x < 13; x++) {
            if (floorMap[nowFloor][y][x] == EMPTY) {
                px = x;
                py = y;
                lastPx = -1;
                lastPy = -1;
                return;
            }
        }
    }
    px = 1; py = 1; lastPx = -1; lastPy = -1;
}

// ==================== 地图绘制(只画一次) ====================
void drawMap() {
    if (mapDrawn) return;

    gotoxy(0, 0);
    for (int y = 0; y < 13; y++) {
        for (int x = 0; x < 13; x++) {
            int obj = floorMap[nowFloor][y][x];
            switch (obj) {
                case EMPTY:       cout << " "; break;
                case WALL:        setColor(8);  cout << "#"; setColor(7); break;
                case YELLOW_DOOR: setColor(14); cout << "Y"; setColor(7); break;
                case RED_DOOR:    setColor(12); cout << "R"; setColor(7); break;
                case MONSTER:     setColor(4);  cout << "M"; setColor(7); break;
                case SHOP:        setColor(11); cout << "S"; setColor(7); break;
                case Y_KEY:       setColor(14); cout << "y"; setColor(7); break;
                case R_KEY:       setColor(12); cout << "r"; setColor(7); break;
                case BLOOD:       setColor(10); cout << "+"; setColor(7); break;
                case ATK_GEM:     setColor(13); cout << "A"; setColor(7); break;
                case DEF_GEM:     setColor(9);  cout << "D"; setColor(7); break;
                case UP_STAIR:    setColor(15); cout << "^"; setColor(7); break;
                case DOWN_STAIR:  setColor(15); cout << "v"; setColor(7); break;
                case BOSS:        setColor(12); cout << "B"; setColor(7); break;
                case TELEPORT:    setColor(13); cout << "*"; setColor(7); break;
                case IRON_SHIELD: setColor(9);  cout << "["; setColor(7); break;
                case STEEL_SWORD: setColor(13); cout << "/"; setColor(7); break;
                case FLIGHT_WING: setColor(14); cout << "~"; setColor(7); break;
            }
        }
        cout << endl;
    }
    mapDrawn = true;
}

// ==================== 只画角色(单字符) ====================
void drawPlayer() {
    // 擦掉旧位置
    if (lastPx >= 0 && lastPy >= 0) {
        gotoxy(lastPx, lastPy);
        cout << " ";
    }
    // 画新位置
    gotoxy(px, py);
    setColor(10);
    cout << "@";
    setColor(7);

    lastPx = px;
    lastPy = py;
}

// ==================== 状态栏 ====================
void showInfo() {
    clearArea(0, 14, 40, 8);
    gotoxy(0, 14);
    setColor(7);
    cout << "===== 玩家状态 =====" << endl;
    cout << "生命:" << hp << " 攻击:" << atk << " 防御:" << def << endl;
    cout << "等级:" << level << " 经验:" << expVal << "/" << level * 100 << endl;
    cout << "黄钥匙:" << yellowKey << " 红钥匙:" << redKey << " 金币:" << money << endl;
    cout << "楼层:" << nowFloor << "/11" << endl;
    if (fairyMet) cout << "[仙子已觉醒] ";
    if (flightStep > 0) cout << "[飞行器:" << flightStep << "] ";
    cout << endl;
    cout << "F1~F3:存档  F5:读档  I:背包  ESC:退出" << endl;
    cout << "WASD:移动  S+E:召唤仙子" << endl;
}

// ==================== 伤害计算 ====================
int calcDamage(int myAtk, int monDef, int monHp,
               int monAtk, int myDef, int& rounds) {
    int dmgPerRound = max(1, myAtk - monDef);
    rounds = (monHp + dmgPerRound - 1) / dmgPerRound;
    int take = max(1, monAtk - myDef);
    return take * rounds;
}

// ==================== 升级检测 ====================
void checkLevelUp() {
    int needExp = level * 100;
    while (expVal >= needExp) {
        expVal -= needExp;
        level++;
        hp += 30;
        atk += 3;
        def += 2;

        system("cls");
        setColor(11);
        cout << "======== 升 级 ========" << endl;
        cout << "等级提升至 Lv." << level << "!" << endl;
        cout << "生命+30  攻击+3  防御+2" << endl;
        setColor(7);
        Sleep(1500);

        // 升级后清屏,强制重绘地图
        system("cls");
        mapDrawn = false;
        lastPx = -1;
        lastPy = -1;
        needUpdate = true;
    }
}

// ==================== 战斗系统(修复:战斗后正确返回地图) ====================
void fight(int mx, int my, bool isBoss) {
    Monster m;
    if (!isBoss) {
        m.hp = 40 + nowFloor * 15;
        m.atk = 6 + nowFloor * 3;
        m.def = 2 + nowFloor;
        m.coin = 10 + nowFloor * 5;
    } else {
        m.hp = 800;
        m.atk = 65;
        m.def = 25;
        m.coin = 999;
    }

    if (hardMode) {
        m.hp *= 2;
        m.atk = m.atk * 3 / 2;
        m.coin /= 2;
    }

    // ---- 战斗前信息 ----
    system("cls");
    if (!isBoss) {
        cout << "遭遇怪物!" << endl;
    } else {
        cout << "=== 最终魔王降临 ===" << endl;
    }

    int rounds = 0;
    int willLose = calcDamage(atk, m.def, m.hp, m.atk, def, rounds);

    cout << "怪物 生命:" << m.hp << " 攻击:" << m.atk << " 防御:" << m.def << endl;
    cout << "预计交锋 " << rounds << " 回合" << endl;
    cout << "预计受伤:" << willLose << " 点" << endl;

    if (willLose >= hp) {
        setColor(12); cout << "? 此战可能致命!" << endl; setColor(7);
    } else if (willLose == 0) {
        setColor(10); cout << "? 可无损击杀" << endl; setColor(7);
    }
    Sleep(1000);

    // ---- 战斗过程 ----
    int turn = 1;
    while (m.hp > 0 && hp > 0) {
        // 玩家攻击
        int hurt = max(1, atk - m.def);
        m.hp -= hurt;
        cout << "[" << turn << "] 你造成 " << hurt << " 伤害,怪物剩余:" << m.hp << endl;

        if (m.hp <= 0) break;

        // 怪物反击
        hurt = max(1, m.atk - def);
        hp -= hurt;
        cout << "[" << turn << "] 怪物反击 " << hurt << " 伤害,你剩余:" << hp << endl;
        cout << endl;
        turn++;
        Sleep(400);
    }

    // ---- 战斗结束 ----
    if (hp <= 0) {
        system("cls");
        setColor(12);
        cout << "============================" << endl;
        cout << "   你被击败了,游戏结束!" << endl;
        cout << "============================" << endl;
        setColor(7);
        Sleep(2500);
        exit(0);
    }

    // 胜利结算
    money += m.coin;
    expVal += m.coin / 2;
    cout << endl;
    setColor(10);
    cout << "★ 胜利!获得 " << m.coin << " 金币 + " << m.coin / 2 << " 经验" << endl;
    setColor(7);
    Sleep(1500);

    // 清除怪物格子
    floorMap[nowFloor][my][mx] = EMPTY;

    // ★ 关键修复:战斗结束后清屏 + 强制重绘地图
    system("cls");
    mapDrawn = false;       // 地图需要重画
    lastPx = -1;            // 角色需要重画
    lastPy = -1;
    needUpdate = true;

    // Boss 特殊流程
    if (isBoss) {
        system("cls");
        setColor(14);
        cout << "##############################" << endl;
        cout << "#                            #" << endl;
        cout << "#      通 关 成 功 !         #" << endl;
        cout << "#                            #" << endl;
        cout << "##############################" << endl;
        cout << endl;
        cout << "你击败了魔王,拯救了这片大陆!" << endl;
        if (hardMode) cout << endl << "★ 困难模式通关 ★" << endl;
        setColor(7);
        Sleep(4000);
        exit(0);
    }

    // 检查升级(可能触发清屏+重绘)
    checkLevelUp();

    // 最终确保返回地图
    system("cls");
    mapDrawn = false;
    lastPx = -1;
    lastPy = -1;
    needUpdate = true;
}

// ==================== 商店 ====================
void shopFunc() {
    system("cls");
    cout << "==== 商 店 ====" << endl;
    cout << "金币:" << money << endl;
    cout << "1: +50生命 (20金)" << endl;
    cout << "2: +5攻击 (25金)" << endl;
    cout << "3: +3防御 (25金)" << endl;
    cout << "4: 离开" << endl;
    cout << ">> ";

    int opt;
    cin >> opt;
    switch (opt) {
        case 1:
            if (money >= 20) { hp += 50; money -= 20;
                cout << "生命+50!" << endl;
            } else { cout << "金币不足!" << endl; }
            Sleep(800);
            break;
        case 2:
            if (money >= 25) { atk += 5; money -= 25;
                cout << "攻击+5!" << endl;
            } else { cout << "金币不足!" << endl; }
            Sleep(800);
            break;
        case 3:
            if (money >= 25) { def += 3; money -= 25;
                cout << "防御+3!" << endl;
            } else { cout << "金币不足!" << endl; }
            Sleep(800);
            break;
        default: break;
    }
    floorMap[nowFloor][py][px] = EMPTY;

    system("cls");
    mapDrawn = false;
    lastPx = -1;
    lastPy = -1;
    needUpdate = true;
}

// ==================== 仙子对话 ====================
void fairyDialog() {
    system("cls");
    setColor(11);
    if (!fairyMet) {
        cout << "仙子:勇敢的勇士,我已等候多时。\n";
        cout << "今后你站在商店(S)上按 F 便可呼唤我。\n\n";
        fairyMet = true;
        Sleep(1800);
    }
    cout << "===== 仙子指引 =====\n";
    cout << "1. 恢复全部生命\n";
    cout << "2. 赐予神力(+5 攻击,仅一次)\n";
    cout << "3. 魔王情报\n";
    cout << "4. 进入商店\n";
    cout << "5. 离开\n";
    setColor(7);

    char c = _getch();
    switch (c) {
        case '1': hp += 200; cout << "仙子为你注入了生命力。\n"; break;
        case '2':
            if (!fairyBuff) { atk += 5; fairyBuff = true; cout << "你感受到体内涌动的神力!\n"; }
            else cout << "神力只能赐予一次。\n";
            break;
        case '3':
            cout << "魔王防御:25\n";
            cout << (atk > 25 ? "你已具备挑战资格。\n" : "你的攻击尚不足以破防。\n");
            break;
        case '4':
            shopFunc();      // 原来商店逻辑
            return;          // shopFunc 里会清屏,直接返回
        default:
            cout << "仙子轻轻挥手,消失在空气中。\n";
    }
    Sleep(1400);
    system("cls");
    mapDrawn = false;
    lastPx = -1; lastPy = -1;
    needUpdate = true;
}

// ==================== 背包 ====================
void showInventory() {
    system("cls");
    setColor(11);
    cout << "===== 背 包 =====" << endl;
    cout << "1. 铁盾:" << (hasIronShield ? "已装备 (+3防御)" : "未获得") << endl;
    cout << "2. 钢剑:" << (hasSteelSword ? "已装备 (+5攻击)" : "未获得") << endl;
    cout << "3. 飞行器:" << (flightStep > 0 ? to_string(flightStep) + " 步" : "未获得/已耗尽") << endl;
    setColor(7);
    cout << endl << "按任意键关闭...";
    _getch();

    system("cls");
    mapDrawn = false;
    lastPx = -1;
    lastPy = -1;
    needUpdate = true;
}

// ==================== 存档 ====================
void saveGame(int slot) {
    string fname = "save" + to_string(slot) + ".txt";
    ofstream f(fname);
    if (!f) return;

    f << hp << ' ' << atk << ' ' << def << '\n';
    f << yellowKey << ' ' << redKey << ' ' << money << '\n';
    f << expVal << ' ' << level << '\n';
    f << nowFloor << ' ' << px << ' ' << py << '\n';
    f << hasIronShield << ' ' << hasSteelSword << ' ' << flightStep << '\n';
    f << fairyMet << ' ' << fairyBuff << '\n';
    f << hardMode << '\n';

    for (int z = 1; z <= 11; z++) {
        for (int y = 0; y < 13; y++) {
            for (int x = 0; x < 13; x++) {
                f << floorMap[z][y][x] << ' ';
            }
            f << '\n';
        }
    }
    f.close();

    system("cls");
    setColor(10);
    cout << "==== 存档成功 ====" << endl;
    cout << "已保存至槽位 " << slot << endl;
    setColor(7);
    Sleep(1000);

    system("cls");
    mapDrawn = false;
    lastPx = -1;
    lastPy = -1;
    needUpdate = true;
}

// ==================== 读档 ====================
bool loadGame(int slot) {
    string fname = "save" + to_string(slot) + ".txt";
    ifstream f(fname);
    if (!f) {
        system("cls");
        setColor(12);
        cout << "存档 " << slot << " 不存在!" << endl;
        setColor(7);
        Sleep(800);
        return false;
    }

    f >> hp >> atk >> def;
    f >> yellowKey >> redKey >> money;
    f >> expVal >> level;
    f >> nowFloor >> px >> py;
    f >> hasIronShield >> hasSteelSword >> flightStep;
    f >> fairyMet >> fairyBuff;
    bool savedHard;
    f >> savedHard;
    hardMode = savedHard;

    for (int z = 1; z <= 11; z++) {
        for (int y = 0; y < 13; y++) {
            for (int x = 0; x < 13; x++) {
                f >> floorMap[z][y][x];
            }
        }
    }
    f.close();

    system("cls");
    setColor(11);
    cout << "==== 读档成功 ====" << endl;
    cout << "已从槽位 " << slot << " 恢复" << endl;
    setColor(7);
    Sleep(1000);

    // 强制全量重绘
    system("cls");
    mapDrawn = false;
    lastPx = -1;
    lastPy = -1;
    needUpdate = true;
    return true;
}

// ==================== 交互(踩格子) ====================
void touch(int x, int y) {
    int obj = floorMap[nowFloor][y][x];
    switch (obj) {
        case YELLOW_DOOR:
            if (yellowKey > 0) {
                yellowKey--;
                floorMap[nowFloor][y][x] = EMPTY;
                needUpdate = true;
            }
            break;
        case RED_DOOR:
            if (redKey > 0) {
                redKey--;
                floorMap[nowFloor][y][x] = EMPTY;
                needUpdate = true;
            }
            break;
        case MONSTER: {
            Monster m;
            m.hp = 40 + nowFloor * 15;
            m.atk = 6 + nowFloor * 3;
            m.def = 2 + nowFloor;
            if (atk <= m.def) {
                system("cls");
                setColor(14);
                cout << "攻击不足以破防,无法战斗!" << endl;
                setColor(7);
                Sleep(900);
                system("cls");
                mapDrawn = false;
                needUpdate = true;
                return;
            }
            fight(x, y, false);
            break;
        }
        case BOSS: {
            if (atk <= 25) {
                system("cls");
                setColor(12);
                cout << "攻击不足以破防魔王!" << endl;
                setColor(7);
                Sleep(900);
                system("cls");
                mapDrawn = false;
                needUpdate = true;
                return;
            }
            system("cls");
            cout << "前方是最终魔王!确定要挑战吗?(Y/N)" << endl;
            char confirm = _getch();
            if (confirm != 'y' && confirm != 'Y') {
                system("cls");
                mapDrawn = false;
                needUpdate = true;
                return;
            }
            fight(x, y, true);
            break;
        }
        case SHOP:
    		fairyDialog();   // 站商店格按任何移动外的键都先问仙子,内部可选进商店
    		break;
        case Y_KEY:
            yellowKey++;
            floorMap[nowFloor][y][x] = EMPTY;
            needUpdate = true;
            break;
        case R_KEY:
            redKey++;
            floorMap[nowFloor][y][x] = EMPTY;
            needUpdate = true;
            break;
        case BLOOD:
            hp += 45;
            floorMap[nowFloor][y][x] = EMPTY;
            needUpdate = true;
            break;
        case ATK_GEM:
            atk += 3;
            floorMap[nowFloor][y][x] = EMPTY;
            needUpdate = true;
            break;
        case DEF_GEM:
            def += 2;
            floorMap[nowFloor][y][x] = EMPTY;
            needUpdate = true;
            break;
        case UP_STAIR:
            if (nowFloor < 11) {
                nowFloor++;
                system("cls");
                initPlayerPos();
                mapDrawn = false;
            }
            needUpdate = true;
            break;
        case DOWN_STAIR:
            if (nowFloor > 1) {
                nowFloor--;
                system("cls");
                initPlayerPos();
                mapDrawn = false;
            }
            needUpdate = true;
            break;
        case TELEPORT:
            system("cls");
            setColor(13);
            if (nowFloor == 5) {
                cout << "传送阵启动,前往地下一层..." << endl;
                nowFloor = 11;
            } else if (nowFloor == 10) {
                cout << "传送阵启动,返回地面..." << endl;
                nowFloor = 1;
            } else if (nowFloor == 11) {
                cout << "传送阵启动,返回第五层..." << endl;
                nowFloor = 5;
            }
            setColor(7);
            Sleep(1200);
            system("cls");
            initPlayerPos();
            mapDrawn = false;
            needUpdate = true;
            break;
        case IRON_SHIELD:
            hasIronShield = true;
            def += 3;
            floorMap[nowFloor][y][x] = EMPTY;
            needUpdate = true;
            break;
        case STEEL_SWORD:
            hasSteelSword = true;
            atk += 5;
            floorMap[nowFloor][y][x] = EMPTY;
            needUpdate = true;
            break;
        case FLIGHT_WING:
            flightStep = 10;
            floorMap[nowFloor][y][x] = EMPTY;
            needUpdate = true;
            break;
    }
}

// ==================== 移动 ====================
void move(char ch) {
    int nx = px, ny = py;
    if (ch == 'w' || ch == 'W') ny--;
    if (ch == 's' || ch == 'S') ny++;
    if (ch == 'a' || ch == 'A') nx--;
    if (ch == 'd' || ch == 'D') nx++;

    if (nx < 0 || nx >= 13 || ny < 0 || ny >= 13) return;

    int target = floorMap[nowFloor][ny][nx];

    if (flightStep > 0) {
        // 飞行状态:穿墙穿门,但消耗步数
        flightStep--;
        if (target == YELLOW_DOOR) { yellowKey++; floorMap[nowFloor][ny][nx] = EMPTY; }
        if (target == RED_DOOR)   { redKey++;   floorMap[nowFloor][ny][nx] = EMPTY; }
        if (flightStep == 0) needUpdate = true;
    } else {
        if (target == WALL) return;
        if (target == YELLOW_DOOR && yellowKey <= 0) {
            gotoxy(0, 22);
            setColor(14);
            cout << "需要黄钥匙!";
            setColor(7);
            Sleep(600);
            clearArea(0, 22, 30, 1);
            return;
        }
        if (target == RED_DOOR && redKey <= 0) {
            gotoxy(0, 22);
            setColor(12);
            cout << "需要红钥匙!";
            setColor(7);
            Sleep(600);
            clearArea(0, 22, 30, 1);
            return;
        }
    }

    px = nx;
    py = ny;
    touch(px, py);
}

// ==================== 开场画面 ====================
void titleScreen() {
    system("cls");
    setColor(11);
    cout << "##############################" << endl;
    cout << "#                            #" << endl;
    cout << "#      10 层 魔 塔 完整版    #" << endl;
    cout << "#                            #" << endl;
    cout << "#  WASD 移动  I 背包         #" << endl;
    cout << "#  F1~F3 存档  F5 读档       #" << endl;
    cout << "#  S+E 召唤仙子  ESC 退出    #" << endl;
    cout << "#                            #" << endl;
    cout << "##############################" << endl;
    setColor(7);

    cout << endl;
    cout << "1. 开始游戏" << endl;
    cout << "2. 困难模式" << endl;
    cout << "3. 读取存档" << endl;
    cout << "4. 退出" << endl;
    cout << ">> ";

    char c = _getch();
    if (c == '2') hardMode = true;
    if (c == '3') {
        for (int s = 1; s <= 3; s++)
            if (loadGame(s)) break;
    }
    if (c == '4') exit(0);

    system("cls");
}

// ==================== 主函数 ====================
int main() {
    hideCursor();
    system("mode con cols=40 lines=35");
    system("title 10层魔塔完整版");

    titleScreen();
    initPlayerPos();
    needUpdate = true;
    windowFocused = isWindowFocused();

    while (true) {
        // === 焦点检测:切走再回来 → 强制全量重绘 ===
        bool focused = isWindowFocused();
        if (!windowFocused && focused) {
            system("cls");
            mapDrawn = false;
            lastPx = -1;
            lastPy = -1;
            needUpdate = true;
        }
        windowFocused = focused;

        // 没焦点就休眠,省 CPU
        if (!focused) {
            Sleep(100);
            continue;
        }

        // === 处理按键 ===
        while (_kbhit()) {
            char c = _getch();

            if (c == 27) return 0;

            // 功能键(F1~F5 前缀)
            if (c == 0 || c == -32) {
                c = _getch();
                if (c == 59)      saveGame(1);
                else if (c == 60) saveGame(2);
                else if (c == 61) saveGame(3);
                else if (c == 63) {
                    for (int s = 1; s <= 3; s++)
                        if (loadGame(s)) break;
                }
                continue;
            }

            if (c == 'i' || c == 'I') { showInventory(); continue; }

            move(c);
        }

        // === 每帧渲染 ===
        drawMap();       // 只画一次
        drawPlayer();    // 只动 @
        showInfo();      // 状态栏覆盖写

        Sleep(16);  // ~60FPS
    }
    return 0;
}

附言:

此代码由豆包和元宝合力生成。

本人蒟蒻,怎么会这么难的代码。

原文