简体   繁体   English

在这个简单的C ++游戏中,我怎么去另一个“地图”?

[英]How could I go to another “map” in this simple C++ game?

So, the problem is the following: I have set up arrays as "map"s, in which the player (@) can move. 因此,问题如下:我将数组设置为“地图”,播放器(@)可以在其中移动。 But how could I make the program switch maps, when, for example the player hits a coordinate, like [4] and [19]? 但是,例如当玩家击中坐标[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;
}

What you need is some way to abstract out the currently used map. 您需要某种方法来抽象出当前使用的地图。 You can do it by keeping a pointer to current map, or by using a function which will return a reference to current map - index variable will indicate which map is current. 您可以通过保留指向当前地图的指针或使用返回当前地图引用的函数来实现此目的-索引变量将指示当前地图。

So when your user reaches coordinate, you should update current map index/pointer to a new one. 因此,当用户达到坐标时,应将当前地图索引/指针更新为新的。 You might also want to store somewhere information, which new map should be used. 您可能还希望将信息存储在某处,应该使用哪个新地图。 Below are few aproaches, but there are more. 以下是一些方法,但还有更多方法。

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] == '#' ) {}
}

You could add another dimension to the array like this: 您可以像这样向数组添加另一个维度:

#include <iostream>

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

void display_map(int level)
{
    std::cout << "level: " << (level + 1) << '\n';

    for(int y = 0; y < 9; ++y)
        std::cout << map[level][y] << '\n';
}

int main()
{
    // Display both maps:

    for(int level = 0; level < 2; ++level)
    {
        display_map(level);

        std::cout << '\n';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM