Pong - The Game Loop

The Game.Runner provides the raw 60fps frame loop of update() + draw(). Within that
process the game itself (in this case Pong) needs to provide the game state machine that
starts with a basic menu, waits for user input to start a game, run the game until there
is a winner, or the user abandons it, and then go back to the menu and repeat.

Initialization

For the Pong game, we are going to use some images to display the ‘menu’ that tells
the user to:

press ‘1’ for a single player game

press ‘2’ for a double player game

This introduces a subtle twist into the GameRunner and Pong relationship. Loading
images is an asynchronous process and we can’t display the images until they are loaded,
so we dont want the GameRunner to go into its loop until the images are fully loaded.

In order to resolve this issue, the Pong initialize method uses a callback pattern to
know when the images are finished loaded and doesn’t tell the GameRunner to start()
until that process is complete:

Keyboard Input

When a keyboard event occurs, the GameRunner automatically calls any
onkeydown() or onkeyup() methods in the game (if they exist), so the
Pong game can detect appropriate keys and start or stop games appropriately: