⚡ Zig Guide LiveUnofficialbut fully verified
✓ Zig 0.18.0-dev.1399+c621b773aWhat's newOn an older Zig?Blograylib-zig

Lane Dodger

This project builds a three lane endless runner in the same way as the rest of this guide: no engine, no assets, and nothing that hides how it works. The game is on this page, and you can play it now. The chapters teach how to write a game that you can test. The usual answer is that you cannot test a game, and the usual result is a game whose difficulty nobody can reason about. Here the simulation imports nothing, so a program that plays the game checks every rule, and the graphics and the sound come later, on top of the simulation.

This project vendors raylib from its C sources at a pinned commit, and does not use raylib as a Zig package. The chapters explain why. The browser build needs the Emscripten SDK, and CI does not have that SDK. Thus the playable artefacts on this page are built by hand and committed.

  1. If a game loop takes a dt, the game plays differently on each machine. A fixed timestep with an accumulator is ten lines, and it gives reproducible runs.
  2. If game logic imports no graphics library, a program can test the logic when it plays the game. Here, a bot tests the rules at every seed in about one second, with no window open.
  3. Derive a difficulty curve from what is physically possible. Do not tune the curve until it feels correct. When one tuned number changes, the tuned numbers no longer agree with each other.
  4. For objects that a program spawns and destroys constantly, handles work better than pointers. A stale index silently reads the next occupant of its slot. A generation counter changes that read into a null.
  5. Sound effects can be a few hundred lines of arithmetic, not a folder of files. Waveforms are as easy to test as other code.

10 chapters.

  • A Game You Can TestA three lane endless runner that you can play here, and the one rule that lets you test the rest.
  • Vendor raylibHow to add raylib to a Zig project, why this project uses the C source, and how the game gets to a browser canvas.
  • The WorldA field with its own units, a player that slides between lanes, and the order of the work in a tick.
  • The Loop Takes No dtA fixed timestep, an accumulator, interpolation for the leftover time, and the split that the browser build needs.
  • Handles, Not PointersA pool with a fixed capacity and generational handles, and the event stream that the particles and the sound use.
  • Draw ItField units mapped onto a window, the road, the entities, and a particle system that contains no raylib types.
  • InputThe game latches presses one time in each frame and gives each press to exactly one tick. A held key must not cross the board.
  • Difficulty You Can ProveRow spacing from what is physically possible, a bot that proves that the course is solvable, and a reaction time model for playability.
  • Sound Without Sound FilesFive effects that the game synthesises at startup from oscillators and envelopes, and waveform tests that run without an audio device.
  • Build It From ScratchThe order in which to write the files, the two points where you can stop and check, and what to do when the build fails.