Skip to content

Engine

src/retrospeedlab/engine/ is the console every game runs on: a deterministic 8-bit machine built on Pyxel. The search runs it headless by the thousand; Pyxel draws the screens, the videos and the browser builds.

Spec Value
Screen 256 × 240 pixels, 16 colours
Memory 2 KiB of RAM, the whole game state
Speed 60 frames per second
Buttons B, SELECT, START, UP, DOWN, LEFT, RIGHT, A

The cartridge contract

A cartridge is a Python module named cartridge inside its game package. The engine loads it with load_cartridge and checks that it exposes:

Name Meaning
TITLE, VERSION The title and a semantic version like 2.0.4
RAM_SIZE = 2048 All state lives in the RAM, so bytes(ram) is a savestate
SPRITES name: (bank, x, y, width, height) for the site and the knowledge pages
reset(ram, unlocked) Powers on; unlocked offers the endless mode after a first win
step(ram, buttons) Advances one frame with the buttons held
level(ram) The level being played, counted from 0
won(ram) True from the frame the campaign is won
endless(ram) True while an endless run is on
hero(ram) (area, x, y) of the hero in the world, or None off the play field
camera(ram) The world position at the screen's top-left
dead(ram) True when the hero goes down or the run is lost
draw(ram) Draws the screen with Pyxel
load_assets(), play_sound(ram) Optional: art and sound
TAKES, load_take(take) Optional: the music takes under test, the current one first

Buttons arrive as a tuple of booleans in the order ("B", None, "SELECT", "START", "UP", "DOWN", "LEFT", "RIGHT", "A"); the empty slot is never pressed. RAM byte 0 is the mode, and mode 0 is the title.

Music plays on channels 0 to 2 and sound effects on channel 3, so M mutes the music alone. A game under a music A/B test names its takes in TAKES; the browser page names the take to play, and an unknown one stops the game instead of playing silence. Takes live outside the RAM, so every run replays the same with any take.

The sandbox

Before a cartridge loads, the engine reads its source and the collection's shared/ package and refuses it when:

  • it imports anything but bisect, collections, dataclasses, functools, heapq, itertools, math, operator, numpy, pyxel, its own package and shared/;
  • it calls open, exec, eval, compile or __import__;
  • reset, step, level or won touch Pyxel, because they run headless on the RAM alone.

Movies and verification

A movie is a msgpack file with the cartridge, its version, the start RAM and one byte of buttons per frame. record makes one, replay plays it back, and a movie of another version only replays when asked to.

The verifier replays raw inputs from power-on and reports the frame the game is won. It refuses runs longer than 30 minutes. The backend runs it in a locked-down subprocess for every highscore; you can run it yourself from src/:

cd src
python -m retrospeedlab.engine.verify retrospeedlab.games.comet_courier.cartridge < run.inputs
# {"cartridge": "retrospeedlab.games.comet_courier.cartridge", "version": "2.0.4", "won_at": 17409}

Videos and browser builds

uv run python -m retrospeedlab.engine.cli video "$PWD/src/retrospeedlab/games" "$PWD/run.movie" "$PWD/run.mp4" libx264
uv run python -m retrospeedlab.engine.cli web "$PWD/src/retrospeedlab/games" retrospeedlab.games.comet_courier.cartridge "$PWD/renders/web"

video replays a movie and encodes it with FFmpeg at twice the size, with the game's music and effects as two audio tracks. The encoder is libx264 or h264_nvenc on an NVIDIA GPU. web builds one game for the browser with the Pyxel web launcher.

engine/demo/ is a one-game collection the tests use.