问题标题: 走迷宫1.0.2

0
0
已解决
被禁言 马博闻
马博闻
初级守护
初级守护

走迷宫系列更新了

代码如下:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <windows.h>
#include <cstdio>
#include <fstream>  // 新增文件操作头文件

using namespace std;

const int WIDTH = 21;
const int HEIGHT = 21;

// 新增全局存档数据
int winCount = 0;
int loseCount = 0;

char maze[HEIGHT][WIDTH];
int px, py;        // 玩家位置
int ex, ey;        // 出口位置
int coins = 0;     // 金币数量
int stamina = 100; // 体力值
int beef = 0;      // 背包物品
int bread = 0;
int bomb = 0;

// 新增文件操作函数
void loadGameData() {
    ifstream file("走迷宫.txt");
    if (file) {
        file >> winCount >> loseCount;
        file.close();
    }
}

void saveGameData() {
    ofstream file("走迷宫.txt");
    if (file) {
        file << winCount << endl << loseCount;
        file.close();
    }
}

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

void generateMaze(int startX, int startY) {
    vector<pair<int, int> > stack;
    stack.push_back(make_pair(startX, startY));

    while (!stack.empty()) {
        int x = stack.back().first;
        int y = stack.back().second;
        vector<pair<int, int> > neighbors;

        int dirs[4][2] = {{0, -2}, {0, 2}, {-2, 0}, {2, 0}};
        for (int i = 0; i < 4; i++) {
            int nx = x + dirs[i][0];
            int ny = y + dirs[i][1];
            if (nx >= 0 && nx < WIDTH && ny >= 0 && ny < HEIGHT && maze[ny][nx] == '#') {
                neighbors.push_back(make_pair(nx, ny));
            }
        }

        if (!neighbors.empty()) {
            int idx = rand() % neighbors.size();
            int nx = neighbors[idx].first;
            int ny = neighbors[idx].second;

            int midX = (x + nx) / 2;
            int midY = (y + ny) / 2;
            maze[midY][midX] = ' ';
            maze[ny][nx] = ' ';

            stack.push_back(make_pair(nx, ny));
        } else {
            stack.pop_back();
        }
    }
}

void initGame() {
    // 初始化迷宫
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            maze[y][x] = '#';
        }
    }

    // 生成迷宫
    maze[1][1] = ' ';
    generateMaze(1, 1);

    // 生成金币(5个)
    for (int i = 0; i < 5; ++i) {
        int x, y;
        do {
            x = rand() % WIDTH;
            y = rand() % HEIGHT;
        } while (maze[y][x] != ' ' || (x == 1 && y == 1) || (x == WIDTH-2 && y == HEIGHT-2));
        maze[y][x] = 'C';
    }

    // 初始化位置和状态
    px = py = 1;
    ex = WIDTH - 2;
    ey = HEIGHT - 2;
    maze[ey][ex] = ' ';
    bomb = 0;
    bread = 0;
    beef = 0;
    coins = 0;
    stamina = 100;
}

void draw() {
    system("cls");
    setColor(7);
    bool flag=0;
    cout << "[体力: ";
    if(stamina>50)     setColor(10);
    else if(stamina>10)setColor(14);
    else{
        setColor(4);
        flag=1;
    }
    cout << stamina;
    setColor(7);
    cout << "] [金币: " << coins << "]" << endl;
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            if (x == px && y == py) {
                setColor(10);
                cout << "Yo";
            } else if (x == ex && y == ey) {
                setColor(11);
                cout << "M ";
            } else if (maze[y][x] == 'C') {
                setColor(14);
                cout << "Co";
            } else if (maze[y][x] == '#') {
                setColor(392);
                cout << "##";
            } else {
                setColor(7);
                cout << "  ";
            }
        }
        cout << endl;
    }

    // 显示状态信息
    setColor(7);
    cout << "背包 1.牛肉*" << beef << "  2.面包*" << bread << "  3.炸药*" << bomb << endl;
    cout << "按E打开商店" << endl;

    if(flag==1)Beep(500, 300);
}

void move(int dx, int dy) {
    int newX = px + dx;
    int newY = py + dy;
    if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT) {
        if (maze[newY][newX] == ' ' || maze[newY][newX] == 'C') {
            if (maze[newY][newX] == 'C') {
                Beep(1046, 200);
                coins += 1;
                maze[newY][newX] = ' ';
            }
            px = newX;
            py = newY;
            stamina = max(0, stamina - 1);

            // 30%概率生成一个金币
            if (rand() % 100 < 30) {
                int x, y;
                do {
                    x = rand() % WIDTH;
                    y = rand() % HEIGHT;
                } while (maze[y][x] != ' ' || (x == px && y == py) || (x == ex && y == ey));
                maze[y][x] = 'C';
            }
        }
    }
}

void useBomb() {
    Beep(500, 300);
    Sleep(600);
    Beep(500, 300);
    Sleep(600);
    Beep(500, 300);
    Sleep(600);
    Beep(900, 300);
    for (int dy = -2; dy <= 2; dy++) {
        for (int dx = -2; dx <= 2; dx++) {
            int x = px + dx;
            int y = py + dy;
            if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT) {
                if (maze[y][x] == '#' && rand() % 2 == 0) {
                    maze[y][x] = ' ';
                }
            }
        }
    }
}

