1

我正在开发一个文本冒险游戏以练习 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...)

和:

退出初始化.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

我的理解是,在堆上分配对象,只要分配不在特定函数内进行——一旦函数调用完成,指向对象的指针可能会超出范围——将允许我“全局”访问就像任何典型的全局变量/常量一样。所有文件都包含在 main() 中,我认为我在每个不同的 cpp 文件中也包含了必要的文件。所以,我不完全明白我做错了什么。我会很感激你们能提供的任何帮助。如果有帮助的话,我也可以放置各种类文件。谢谢。

4

1 回答 1

0

就个人而言,我会编写一些代码,该代码采用描述房间等的文本文件来形成“房间列表”及其在向量中的出口。要么在你的发行版中包含这个代码和源文本文件[也许有一些加密],或者使用这个代码来生成代码,然后你编译这些代码来制作实际的游戏。

但是要访问您的全局变量,您需要extern Room* p_deserted_beach;在使用它们的地方声明它们。

于 2013-09-29T18:04:16.487 回答