Developer diaries

Developer Diary: Building a Cave That Tries to Kill You in Mothfall

A cave that narrows every zone, three kinds of moth and rock that kills on touch. How we made Mothfall scroll smoothly and fairly on a Spectrum.

Mothfall is our side-scrolling cave shooter. You fly the Lamplighter, a small lantern ship, through a cave of glowing rock while flocks of starmoths sweep in from the right. The moths are only half the danger: touch the rock and you lose a lamp. This diary is about building that cave, making it harder zone by zone, and keeping every crash fair.

A cave made of two numbers

The cave is stored very simply. For every column of the screen the game keeps two numbers: how far down the roof reaches and how far up the floor comes. Everything between them is open space. To scroll, the game moves the cave one character to the left and works out a new column on the right, nudging the roof and floor up or down a little.

That makes the shape easy to steer. How often the roof and floor are allowed to change, and the smallest gap they must leave between them, are just numbers in a table. Change the table and the whole cave changes character.

Scrolling without redrawing everything

Redrawing the whole screen every time the cave moves would be far too slow for a Spectrum. So Mothfall only touches the character cells that actually change. Most of the cave looks the same after a scroll as it did before, because one stretch of rock looks much like the next.

A trick helps here. All the rock uses the same pattern of pixels, so when a cell at the edge of the rock becomes a cell deep inside it, nothing needs drawing at all. The game only rewrites that cell's colour. We measured the cost: a scroll step takes about 15,000 T-states (ticks of the Z80's clock), and even the busiest frames, with moths, spores and shots all on screen, take about 51,000. That fits inside the Spectrum's fiftieth of a second, so the game holds a steady 50 frames a second.

Rock you can trust

In a cave game, nothing feels worse than dying when you were sure you'd missed the wall. So Mothfall doesn't keep a separate, hidden map for crashes. It checks the colours on screen. Every Spectrum character cell has its own paper and ink colours, and the rock is coloured paper, so if the Lamplighter is over rock-coloured paper, it has hit the rock. What you see is exactly what kills you.

That only works if the screen and the cave data always agree, so we tested it. Across 925 checks in testing, the colours on screen matched the game's own record of the cave every time. Our article on why Spectrum colours clash explains the 8 by 8 cells this relies on.

Fairness also matters after a crash. When you lose a lamp, the cave resets to a straight, safe tunnel so you can get going again, and for 120 frames (just over two seconds) the moths can't hurt you. The rock still can.

Three moths and a narrowing cave

Moths arrive in groups of four, or five from zone four, and snake through the cave along a wave. There are three kinds:

  • Glimmer moths (50 points) follow the wave and nothing more.
  • Dart moths (100 points) are fast and steer towards you.
  • Spore moths (150 points) are slow but fire spores that drift towards your height.

Each zone is 320 scroll steps long and has its own row in the difficulty table. Zone one has only glimmer moths, scrolls once every four frames and always leaves a gap of at least 14 rows. Darts join in zone two and spore moths in zone three, when the cave also speeds up. By zone six it scrolls every two frames, and in zones seven and eight the gap can close to nine rows while a new group of moths arrives more and more often. From zone eight the table stays at its hardest row.

The rock changes colour every zone (blue, red, magenta, green, cyan, then round again), so you can see at a glance that you've moved on. Clearing a zone pays 1,000 points times the zone number, and every second zone cleared gives you another lamp, up to five.

Sprites that don't slow things down

The ship and the moths are small sprites, 16 by 8 pixels. Moving a sprite by a single pixel on a Spectrum normally means shifting its bytes, which takes time. Mothfall stores each sprite already shifted into all eight positions, so drawing one is a straight copy. That costs memory: about 2 KB of the game's 7.9 KB is sprites. Each frame the game keeps a list of what it drew, so it can rub everything out cleanly before the next update.

The game is written in Z80 assembly and calls no Sinclair ROM routines. It has its own font and its own interrupt routine, which is why it runs in Specz's quick start, the mode that doesn't need the ROM. On a 48K you get beeper sound effects and a title tune that plays in small pieces each frame, so a key press starts the game at once. On a 128K the AY chip plays "Lanterns in the Dark" on the title and "Starmoth Run" in the caves, with a new key and beat for each zone.

Testing and playing

An autopilot loaded the tape through the real 48K and 128K start-up routines and flew on to zones 8 to 13 on both machines, using keys on one and a Kempston joystick on the other. In Specz in a browser at phone size it ran at 51 frames a second with the on-screen stick and FIRE button working.

You can play Mothfall in your browser now. A pad makes the narrow zones easier, and our guide to using a gamepad with Specz shows how to set one up. If you'd like to see how another of our games was built, read the Gemquake developer diary, then find your next game in the arcade.

More from the journal

Developer diaries ·

New to Spectrum Arcade: Gold Top Grand Prix

Race Dot Dawson's milk float against three rival milkies round eight circuits of Pennyford at dawn, and spend your gold tops at the Dairy Depot. Here's how we made it.

New games and guides, straight to your inbox

Hear first when a new Spectrum game lands in the arcade. No spam, and you can unsubscribe whenever you like.