void shop() {
    while (true) {
        system("cls");
        cout << "+=========== 商店 ===========+\n";
        cout << "|1. 牛肉(+10体力)  - 5 金币|\n";
        cout << "|2. 面包(+5体力)   - 3 金币|\n";
        cout << "|3. 炸药(破坏障碍) - 10金币|\n";
        cout << "+============================+\n";
        cout << "按Q返回\n";
        cout << "当前金币: " << coins << endl;

        char input = getch();
        if (input == 'q' || input == 'Q') break;

        switch (input) {
        case '1':
            if (coins >= 5) {
                coins -= 5;
                beef++;
            }
            break;
        case '2':
            if (coins >= 3) {
                coins -= 3;
                bread++;
            }
            break;
        case '3':
            if (coins >= 10) {
                coins -= 10;
                bomb++;
            }
            break;
        }
    }
}

void drawLobby(int selectedOption) {
    system("cls");
    setColor(8);
    cout << "+===========================+" << endl;
    cout << "|       ";
    setColor(270);
    cout <<"迷宫大冒险";
    setColor(8);
    cout <<"          |" << endl;
    cout << "+===========================+" << endl;

    // 新增战绩显示
    setColor(7);
    cout << "历史战绩:";
    setColor(6);
    cout <<"胜利 ";
    setColor(7);
    cout << winCount << " 次,";
    setColor(4);
    cout <<"失败 ";
    setColor(7);
    cout << loseCount << " 次\n\n";

    setColor(9);
    cout << "+============+" << endl;
    cout << "|  ";
    setColor(7);
    cout << "开始游戏";
    setColor(9);
    cout << "  |";
    if (selectedOption == 0) {
        setColor(11);
        cout << " <<";
    }
    setColor(9);
    cout << endl;
    cout << "+============+" << endl;

    cout << endl;

    cout << "+============+" << endl;
    cout << "|  ";
    setColor(7);
    cout << "退出游戏";
    setColor(9);
    cout << "  |";
    if (selectedOption == 1) {
        setColor(11);
        cout << " <<";
    }
    setColor(9);
    cout << endl;
    cout << "+============+" << endl;
}

int lobby() {
    int selectedOption = 0;
    while (true) {
        drawLobby(selectedOption);
        char input = getch();
        switch (tolower(input)) {
        case 'w':
            if (selectedOption > 0) selectedOption--;
            break;
        case 's':
            if (selectedOption < 1) selectedOption++;
            break;
        case 'a':
            return selectedOption;
        }
    }
}

int main() {
    srand(time(0));
    loadGameData(); // 加载存档数据

    while (true) {
        int option = lobby();
        if (option == 0) {
            initGame();
            while (true) {
                draw();

                if (px == ex && py == ey) {
                    system("cls");
                    setColor(110);
                    cout << "\n恭喜逃出迷宫!\n";
                    Beep(600, 150);
                    Beep(800, 150);
                    Beep(1000, 150);
                    Beep(1200, 150);
                    Sleep(1500);
                    setColor(7);
                    winCount++;
                    saveGameData();
                    break;
                }
                if (stamina <= 0) {
                    system("cls");
                    setColor(64);
                    cout << "\n体力耗尽!\n";
                    Beep(1200, 150);
                    Beep(1000, 150);
                    Beep(800, 150);
                    Beep(600, 150);
                    Sleep(1500);
                    setColor(7);
                    loseCount++;
                    saveGameData();
                    break;
                }

                char input = getch();
                switch (tolower(input)) {
                case 'w': move(0, -1); break;
                case 's': move(0, 1); break;
                case 'a': move(-1, 0); break;
                case 'd': move(1, 0); break;
                case 'e': shop(); break;
                case '1':
                    if (beef > 0) {
                        stamina = min(100, stamina + 10);
                        beef--;
                    }
                    break;
                case '2':
                    if (bread > 0) {
                        stamina = min(100, stamina + 5);
                        bread--;
                    }
                    break;
                case '3':
                    if (bomb > 0) {
                        useBomb();
                        bomb--;
                    }
                    break;
                }
            }
        } else {
            saveGameData();
            system("cls");
            setColor(7);
            cout << endl << "退出游戏,再见!";
            break;
        }
    }
    return 0;
}

优化了迷宫显示,添加了玩家过去的战绩系统。

部分截图:

游戏大厅,按w s移动光标,按a选中

 

游戏界面,按w s a d移动玩家,按数字键选中背包里的物品

 

商店界面,按数字键购买商品


0
已采纳
石峻帆
石峻帆
高级守护
高级守护

建议:在网上查找如何清屏不闪屏(双缓冲、移动光标)

0
被禁言 马博闻
马博闻
初级守护
初级守护

                            必看!!!

游玩过程中如有任何BUG或疑问,欢迎指出;

游戏过程中请使用英文输入法!!!

0
我要回答