Lessons and the learning game

Python games in the terminal

Build a text adventure on a derelict space station, one real game system at a time, on a Raspberry Pi 4 with two LEDs for alarms.

The course

Python games in the terminal is a beginner Coding course in seven lessons, for anyone who has just finished Python from zero. The whole course is one game: a crawl through Tycho-9, a space station nobody has answered from in years. It runs on the gallery's two-LED Pi build: red on GPIO17 is the station's alarm, yellow on GPIO27 the reactor. You play by typing in the Serial Monitor, and every step is checked against what the Pi prints or what its pins do.

  1. A story that branches: input() for the player's answer, strip() and lower() so " NORTH" still works, if, elif and else, a story tree with five endings, a loop that asks until the answer makes sense, and a red radiation alarm.
  2. Random and dice: randint for a d6, a seed that gives the same rolls every run, a roll() function for 2d6 skill checks, the odds of 8 or more counted (15 of 36) and then measured over 3,600 rolls, a loot table with random.choice, and a yellow light for a double six.
  3. Game state in a dict: the player as one dict, functions that change it, an inventory list inside it, a verb and object command loop, an oxygen alarm, and a game over when the air runs out.
  4. Save and load: the state as JSON, written to a file and read back unchanged, a first run with no save, save and load commands that work as a time machine, and an autosave every third turn with %.
  5. A map of rooms: the station as data, each room a dict with exits, a loop that walks it, a new room added without touching the code, a door locked behind a keycard, and rooms explored counted with a set.
  6. Enemies with simple rules: an enemy as a dict, a turn-based fight, a coward that flees when hurt, a table of enemies whose behavior is a rule name (coward, brave, slow), a duel where you choose to attack or patch, and a danger light for low hp.
  7. Boss: a station crawler: the finale.

The finale

The boss is a complete game built from the pieces of the first six lessons. Six rooms from the pod to the reactor deck, a fuse in the lab and a keycard on the bridge, each guarded by an enemy that fights by its own rule. Every command costs 5 percent of your air, and the red LED warns when air or hp runs low. The keycard opens the ladder down to the reactor, and using the fuse there lights the yellow LED: Tycho-9 is back online. A save stores the whole world, the rooms as well as the player, so a taken item never comes back. The last step is yours: a new room, a new enemy rule, a second ending or a score, playtested by a friend.

What the Pi engine does differently

The Pi runs MicroPython 1.29, not CPython, and a few things show in a game:

  • A dict does not keep the order keys went in, so exits are printed with sorted() and saves use json.dumps(..., sort_keys=True).
  • random is a different generator from CPython's, so a seed gives different numbers than on a real Pi. They still repeat run after run, which is why the lessons seed the dice wherever they quote exact rolls.
  • A file the program writes lasts while it runs. Press Stop and the save is gone, so the lessons save and load in the same run. On a real Pi the file would stay on the SD card.
  • When a program ends, the Pi's pins go back to inputs and the LEDs go dark, so a story that lights one ends with a last input().

input(), json, random.choice and gpiozero's LED work as they do on a real Pi.