The main loop that runs everything sixty times a second, and the three lines that start the game.
// ---------- loop ----------
function frame(now) {
const dt = Math.min(0.033, (now - last) / 1000);
last = now;
update(dt);
ctx.setTransform(dpr * scale, 0, 0, dpr * scale, 0, 0);
ctx.save();
if (shake > 0) ctx.translate((Math.random() - 0.5) * shake, (Math.random() - 0.5) * shake);
drawBackground();
drawGates();
if (state !== 'ready') { drawShip(); drawParts(); }
ctx.restore();
if (flash > 0) { ctx.fillStyle = 'rgba(255,255,255,' + (flash / 14 * 0.5) + ')'; ctx.fillRect(0, 0, W, H); }
drawHUD();
drawOverlays();
requestAnimationFrame(frame);
}
reset();
state = 'ready';
requestAnimationFrame(frame);
</script>
</body>
</html>This file is the smallest one, and it's the one that makes everything else run. It holds the main loop and the three lines that start the game.
function frame(now) {
const dt = Math.min(0.033, (now - last) / 1000);
last = now;
update(dt);
...
requestAnimationFrame(frame);
}
frame() runs about 60 times per second, for as long as the page is open. Here's what one call does, in order:
1. COMPUTE dt: dt = (now - last) / 1000 — the seconds since the last frame. now is a timestamp the browser passes in (milliseconds since the page loaded). dt is clamped to a maximum of 0.033 seconds (about 2 frames). Why clamp? If the tab was hidden, or the computer hiccuped, the gap between frames could be seconds — and without the clamp the ship would fall hundreds of pixels in one frame. The clamp means the game slows down instead of teleporting. last = now — remember this frame's time for the next one.
2. UPDATE: update(dt) — file 05. Move everything, check collisions, score, land, crash. This is where the game state changes.
3. SET UP THE DRAWING TRANSFORM: ctx.setTransform(dpr * scale, 0, 0, dpr * scale, 0, 0); This is the magic that makes the 480x640 logical playfield (file 01) appear at the right size on any screen. It scales every drawing call by dpr * scale, so code that draws at (108, 300) in logical units lands at the right real pixels. Everything drawn after this line is in logical coordinates.
4. SCREEN SHAKE: if (shake > 0) ctx.translate((Math.random() - 0.5) * shake, ...); When you crash, shake is set to 10 (file 02) and counts down by 1 per frame (file 05). While it's above zero, the whole scene is shifted by a random amount up to shake pixels in each direction. That's the screen shake. It's wrapped in ctx.save() / ctx.restore() so it only affects the world drawing, not the HUD.
5. DRAW THE WORLD: drawBackground() — sky, nebula, planets, stars. drawGates() — gate slabs, gap markers, pad. if (state !== 'ready') { drawShip(); drawParts(); } — the ship and its exhaust particles, but NOT on the title screen (the ready overlay draws its own bobbing ship instead).
6. FLASH OVERLAY: if (flash > 0) — a white rectangle over the whole screen, opacity flash/14 * 0.5. flash is set to 14 on a crash and counts down, so the screen flashes white and fades. This is drawn OUTSIDE the save/restore, so the shake doesn't move it.
7. DRAW THE HUD AND OVERLAYS: drawHUD() — score, level, pad distance, best, mute icon, footer. drawOverlays() — the title screen, game-over screen, level-clear screen, and level intro. These are drawn last so they're on top of everything.
8. SCHEDULE THE NEXT FRAME: requestAnimationFrame(frame) — asks the browser to call frame() again before the next screen repaint. This is what makes it a loop: frame() ends by asking to be called again. When the tab is hidden the browser pauses it, and resumes when you come back.
reset();
state = 'ready';
requestAnimationFrame(frame);
That's it. Three lines and the game is running. From here on, frame() calls itself forever, update() moves things, the draw functions paint them, and the input handlers (file 04) change the state.
If you want to change how the game FEELS, start in 02 (the constants) and 05 (the burn and gravity). If you want to change how it LOOKS, start in 06. If you want to change how it SOUNDS, start in 03.