试图完成我的个人项目,我坚持这个问题。我正在制作一个2D游戏,并且认为我设置得很好,但是我得到了一个错误,基本上说我的一个变量没有在很多范围内声明。这是我的代码,尝试制作一个简单的2D游戏,尝试在Linux中编译和运行。
错误具体说明:
project.cpp:52:7:错误:未在此范围内声明“map”
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;
}
答案 0 :(得分:1)
尝试将变量重命名为“map
”之外的其他内容,因为它也代表std::map
。
并且第一行“if(map[y2][x] == '^') {
”是一个错字吗?
我认为你必须移动它
cout << "Help the Brave adventurer, &, find his way through the maze, to reach the Treasure, $$!"
进入main()
答案 1 :(得分:1)
将第一行if(map[y2][x] == '^') {
视为拼写错误,
以下需要修复,至少要编译并运行**
首先,cout << "Help the Brave adventurer...
应放在main
其次,修复以下内容:
char map[17][21] =
{
"Use W,A,S,D to move", // add comma
"Press X to end game", // add comma
// ....
} ;
**似乎也存在逻辑错误!请不要在这里问所有人。