No history yet

The Game Loop

The Heartbeat of a Game

Every video game, from the simplest 2D scroller to a massive open-world epic, runs on a single, fundamental concept: the game loop. Think of it as the game's heartbeat. It's a continuous, rapid cycle that keeps the game world alive and responsive. Without it, the screen would be a static image.

This is the core structure of a game, where the game continuously processes user input, updates the game state, and renders the game world.

The game doesn't just wait for you to press a button. It's constantly cycling, thousands of times per second, through three main phases: processing input, updating the game's logic, and drawing the result to the screen. This ensures that even when you do nothing, the world keeps moving. Clouds drift, characters idle, and music plays. The loop is what makes a game a living simulation rather than a slideshow.

Before the loop can even start, the game must go through an initialization phase. This is where it loads assets like graphics and sounds into memory, sets up the initial game state (placing the player character at the start, setting the score to zero), and prepares everything for action. When the game ends, or the player quits, a shutdown phase runs to clean up, release memory, and save progress. The game loop sits between these two bookends.

The Three Phases

Let's break down the cycle.

  1. Process Input: The first step in each cycle is to check what the player is doing. Did they press the jump button? Move the joystick? Click the mouse? The game gathers all these inputs so it can act on them in the next phase. If there's no input, the game simply notes that and moves on.

  2. Update Game State: This is where the game's rules are applied. Based on the player's input (or lack thereof), the game updates everything that needs to change. If the player pressed jump, the character's vertical velocity is updated. If an enemy's timer is up, it might fire a projectile. This phase handles physics, , AI logic, and score changes. It's the brain of the operation, modifying the internal data that defines the game world.

  3. Draw (or Render): Once the game state has been fully updated, it's time to show the player what happened. The game takes the new state of every object—their positions, orientations, and animations—and draws a single picture, or frame, onto the screen. This happens so quickly that your brain perceives it as smooth motion, like a flipbook.

Keeping Consistent Time

A major challenge for any game loop is hardware variation. A fast gaming PC might complete the loop thousands of times per second, while an older console might be much slower. If a character's movement is tied directly to the loop's speed, they would move at superhuman speeds on the fast PC and in slow motion on the console.

This is where comes in. Instead of moving a character a fixed amount each frame, we move them an amount based on how much time has passed since the last frame. Delta time (often shortened to dt) is simply the duration of the previous frame in seconds.

new_position=old_position+(speed×Δt)new\_position = old\_position + (speed \times \Delta t)

If a game is running at 60 frames per second (FPS), delta time will be about 1/60th of a second (or 0.0167s). If it drops to 30 FPS, delta time will be 1/30th of a second (0.0333s). The character moves a larger distance during the slower frame, but because fewer frames happen per second, the character's actual speed remains constant. This is called frame-independent movement, and it's essential for creating a consistent and fair gameplay experience for everyone.

Now that you understand the basic rhythm of a game, let's test your knowledge.

Quiz Questions 1/6

What is the primary function of the game loop?

Quiz Questions 2/6

Which of the following lists the three main phases of the game loop in the correct, most common order?

The game loop is a simple yet powerful construct that forms the foundation of all interactive digital experiences. By understanding its phases and the importance of time management, you've grasped the central mechanism that brings game worlds to life.