试图完成我的个人项目,但我陷入了这个问题。我正在制作一款 2d 游戏,并认为我已经设置得很好,但我得到了一个错误,这基本上表明我的变量之一没有在多个范围中声明。这是我的代码,试图制作一个简单的 2d 游戏,试图在 Linux 中编译和运行。
错误具体指出:
project.cpp:52:7: error: 'map' was not declared in this scope
if(map[y2][x] == '^') {
#include <iostream>
#include <cstdlib>
using namespace std;
cout << "Help the Brave adventurer, &, find his way through the maze, to reach the Treasure, $$!"
char map[17][21] = {
"Use W,A,S,D to move"
"Press X to end game"
"####################",
"# & ## ## # ^#",
"# ##^ ^# ^#",
"# ## ## ^# #",
"# ^## ^## #",
"# ## #^ #",
"#^ ##### #",
"# ## ^# #",
"# ##^^ #^ # #",
"# ^^## ##### # #",
"# ## # ^ #",
"# ## #^ ^# # #",
"# ##^ ^#$$#",
"####################",
};
int x = 1;
int y = 1;
int hp = 5;
bool game_run = true;
int main ()
{
while(game_run == true) {
system("clear");
for(int showMap=0; showMap<17; showMap++) {
cout << map[showMap]
}
system("pause > nul");
if(cin.get() == 's'){
int y2 = y+1;
if(map[y2][x] == ' ') {
map[y][x] = ' ';
y++;
map[y][x] = '&';
}
if(map[y2][x] == '^') {
hp--;
}
}
if(cin.get() == 'w'){
int y2 = y-1;
if(map[y2][x] == ' ') {
map[y][x] = ' ';
y--;
map[y][x] = '&';
}
if(map[y2][x] == '^') {
hp--;
}
}
if(cin.get() == 'd'){
int x2 = x+1;
if(map[y][x2] == ' ') {
map[y][x] = ' ';
x++;
map[y][x] = '&';
}
if(map[y][x2] == '^') {
hp--;
}
}
if(cin.get() == 'a'){
int x2 = x-1;
if(map[y][x2] == ' ') {
map[y][x] = ' ';
x--;
map[y][x] = '&';
}
if(map[y][x2] == '^') {
hp--;
cout << "You have run into spikes!!! Health has been decreased by 1...";
}
}
if(cin.get() == 'x'){
game_run = false;
}
if(hp == 0) {
game_run = false;
cout << "The hero has died.... game over... :(";
}
}
system("clear");
cout << "Game Over";
return 0;
}
最佳答案
尝试将变量重命名为“
map
”以外的名称,因为它也代表std::map
.第一行“
if(map[y2][x] == '^') {
”是拼写错误吗?我认为你必须移动它
cout << "Help the Brave adventurer, &, find his way through the maze, to reach the Treasure, $$!"
进入main()
关于c++ - 错误 'map' 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23319411/