← PathwayLesson 4 · Game Loop & RenderingNext lesson →

Lesson 04 · 55 minutes · Engine layer

Sixty times a second: read, update, draw.

The game loop is the heartbeat under every game you have played in this course, from Pong to Pixel Courier. You will see why games multiply by delta time, why physics uses a fixed timestep, and how draw order builds a frame.

GAME LOOPDELTA TIMEFIXED TIMESTEPDRAW ORDERINTERNAL RESOLUTION
01 / WHY

Where this comes from

Pong had the whole loop in 1972.

1972

Pong read two paddle knobs, moved a ball, checked whether it overlapped a paddle, and redrew the screen — again and again. The Tiny-Loop tutorials and this lesson use the same four steps. What changed is that today’s screens refresh at 60, 120 or 144 Hz, so “once per frame” is no longer a fixed amount of time.

02 / TIME

Timestep lab

Same jump, different computers.

Three versions of the same jump code run on a pretend computer. Choose a frame rate and compare the highest point each reaches.

Per-frame vs delta time vs fixed stepsHighest point should never depend on the screen

  • A · per frame assumes 60 fps. At 15 fps it moves a quarter as far each second — the game runs in slow motion.
  • B · × dt fixes the speed, but big steps change the arc slightly, so a jump that just clears a wall on one laptop can miss on another.
  • C · fixed steps always simulates in 1/60 s slices and only draws as often as the screen allows. Same result everywhere.
fixed timestep · JavaScript
const STEP = 1 / 60;
let acc = 0, last = performance.now();
function frame(now) {
  acc += Math.min((now - last) / 1000, 0.25); // clamp after a tab switch
  last = now;
  while (acc >= STEP) { update(STEP); acc -= STEP; } // physics: always 1/60 s
  draw();                                            // drawing: once per refresh
  requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
A student’s game runs perfectly on their laptop but the character jumps lower on the 144 Hz classroom monitor. Best fix?

03 / DRAW

Draw order lab

A frame is painted back to front.

The game draws background layers, tiles, objects, the player, particles and the HUD in a fixed order. Later layers cover earlier ones. Change the order and predict the result before you look.

Game Loop Architecture and 8-Layer Draw Order Stack Diagram showing decoupled 60Hz accumulator and painter's algorithm
The Decoupled Game Loop & Draw Order Stack: A fixed 60 Hz physics accumulator (dt = 1/60s) guarantees consistent simulation regardless of monitor refresh rate. Rendering follows the Painter’s Algorithm from Layer 0 (Sky gradient) up to Layer 7 (Screen-space HUD).

Layer orderClick the game to play while you reorder

Internal resolution. Pixel Courier always draws into a 320 × 180 canvas — 20 × 11¼ tiles — and CSS scales that picture to fit your screen. The game logic never needs to know how big the window is.
04 / SHIP

Definition of done

You understand the loop when…

0/4 checked

Evidence: Sketch the loop as a cycle diagram and label where input, fixed steps and drawing happen.

Lesson 04 complete?

Your art and settings are already saved for the next lesson.