我正在开发一款文字冒险游戏,以便练习C ++。到目前为止我所拥有的是这样的事情:
在main()中:
#include <iostream>
#include "game_functions1.h"
#include "game_functions2.h"
#include "RoomsInit.h"
#include "exitsInit.h"
#include "playerInit.h"
using namespace std;
int main()
{
GameDescription();
while(PlayGame) {}
return 0;
}
我已经包含了我的Room Initialization cpp文件,Exit Init文件和Player Init文件。它们看起来像这样:
RoomInit.h:
#ifndef ROOMSINIT_H_INCLUDED
#define ROOMSINIT_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
#endif // ROOMSINIT_H_INCLUDED
及其对应的cpp文件:
#include "RoomsInit.h"
#include "Room.h"
string roomName1 = "On a deserted beach";
string roomDescr1 = "You are standing on a deserted beach. To the east, a "
"crystal blue ocean\n dances in the morning sun. To the "
"west is a dense jungle, and somewhere\n far off, you can "
"hear the singing of a strange bird. The white, sandy \n"
"beach runs out of sight to the north and south.\n\n\n";
Room* p_deserted_beach = new Room(roomName1, roomDescr1);
string roomName2 = "In the ocean";
string roomDescr2 = "You are swimming in the ocean. The sunlight dances\n"
"merrily on the waves.\n\n\n";
Room* p_in_the_ocean = new Room(roomName2, roomDescr2);
string roomName3 = "Caught in a current";
string roomDescr3 = "You are caught in a current that is pulling you\n"
"toward the north. I'm not sure, but...I think this \n"
"may not have been such a good idea. Just sayin'.\n\n\n";
Room* p_caught_in_a_Current = new Room(roomName3, roomDescr3);
string roomName4 = "Caught in a strong current";
string roomDescr4 = "The current is much stronger here. Okay, now I'm\n"
"sure. This was a really bad idea. Er...how long can \n"
"you tread water...'cause...with a little luck, we should\n"
"hit the East coast of Japan in about 3 weeks.\n\n\n";
Room* p_caught_in_a_strong_current = new Room(roomName4, roomDescr4);
(21 rooms like this...)
和
ExitInit.h:
#ifndef EXITSINIT_H_INCLUDED
#define EXITSINIT_H_INCLUDED
#include <iostream>
#include <string>
#include <vector>
class Room;
using namespace std;
#endif // EXITSINIT_H_INCLUDED
和ExitInit.cpp:
#include "exitsInit.h"
#include "exit.h"
#include "room.h"
#include "RoomsInit.h"
vector<Exit*> exitVec;
Exit* p_north1 = new Exit("north", p_on_the_beach_north, true, false);
Exit* p_south1 = new Exit("south", p_on_the_beach_south1, true, false);
Exit* p_east1 = new Exit("east", p_in_the_ocean, true, false);
Exit* p_west1 = new Exit("west", p_in_the_jungle, true, false);
exitVec.push_back(p_north1);
exitVec.push_back(p_south1);
exitVec.push_back(p_east1);
exitVec.push_back(p_west1);
(*p_deserted_beach).SetExitVec(exitVec);
exitVec.clear();
Exit* p_north2 = new Exit("north", p_caught_in_a_Current, true, false);
Exit* p_south2 = new Exit("south", p_caught_in_a_Current, true, false);
Exit* p_east2 = new Exit("east", p_caught_in_a_Current, true, false);
Exit* p_west2 = new Exit("west", p_deserted_beach, true, false);
Exit* p_northeast2 = new Exit("northeast", p_caught_in_a_Current, true, false);
Exit* p_northwest2 = new Exit("northwest", p_caught_in_a_Current, true, false);
Exit* p_southeast2 = new Exit("southeast", p_caught_in_a_Current, true, false);
Exit* p_southwest2 = new Exit("southwest", p_caught_in_a_Current, true, false);
exitVec.push_back(p_north2);
exitVec.push_back(p_south2);
exitVec.push_back(p_east2);
exitVec.push_back(p_west2);
exitVec.push_back(p_northeast2);
exitVec.push_back(p_northwest2);
exitVec.push_back(p_southeast2);
exitVec.push_back(p_southwest2);
(*p_in_the_ocean).SetExitVec(exitVec);
exitVec.clear();
Exit* p_north3 = new Exit("north", p_caught_in_a_strong_current, true, false);
Exit* p_south3 = new Exit("south", p_caught_in_a_strong_current, true, false);
Exit* p_east3 = new Exit("east", p_caught_in_a_strong_current, true, false);
Exit* p_west3 = new Exit("west", p_caught_in_a_strong_current, true, false);
Exit* p_northeast3 = new Exit("northeast", p_caught_in_a_strong_current, true, false);
Exit* p_northwest3 = new Exit("northwest", p_caught_in_a_strong_current, true, false);
Exit* p_southeast3 = new Exit("southeast", p_caught_in_a_strong_current, true, false);
Exit* p_southwest3 = new Exit("southwest", p_caught_in_a_strong_current, true, false);
exitVec.push_back(p_north3);
exitVec.push_back(p_south3);
exitVec.push_back(p_east3);
exitVec.push_back(p_west3);
exitVec.push_back(p_northeast3);
exitVec.push_back(p_northwest3);
exitVec.push_back(p_southeast3);
exitVec.push_back(p_southwest3);
(*p_caught_in_a_Current).SetExitVec(exitVec);
exitVec.clear();
(etc...Exit objects for each room which also take room pointers as arguments to
their constructors. These Exit objects are passed to a vector which is, itself,
a data member of the Room class)
我在编译时遇到的问题是很多错误说明:
P_on_the_beach_south was not declared in this scope....etc etc
我的理解是在Heap上分配对象,只要分配不在特定函数中发生 - 一旦函数调用完成,指向对象的指针就会超出范围 - 将允许我'全局'访问就像任何典型的全局变量/常量一样。所有文件都包含在main()中,我认为我在每个不同的cpp文件中都包含必要的文件。所以,我并不完全明白我做错了什么。我很感激你们所能给予的任何帮助。我也可以提出各种类文件,如果这会有所帮助。谢谢。
答案 0 :(得分:0)
就个人而言,我会编写一些代码,这些代码采用描述房间的文本文件,以形成“房间列表”及其在向量中的出口。在您的发行版中包含此代码和源文本文件[可能带有一些加密],或使用此代码生成代码然后编译以制作实际游戏..
但要访问全局变量,您需要在使用它们的地方将它们声明为extern Room* p_deserted_beach;
。