TExcelTAD
Home › Game Development Roadmap & Fundamentals › Input, Update, Render

Input, Update, Render

Every game, from Pong to an open-world RPG, runs the same loop many times per second:

text
while the game is running:
    read input        what is the player doing right now?
    update the world  move things, apply rules, run AI and physics
    render a frame    draw the current state to the screen

One pass through the loop is a frame. At 60 frames per second (FPS) the loop runs every 16.67 milliseconds. Engines hide this loop from you: Unity calls your Update() method, Godot calls _process(delta), Unreal calls Tick(DeltaTime). Your code is always one step of somebody's loop, so it helps to know what the whole loop looks like.

The three phases

Phase Job Must not
Input collect key presses, mouse, touch and gamepad state into a snapshot change the world directly
Update advance the simulation one step using the input snapshot draw anything
Render draw the current state change the state

Keeping the phases separate is the most important architectural habit in games. It lets you replay recorded input to reproduce bugs, run the update without a screen (servers, tests, this site), and later run update and render at different rates.

A loop you can play

Click the canvas, then use the arrow keys (or A and D) to move the paddle and catch the falling squares. The code is plain JavaScript with requestAnimationFrame, the browser's "call me before the next repaint" function.

html · try it yourself
<canvas id="game" width="320" height="200" tabindex="0" style="background:#111;display:block;outline:none"></canvas>
<p style="font:14px sans-serif">Click the canvas, then use ← → or A/D.</p>
<script>
  const canvas = document.getElementById("game");
  const ctx = canvas.getContext("2d");
  const held = new Set();                         // input state: keys held down
  addEventListener("keydown", (e) => { held.add(e.key); if (e.key.startsWith("Arrow")) e.preventDefault(); });
  addEventListener("keyup", (e) => held.delete(e.key));
  canvas.addEventListener("click", () => canvas.focus());

  const paddle = { x: 140, w: 40, speed: 220 };    // pixels per second
  let drop = { x: 60, y: 0, speed: 90 };
  let score = 0, last = 0;

  function update(dt) {
    const left = held.has("ArrowLeft") || held.has("a");
    const right = held.has("ArrowRight") || held.has("d");
    paddle.x += ((right ? 1 : 0) - (left ? 1 : 0)) * paddle.speed * dt;
    paddle.x = Math.max(0, Math.min(canvas.width - paddle.w, paddle.x));
    drop.y += drop.speed * dt;
    if (drop.y > 180 && drop.y < 190 && drop.x + 10 > paddle.x && drop.x < paddle.x + paddle.w) {
      score++; drop = { x: (drop.x * 7 + 53) % 300, y: 0, speed: drop.speed + 10 };
    } else if (drop.y > canvas.height) {
      drop = { x: (drop.x * 7 + 53) % 300, y: 0, speed: drop.speed };
    }
  }

  function render() {
    ctx.fillStyle = "#111"; ctx.fillRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = "#7c3aed"; ctx.fillRect(paddle.x, 185, paddle.w, 8);
    ctx.fillStyle = "#facc15"; ctx.fillRect(drop.x, drop.y, 10, 10);
    ctx.fillStyle = "#fff"; ctx.font = "14px monospace"; ctx.fillText(`score ${score}`, 8, 18);
  }

  function frame(now) {
    const dt = last ? Math.min((now - last) / 1000, 0.1) : 0;  // seconds since the last frame
    last = now;
    update(dt);     // input was gathered by the key listeners
    render();
    requestAnimationFrame(frame);
  }
  requestAnimationFrame(frame);
</script>

Notice that the key listeners only record which keys are held; the update reads that set once per frame. Reacting to input inside the event handler would move the paddle at the rate keys repeat, not at the frame rate.

The same loop, simulated

A game loop doesn't need a window. Here the "player input" is a script of commands, one per frame, so the program is deterministic and runs anywhere. This is also how many engines run automated tests and how servers run multiplayer games.

csharp · try it yourself
using System;
using System.Collections.Generic;

string[] inputScript = { "", "right", "right", "jump", "", "", "left", "" };

var world = new World();
for (int frame = 0; frame < inputScript.Length; frame++)
{
    var input = Input.From(inputScript[frame]);   // 1. input snapshot
    world.Update(input);                           // 2. update
    Console.WriteLine(
quot;{frame}: {world.Render()}"); // 3. render } record Input(int MoveX, bool Jump) { public static Input From(string command) => command switch { "left" => new Input(-1, false), "right" => new Input(1, false), "jump" => new Input(0, true), _ => new Input(0, false), }; } class World { int x = 2, height = 0, velocityY = 0; public void Update(Input input) { x = Math.Clamp(x + input.MoveX, 0, 9); if (input.Jump && height == 0) velocityY = 2; // can only jump from the ground height = Math.Max(0, height + velocityY); velocityY -= 1; // gravity if (height == 0) velocityY = 0; } public string Render() { var row = new char[10]; Array.Fill(row, '.'); row[x] = height > 0 ? '^' : '@'; return
quot;{new string(row)} x={x} height={height}"; } }
0: ..@....... x=2 height=0
1: ...@...... x=3 height=0
2: ....@..... x=4 height=0
3: ....^..... x=4 height=2
4: ....^..... x=4 height=3
5: ....^..... x=4 height=3
6: ...^...... x=3 height=2
7: ...@...... x=3 height=0

Trace frame 3: the jump sets velocityY = 2, height becomes 2, gravity drops the velocity to 1. Frame 4 adds 1 (height 3), velocity 0. Frame 5 adds 0, velocity −1. Frame 6 falls to 2 and frame 7 lands at 0. That arc is the whole idea of a jump; later lessons make it smooth and frame-rate independent.

What real loops add

Real engine loops do more between the three phases: processing OS window events, a fixed-rate physics step, animation, audio mixing, streaming assets in the background, waiting for the display (vsync) and synchronizing with the GPU. The next lessons cover the parts you control: delta time, the fixed timestep, the frame budget and pausing.

Test yourself

What is the usual order of the phases in one frame?

  1. Render, input, update
  2. Input, update, render
  3. Update, render, input
  4. Input, render, update

Why should the render phase never change game state?

  1. Drawing is slow
  2. So the simulation can run without a screen and at its own rate, and bugs are reproducible
  3. GPUs can't write to memory
  4. It is a rule of C#

In the canvas example, why do the key listeners only record held keys?

  1. Events are slow
  2. So movement happens once per frame in update, not at the keyboard repeat rate
  3. Browsers forbid moving in event handlers
  4. To save memory