Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

A tour of the examples

The repository ships example carts under examples/ — each a standalone project you can open, run and take apart. They are all playable right here (and on the cart shelf), and most are the worked answer to one of this book’s chapters.

From a source checkout, any of them opens in the console:

cargo console -- examples/platformer     # then type `run`

Or crack open the published cartridges: save a .png from the shelf and load it — the examples embed their source, so import turns any of them back into an editable project.

sprite_move

Sprites, flipping, and a two-frame walk cycle driven by a frame counter — the Drawing chapter, in 60 lines. Source.

platformer

The capstone: run, jump, collect coins, stomp (or dodge) the badie, grab the trophy before the clock runs out. Almost every chapter of this book is in here, in a few hundred lines split into small modules:

  • map + sprite flags as the collision system (solid tiles carry flag 0, the badie’s sprites flag 1);
  • a camera that follows the hero, with the HUD drawn in screen space;
  • the falling, the walls and the edge of the level from the physics feature: the scene is one World of two seats, which owns where the hero and the badie are, how fast, and what they last ran into. The walls are declared on it once (the same flag the map marks its solid tiles with), it owns the level’s Gravity, and each actor is enlisted in one chain — the Bounds it covers, the rectangle the hero may never leave (confined_to), the wearing cell that says the badie is a badie — keeping the Member handle the chain ends in beside its own game data. A single step an update pulls the hero down, stops it at the solid tiles, holds it inside the level, patrols the badie and tells each of them what it met; the hero draws at the world’s draw_pos, so running jumps climb clean staircases;
  • nothing walks a pair of casts: the badie’s sprites are flagged, so the hero’s contacts report having met one in touches and the cart only decides whether that was a ram or a stomp — same frame, so a stomped badie is retired on the spot;
  • coins collected by rewriting the map in RAM and put back on restart;
  • the best score kept in storage across runs;
  • win/lose music held inside the game-state enum — leaving the state drops the PlayingMusic handle, stopping the song;
  • heapless::Vec and heapless::format! in a #![no_std] cart.

Source.

sfx_demo

A four-pad soundboard: each arrow key fires a slot via ctx.sfx, with the pads lighting up on the screen — the sound half of Sound and music. Source.

music_demo

Starting and stopping a song with fades, the PlayingMusic handle held in an Option, and dancing bars while it plays — the music half of the same chapter. Source.

campfire

Not a game: a scene that plays itself. Nothing reads the buttons and there is nothing to win — it is an animation that happens to be a cartridge. A fire burns under a moon and a scatter of stars, one figure sat beside it rocking in and back, another stood off to the side working through a cigarette, and an owl blinking on a branch of the old tree; the fire, the crickets and an occasional hoot loop underneath it all.

The flames are a SmokingFire, one of the SDK’s plume effects, which sit behind the off-by-default plume-effects feature: turn it on and a fire is three lines of code. Its spent flames carry on as smoke instead of vanishing, so the column reads as one effect rather than two — and the cigarette is the same Smoke, turned right down and pointed up-left. One gusty Wind from the physics feature stands in for the sway both of them do on their own, so the column wanders on the night air with a lean towards the tree and never quite repeats itself. Everything else is the cheapest animation there is: compare ctx.time() against a constant, pick one of two sprites. Scale the fire down for a candle, point it another way for an exhaust trail. Source.

stress

Not on the shelf, and not a game: a cart that deliberately allocates and burns CPU to probe the limits. It’s the one example that opts into std, and the fastest way to see the “ran out of memory” and “ran too long” error screens on purpose. Source.