Game loop skeleton
The structure every game needs before it needs anything else.
Build me a game skeleton in one HTML file. Canvas, no libraries, no build step. Structure, strictly: - One `state` object holding everything mutable. - `update(dt)` — changes state only, never draws. Takes elapsed seconds, so movement is speed × dt and not per-frame. - `draw()` — reads state and renders, changes nothing. - A requestAnimationFrame loop calling both, with dt clamped so a tab returning from the background doesn't teleport everything. Include a `mode` field with 'playing', 'paused' and 'gameover' from the start, and a placeholder square I can move with the arrow keys. Then tell me where I'd add a new entity type, a collision, and a score.
Clamping dt is the line everyone omits. Without it, switching tabs for a minute produces one frame with a 60-second delta and everything ends up outside the level.