Tutorial 2: Game States and State Transitions

Unit 1 · Game loops and real-time systems

Objectives

Make modes explicit

A game usually has modes such as title, playing, paused, and game over. A state enum gives those modes names and prevents unrelated booleans from creating impossible combinations such as “paused and title screen” at the same time.

enum GameState { TITLE, PLAYING, PAUSED, GAME_OVER }

GameState state = GameState.TITLE;

void update(Input input, double delta) {
    switch (state) {
        case TITLE -> { if (input.startPressed()) state = GameState.PLAYING; }
        case PLAYING -> updateWorld(input, delta);
        case PAUSED -> { if (input.pausePressed()) state = GameState.PLAYING; }
        case GAME_OVER -> { if (input.startPressed()) reset(); }
    }
}

Transitions should have clear triggers. Entering PAUSED may stop simulation time while keeping the render active. Leaving GAME_OVER should reset the minimum required world state, not rely on stale objects.

Exercises

  1. Draw a transition table for TITLE, PLAYING, PAUSED, and GAME_OVER.
  2. Decide which systems update in each state: world, audio, input, and UI.
  3. Implement a pause toggle that cannot trigger twice from one key press.

Self-check

  1. Why prefer an enum to several independent booleans?
  2. Should the pause menu render while the world is paused?
  3. What should a restart transition reset?

Self-Check Quiz

1. What is a state transition?

AnswerA change from one named mode to another caused by a defined event or condition.

2. Why should paused gameplay stop simulation updates?

AnswerPlayers expect the world to remain stable while they inspect the pause menu; input and UI can still update.

3. What is an impossible state combination?

AnswerA contradictory combination created by loosely related flags, such as being in both title and active gameplay modes.

Homework

  1. Implement a state machine for a small game with at least five legal transitions.
  2. Write one test or trace for every transition.
  3. Explain which parts of the loop continue during pause and why.
Sample answer

A strong design uses one GameState value, transition triggers such as start, pause, player death, and restart, and a table that documents entry and exit effects. During pause, render and menu input continue, while world simulation, timers, and enemy AI stop.

👤