Skip to content

Give meteors a gentle drift

  • Sinclair ZX Spectrum
  • Assembly

A meteor moving sideways every update would cross the screen too quickly. We will give it a small signed drift, applied less often than its downward movement.

Each object gains offset 4: drift. Its five-byte record is now X, Y, active, downward speed, drift. Event records also gain a drift byte, becoming four bytes long. Update every stride together: the pool walk, the reserved pool size and the event-address calculation.

Source · view and edit
ZX Spectrum
Companion file: assets.inc

The include line reads this editable data when you assemble. Keep it beside your source for a local build.

Edit the source, then assemble.

Edits are saved in this browser. Keep a downloaded copy of work you want to keep.

Focus the Spectrum screen to use the keyboard, or hold these buttons. Assemble again for a fresh machine.

Run the program to inspect it. Editing source does not change the running machine.

ship_x
—
wave
—
ticks
—
Read the active object records

These rows read the twenty-slot pool. Empty slots are omitted. The display samples RAM while the program runs, so values can change between observations.

SlotXYKind / activeSpeedDrift

The accepted drift values are -1, 0 and +1 pixel. The inspector displays the signed interpretation. In the byte table, -1 is stored as 255: adding it to X has the same low-byte result as subtracting one.

Use the course counter as a cadence

Drift needs to count course steps, so this checkpoint adds a word, ticks. After count_objects, the main loop adds one to it with ld hl,(ticks), inc hl and ld (ticks),hl: sixteen-bit INC carries from the low byte into the high byte. ticks sits inside the run block, so every run starts it at zero.

The drift code reads the low byte of ticks and applies and 3. Only a zero result permits horizontal motion. That is once every four course steps. Downward motion still happens on every step, at each object’s own speed.

This separates two rates: a meteor falling four pixels per step travels sixteen pixels down while drifting just one pixel sideways. At 25 steps per second, nonzero horizontal drift averages 6.25 pixels per second.

meteor-storm.asm, excerpt
 ld a,(ticks)
 and 3
 jr nz,drift_done
 ld a,(ix+0)
 add a,(ix+4)
 cp 8
 jr c,drift_left_edge
 cp 225
 jr nc,drift_right_edge
 jr drift_store
drift_left_edge:
 ld (ix+4),1
 ld a,8
 jr drift_store
drift_right_edge:
 ld (ix+4),255
 ld a,224
drift_store:
 ld (ix+0),a

The addition uses an ordinary byte operation. At x=80 with stored drift 255, the low byte of 80+255 is 79. We interpret that stored pattern as -1 because of the agreement established by the data, not because memory remembers a signed type.

Reflect before leaving the playfield

After adding drift, the code clamps X to 8 or 224 if needed and reverses the stored direction. At the left edge it writes +1; at the right edge it writes 255. This gives the next horizontal update a direction back into the playfield.

A meteor at x=8 has drift -1. What are X and drift after its next horizontal update?

Show the explanation

The proposed X is 7, below the allowed bound. The routine stores X=8 and drift=+1. It does not store 7 for one frame and repair it later. The next drift opportunity moves to 9.

Change and 3 in the drift gate to and 7. Sideways movement now happens every eight course steps while downward movement stays unchanged. Try it before making drift stronger: frequency and distance per change are separate design controls.

Restore the accepted four-step cadence. The slight uncertainty should make dodging more interesting without making trajectories unreadable. Stars, when added next, will deliberately ignore drift and fall straight down.

Sources

Zilog, Z80 CPU User Manual, UM008011-0816, instruction descriptions for the operations shown in this lesson. The running experiment supplies the program-specific observations.

Run it here

This unit's program, built for you.

Program
meteor-storm.tap
Machine
ZX Spectrum 48K (PAL)

Download meteor-storm.tap