因此,问题如下:我已将数组设置为“ 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] = ↦
if ( (*cur_map)[9][19] == '#' ) {}
// mathod1: Also pointer to current map, but using decltype
decltype(map)* cur_map2 = ↦
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/