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.
Where this comes from
Pong had the whole loop in 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.
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.
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);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.
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
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.