我正在写一个简短的文字冒险游戏,该游戏已准备好,但现在我想用主菜单做点什么。请注意,整个游戏都在DOS中。
这就是我想要实现的目标:
按以下方式创建主菜单,但使用while和switch循环。开关循环将包含案例(案例1:,案例2:,案例3:等),具有以下选项(将在循环上方cout)
cout << "[1] Play\n";
cout << "[2] Credits\n";
cout << "[3] Exit\n";
现在,文本冒险游戏太大了,不能放入这个循环,因为嵌套变得越来越难以阅读。游戏本身也存在循环,同时还有循环切换。我现在想做的事情如下,但我不知道该怎么做。我将用伪代码编写它:
*open file game_name.cpp*
If player presses 1
Insert actual_game.cpp
Play game until over or user presses escape
else if player presses 2
Show credits
Return to main menu
else if player presses 3
Terminate the session
else
Shows the player that an invalid option has been chosen
关键是我想要包含多个.cpp文件,而不是将所有代码放在一个文件中(actual_game.cpp)。这样做的最佳方式是什么?
答案 0 :(得分:2)
这个问题和答案与你的非常相似,请看一下:
如果有什么不清楚的地方,请告诉我。底线是您不插入代码文件 - 编译程序后无论如何都不存在。您可以调用函数来响应每个条件 - 这些函数将依次执行相关逻辑。您需要将游戏组织成一组功能,其中每个功能在游戏中执行一项特定的工作(并且可能会调用更专业的功能来处理游戏中的各个位置等)。
以下是一个例子:
// In GameFunctions.h:
bool StartGame ();
bool ShowCredits ();
bool ExitGame ();
// Add all function definitions here or create multiple header files to hold
// groups of function definitions. Include these headers files in your CPP files.
// In GameFunctions.cpp:
#include <iostream>
#include "GameFunctions.h"
using namespace std;
int main ( int argc, const char* argv[] )
{
int nKeyPress; // this holds the key pressed by the user
bool bContinue = true;
while ( bContinue )
{
... // read a key here into nKeyPress
switch ( nKeyPress )
{
case 1:
bContinue = StartGame ();
break;
case 2:
bContinue = ShowCredits ();
break;
case 3:
bContinue = ExitGame ();
break;
}
}
}
...
bool StartGame ()
{
// initialize your game here
InitGame ();
// Show the first room of your game and start waiting for
// user input (the user making various selections in the game).
// You'll invoke other rooms from this function as you respond to
// user selections.
ShowRoom ( 1 );
return ( true );
}
bool ShowCredits ()
{
... // show credits here
return ( true );
}
bool ExitGame ()
{
// do cleanup (if you have any to do) here
return ( false );
}
您还可以将游戏代码分解为多个.cpp
和.h
文件,以将您的功能分组为逻辑组。即使您不使用类,将所有代码放在单个.cpp文件中通常也是个坏主意,除非您的游戏非常非常短。因此,您可以创建多个.cpp文件,例如,每个房间一个:每个.cpp文件将保存代码以处理游戏中的特定房间。您需要头文件中的函数定义,并且需要将所有头文件包含在您打算使用的特定.cpp文件中。 (但是,您不需要在每个.cpp中包含每个.h - 您只需要包含您打算在单个.cpp中使用的定义的标头。)
最后,您的游戏将由几个.cpp和.h文件组成,并且将具有许多功能:一些将读取用户输入,一些将在屏幕上显示消息,一些可能跟踪用户的得分,有些人会在玩家第一次进入之前初始化房间等等。
您可能需要标准C或C ++库中的其他头文件,具体取决于您尝试使用的标准功能。
答案 1 :(得分:1)
对于使用该函数的C ++编译器,它只需知道函数的签名,而不是整个函数。在编译所有.cpp文件之后,链接器必须知道整个函数,以便它可以将所有部分链接到应用程序中。函数的签名(也称为“函数声明”)通常存储在头文件(example.h)中,实际函数(也称为“函数定义”)通常存储在源文件(example.cpp)中。在一个.cpp文件中,您可以调用另一个.cpp文件中定义的函数,只需添加一个include行,您可以告诉编译器在哪里查找该函数的声明(#include“example.h”):
-----------------
Main project file
-----------------
#include "actual_game.h"
#include "credits.h"
int main()
{
for (;;)
{
PrintMainMenu();
int choice = GetUsersChoice();
if (choice == CHOICE_PLAY_GAME)
PlayTheGame(); // This function is found in files actual_game.h and .cpp
else if (choice == CHOICE_SHOW_CREDITS)
ShowCredits(); // This function is found in files credits.h and .cpp
else if (choice == CHOICE_TERMINATE)
break;
else
ShowInvalidOptionMessage();
}
return 0;
}
------------------
File actual_game.h
------------------
void PlayTheGame();
--------------------
File actual_game.cpp
--------------------
#include "actual_game.h"
void PlayTheGame()
{
// The body of the function. If this function is very large and difficult to
// read by humans then divide it somehow to several other functions, that can
// be put in several files, and so easier to handle and maintain
}