Game Development Reference
In-Depth Information
Chapter 20
Game State Management
Normally, you don't immediately start playing when a game application starts. For example, in the
Jewel Jam game, you see a title screen before playing. More complicated games have menus for
options, menus for selecting different levels, screens to display the high score after finishing a level,
a menu to select different characters and attributes, and so on. In Jewel Jam, adding a title screen
wasn't that difficult, because the title screen itself had very little interaction. However, when you
look at the example in the previous chapter, you can see that building a screen with a few options
and controls can result in quite a lot of code. You can imagine that when you add more menus and
screens to the game, it's going to be a pain to manage which objects belong to which screen and
when they should be drawn or updated.
Generally, these different menus and screens are called game states . In some programs, they're called
scenes , and the object responsible for managing the scenes is the director . Sometimes a distinction
is made between game modes and game states . In that case, things like menus, the main playing
screen, and so on are game modes , whereas “level finished” and “game over” are game states .
This topic follows a simplified paradigm and calls everything game states . To deal with these
different game states, you need a manager . In this chapter, you develop the main classes needed
for such a structure and see how you can use it to display different menus and switch between them
while keeping the code cleanly separated.
Basics of Managing Game States
When you want to deal properly with game states, you need to make sure of the following:
Game states should be run completely independently. In other words, you don't
want to have to deal with the options menu screen or the “game over” screen
while you're in the game-playing state.
There should be an easy way to define game states, find game states, and
switch between them. That way, when the player presses the Options button in
the title screen, you can easily switch to the options menu state.
265
 
Search WWH ::




Custom Search