C++ 我怎么能去另一家;“地图”;在这个简单的C++;游戏

C++ 我怎么能去另一家;“地图”;在这个简单的C++;游戏,c++,arrays,loops,coordinates,assign,C++,Arrays,Loops,Coordinates,Assign,所以,问题在于:我已经将数组设置为“map”,玩家(@)可以在其中移动。但是,当玩家点击一个坐标,比如[4]和[19]时,我如何制作程序切换贴图呢 #include <iostream> #include <windows.h> using namespace std; char plyr = '@'; char map[10][20] = { "###################", "#@ #", "#

所以,问题在于:我已经将数组设置为“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;
}
#包括
#包括
使用名称空间std;
字符plyr='@';
字符映射[10][20]={
"###################",
"#@                #",
"#                 #",
"#                  ",
"#                  ",
"#                 #",
"#                 #",
"#                 #",
"###################"
};
char map2[10][20]={
"###################",
"#  #############  #",
"#     ##           ",
"                   ",
"                  #",
"#                 #",
"#              ####",
"#           #######",
"###################"
};
int x=1;
int y=1;
bool gameRunning=true;
int main(){
while(gameRunning==true){
系统(“cls”);

对于(int disp=0;disp,可以向数组中添加另一个维度,如下所示:

#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';
    }
}
#包括
字符映射[2][10][20]=
{
{
"###################",
"#@                #",
"#                 #",
"#                  ",
"#                  ",
"#                 #",
"#                 #",
"#                 #",
"###################"
}
,
{
"###################",
"#  #############  #",
"#     ##           ",
"                   ",
"                  #",
"#                 #",
"#              ####",
"#           #######",
"###################"
}
};
无效显示图(整数级)
{

std::cout您可以向数组中添加另一个维度,如下所示:

#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';
    }
}
#包括
字符映射[2][10][20]=
{
{
"###################",
"#@                #",
"#                 #",
"#                  ",
"#                  ",
"#                 #",
"#                 #",
"#                 #",
"###################"
}
,
{
"###################",
"#  #############  #",
"#     ##           ",
"                   ",
"                  #",
"#                 #",
"#              ####",
"#           #######",
"###################"
}
};
无效显示图(整数级)
{

std::cout您需要的是提取当前使用的映射的某种方法。您可以通过保留指向当前映射的指针,或者通过使用一个函数返回对当前映射的引用来实现这一点。索引变量将指示哪个映射是当前映射

因此,当用户到达坐标时,您应该将当前地图索引/指针更新为一个新的索引/指针。您可能还希望将信息存储在某个位置,即应该使用哪个新地图。下面是一些方法,但还有更多方法

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

您需要的是提取当前使用的映射的某种方法。您可以通过保留指向当前映射的指针,或者通过使用将返回对当前映射的引用的函数来实现这一点。索引变量将指示哪个映射是当前映射

因此,当用户到达坐标时,您应该将当前地图索引/指针更新为一个新的索引/指针。您可能还希望将信息存储在某个位置,即应该使用哪个新地图。下面是一些方法,但还有更多方法

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

也许你应该分解代码,然后通过引用或指针传递映射到该函数。你可能需要一点抽象2个映射,生成指针,并在需要时切换指针。也许你应该分解代码,然后通过引用或指针传递映射到该函数。你可能需要一点抽象2个映射,生成指针,并在需要时切换指针这是最简单的方法:-)我可以这样做吗?使用一个变量来控制第一个“映射”维度吗?问题是,它只是打印出一堆指针…@amsterdamn,因为当你打印出来时,你需要使用
z
变量来选择映射:
cout@amsterdamn哦,
z
需要从零开始计数,而不是从一开始。好的,第一个问题解决了,但现在我尝试做一个if语句,所以当结束坐标([z][endcoordy][endcoordx])时等于'plyr'字符,它将z增加1,但它只是破坏了游戏并显示一些随机的东西。我怎么可能解决这个问题?这是最简单的:-)我可以这样做吗?使用一个变量来控制第一个'map'维度吗?问题是,它只是打印出一堆指针…@amsterdamn,因为如果你打印出来,你需要使用
z
变量来选择地图:
cout@amsterdamn-Oh和
z
需要从零开始计数,而不是从一开始计数。第一个问题已经解决了,但现在我试着做了一个if语句,所以当结束坐标([z][endcoordy][endcoordx])等于'plyr'字符,它将z增加1,但它只是破坏游戏并显示一些随机的东西。我怎么可能解决这个问题?