No matter how simple your game might be, chances are that there will be multiple screens or modes the player will be able to interact with. Think of it for a moment—in most games there is a title screen, which is followed by a main menu, which lets you branch into various other sub-screens that let you set the game's difficulty or level of detail. Typically, you must navigate your way through many if not most of these screens and menus before you are finally ready to play the game itself.
All these varying screens and modes fall into the game development concept of game states. Each of these screens is seen as a state and might set the game to a different mode of operation. As the game transitions from one state to another, it is setting variables, loading content and finally, drawing different graphics to the screen or presenting new ways of interaction to the player.
This recipe will show you how to define and use a state machine that lets you easily define and cleanly...