c++ - 我怎么能在这个简单的 C++ 游戏中转到另一个 "map"?

标签 c++ arrays loops coordinates assign

因此,问题如下:我已将数组设置为“ map ”,玩家 (@) 可以在其中移动。但是我怎样才能让程序切换 map ,例如当玩家点击坐标时,如 [4] 和 [19]?

#include <iostream>
#include <windows.h>
using namespace std;

char plyr = '@';

char map[10][20] = {
    "###################",
    "#@                #",
    "#                 #",
    "#                  ",
    "#                  ",
    "#                 #",
    "#                 #",
    "#                 #",
    "###################"
};

char map2[10][20] = {
    "###################",
    "#  #############  #",
    "#     ##           ",
    "                   ",
    "                  #",
    "#                 #",
    "#              ####",
    "#           #######",
    "###################"

};


int x = 1;
int y = 1;

bool gameRunning = true;

int main(){

    while(gameRunning == true){
        system("cls");
        for(int disp=0; disp<10; disp++){
            cout << map[disp] << endl;  
        }

        system("pause>nul");    


        if(GetAsyncKeyState(VK_DOWN)){
                int y2 = y+1;
                if(map[y2][x] == ' '){
                    map[y][x] = ' ';
                    y++;
                    map[y][x] = plyr;
            }
        }

        if(GetAsyncKeyState(VK_UP)){
            int y2 = y-1;
            if(map[y2][x] == ' '){
                map[y][x] = ' ';
                y--;
                map[y][x] = plyr;   
            }
        }

        if(GetAsyncKeyState(VK_RIGHT)){
            int x2 = x+1;
            if(map[y][x2] == ' '){
                map[y][x] = ' ';
                x++;
                map[y][x] = plyr;
        }       
    }   

    if(GetAsyncKeyState(VK_LEFT)){
        int x2 = x-1;
        if(map[y][x2] == ' '){
            map[y][x] = ' ';
            x--;
            map[y][x] = plyr;
        }       
    }
}



    return 0;
}

最佳答案

您需要的是某种方法来抽象出当前使用的 map 。您可以通过保留指向当前 map 的指针或使用将返回对当前 map 的引用的函数来实现 - 索引变量将指示哪个 map 是当前 map 。

因此,当您的用户到达坐标时,您应该将当前 map 索引/指针更新为新的。您可能还想在某处存储应该使用哪个新 map 的信息。以下是一些方法,但还有更多方法。

decltype(map)& get_map(int map_index) {
    switch(map_index) {
      case 1:        
      return map;
      case 2:
      default:
        return map2;
    }
}

struct map_obj {
    int current_map;
    decltype(map[0])& operator[](int ind1) {   
        auto& mp = get_map(current_map);
        return mp[ind1];
    }
};

int main()
{
    // mathod1: Pointer to the current map
    char (*cur_map)[10][20] = &map;
    if ( (*cur_map)[9][19] == '#' ) {}

    // mathod1: Also pointer to current map, but using decltype
    decltype(map)* cur_map2 = &map;
    if ( (*cur_map2)[9][19] == '#' ) {}

    // mathod2: Use function which returns reference to current map
    int current_map = 0;
    if ( get_map(current_map)[9][19] == '#' ) {}

    // mathod3: Use object which uses operator[] to return row for specified column
    map_obj mp;
    mp.current_map = 1;
    if ( mp[9][19] == '#' ) {}
}

关于c++ - 我怎么能在这个简单的 C++ 游戏中转到另一个 "map"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37059802/

相关文章:

c++ - E-olymp : Cake. 给出错误答案

Python:循环中IF语句的处理不一致

javascript - jquery中的for循环错误

c++ - avr asm 从变量输出到端口

c++ - 如何进行反向转换

php - 文本的索引词

arrays - Google Apps脚本替换和更新范围内的单元格

java - 白色和黑色图像到二维数组以及经过一些操作后二维数组到Java中的图像

java - 一个 do while 循环内的 if-else 循环 另一个 do while 循环内的 if-else 循环

c++ - 复杂 C 语法的含义