PVG
3. The Game Loop — Program Video Games

3. The Game Loop

Every moment you've ever played in a video game - every bullet fired, every chest opened, every enemy slain - has been orchestrated by a single, invisible conductor: the Game Loop.

What's Really Going On

Every running game must do three things, over and over:

  1. Handle input
  2. Update state
  3. Produce output

The Game Loop is simply the Fractal Code Cycle applied at the level of an entire game frame.

A Closer Look

A game loop can be as simple as code like this:

for is_running {
    input()
    update()
    render()
}

No matter what kind of game you are creating, this general structure will usually be found.

Zooming Out

The Game Loop is the overarching cycle that is responsible for calling almost all of the code in a game. The only exceptions are initialisation and cleanup code.

Some game loops can be simple, like the example above.

Others grow more sophisticated - for instance, using a fixed timestep to guarantee consistent updates.

More complex loops may even catch up after dropped frames to maintain a smooth simulation.

Try It Yourself

Imagine you're building a farming game. What would need to happen every frame? List 3 systems you would update inside your Game Loop.

The Big Idea

The Game Loop keeps everything on-time, advancing your game into the future. Though it may be a small amount of code, its importance cannot be overstated.