C ++ ASCII游戏地图

时间:2015-07-31 21:21:07

标签: c++

我做了一张简单的地图,几乎一切都很好 无论如何,我无法弄清楚如何限制玩家(@)走出地图。 我尝试使用if语句,但这不能正常工作,当播放器(@)走出地图时程序崩溃。

if (input == 's' && y < HEIGHT) {
        myMap[y][x] = temp_tile;
        y++;
        temp_tile = myMap[y][x];
        myMap[y][x] = '@';
        PrintMap();
    }
    if (input == 'e' && x < WIDTH) {
        myMap[y][x] = temp_tile;
        x++;
        temp_tile = myMap[y][x];
        myMap[y][x] = '@';
        PrintMap();
    }
    if (input == 'n' && y >= 0){
        myMap[y][x] = temp_tile;
        y--;
        temp_tile = myMap[y][x];
        myMap[y][x] = '@';
        PrintMap();
    }

    if (input == 'w' && x >= 0) {
        myMap[y][x] = temp_tile;
        x--;
        temp_tile = myMap[y][x];
        myMap[y][x] = '@';
        PrintMap();
    }

完整代码:

#include <iostream>
#include <Windows.h>

using namespace std;

const int HEIGHT = 15, WIDTH = 30;
int x = 0, y = 0; //Primary player coordinates...global :-O

void GrassColor() {
    SetConsoleTextAttribute
        (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
}

void NormalColor() {
    SetConsoleTextAttribute
        (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}

void BlueColor() {
    SetConsoleTextAttribute
        (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
}

void RedColor() {
    SetConsoleTextAttribute
        (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
}

char myMap[HEIGHT][WIDTH] = {
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '~', '~', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'~', '~', '~', '~', '~', '~', '~', '~', '~', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '~', '.', '~', '~', '~', '~', '~', '~', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '~', '~', '~', '~', '~', '~', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '~', '~', '~', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '~', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' },
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.' }
};

void PrintMap() {
    cout << "(" << x << ','<< y << ")" << endl;
    GrassColor();
    for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
            if (myMap[i][j] == '.') {
                GrassColor();
                cout << myMap[i][j];
            }
            else if (myMap[i][j] == '~'){
                BlueColor();
                cout << myMap[i][j];
            }
            else if (myMap[i][j] == '@') {
                RedColor();
                cout << myMap[i][j];
            }
            else{
                NormalColor();
                cout << myMap[i][j];
            }
        }
        cout << endl;
    }
    NormalColor();
}

int main() {
    char input; //For user input.
    char temp_tile = myMap[y][x]; // '.' (0, 0);
    myMap[y][x] = '@'; // '@' (0, 0);
    PrintMap(); 

while (true) {
    cin >> input;

    if (input == 's' && y < HEIGHT) {
        myMap[y][x] = temp_tile;
        y++;
        temp_tile = myMap[y][x];
        myMap[y][x] = '@';
        PrintMap();
    }
    if (input == 'e' && x < WIDTH) {
        myMap[y][x] = temp_tile;
        x++;
        temp_tile = myMap[y][x];
        myMap[y][x] = '@';
        PrintMap();
    }
    if (input == 'n' && y >= 0){
        myMap[y][x] = temp_tile;
        y--;
        temp_tile = myMap[y][x];
        myMap[y][x] = '@';
        PrintMap();
    }

    if (input == 'w' && x >= 0) {
        myMap[y][x] = temp_tile;
        x--;
        temp_tile = myMap[y][x];
        myMap[y][x] = '@';
        PrintMap();
    }
}
    system("PAUSE");
    return 0;
}

enter image description here

1 个答案:

答案 0 :(得分:2)

if (input == 's' && y < HEIGHT)

此测试(y < HEIGHT)错误。最底部的y坐标应为HEIGHT - 1。因此,如果玩家的位置不小于HEIGHT - 1,则应停止向南移动。同样,所有其他测试也都是一个。所以:

if (input == 's' && y < HEIGHT) -> if (input == 's' && y < HEIGHT - 1)
if (input == 'e' && x < WIDTH)  -> if (input == 'e' && x < WIDTH - 1)
if (input == 'n' && y >= 0)     -> if (input == 'n' && y > 0)
if (input == 'w' && x >= 0)     -> if (input == 'w' && x > 0)