【发布时间】:2014-04-27 04:30:21
【问题描述】:
试图完成我的个人项目,但我被困在这个问题上。我正在制作一个 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;
}
【问题讨论】:
-
开头的“if(map[y2][x] == '^') {”是什么?
-
不要使用
using namespace std。这真的很有害,因为在这种情况下它隐藏了您的map变量。
标签: c++ linux compiler-errors