Game Development Reference
In-Depth Information
finished:
return 0;
}
A goto consists of two parts:
A label
goto statement
A label is inserted into the code, which creates a named point to which we can jump. In this example
our label is finished . We can then use this label to have the goto command jump directly to that
point.
The
I have seen this used to move between the game loop, the frontend loop and a pause loop in game
code before. It's also been known for goto to be used to jump to error handling code. This might
seem to be a good idea, but there are always better options such as using break , a state machine for
game state, or return values or exceptions for handling errors.
In this chapter we have covered the essential flow control statements provided by C++. It's now time
to put these to good use in our Text Adventure game.
Adding a Game Loop to Text Adventure
For our Text Adventure to be a compelling game, we must add a loop that allows the player to
interact. We looked at the for loop and the while loop in this chapter. A for loop provides us with a
set number of iterations, so a while loop would be better suited to our needs for a game loop.
Listing 6-12 shows how we can use an enum , functions , references, relational operators, and a while
loop to write code that begins to resemble an interactive program.
Listing 6-12. The Text Adventure Game Loop
#include <iostream>
#include <string>
using namespace std;
The Player struct and WelcomePlayer functions have not changed since Chapter 5.
struct Player
{
string m_name;
};
void WelcomePlayer(Player& player)
{
cout << "Welcome to Text Adventure!" << endl << endl;
cout << "What is your name?" << endl << endl;
 
Search WWH ::




Custom Search