Skip to content

Turn back to brake

Use counterthrust to slow down with the existing flight rules.

  • Sinclair ZX Spectrum
  • BASIC

The ship already has everything it needs to brake. We will use the existing flight program to find out how, without adding any numbered lines.

Complete source at this checkpoint
drift.bas
  10 GO SUB 7000
 100 LET x=48: LET y=56: LET vx=0: LET vy=0: LET h=2: LET steps=0
 110 GO SUB 1000: GO SUB 3000
 200 LET k$=INKEY$
 210 IF k$="q" OR k$="Q" THEN GO TO 9000
 220 IF k$="r" OR k$="R" THEN GO TO 100
 230 GO SUB 3000
 240 IF k$="o" OR k$="O" THEN LET h=h+1
 250 IF k$="p" OR k$="P" THEN LET h=h-1
 260 IF h=9 THEN LET h=1
 270 IF h=0 THEN LET h=8
 280 IF k$<>" " THEN GO TO 320
 290 LET vx=vx+.2 *a(h): LET vy=vy+.2 *b(h)
 300 LET speed=SQR (vx*vx+vy*vy)
 310 IF speed>3 THEN LET vx=3 *vx/speed: LET vy=3 *vy/speed
 320 LET nx=x+vx: LET ny=y+vy
 330 IF nx<22 OR nx>233 OR ny<30 OR ny>145 THEN GO SUB 3000: LET e$="Hull lost. Try a gentler burn.": GO TO 4000
 340 LET x=nx: LET y=ny: LET steps=steps+1
 350 GO SUB 3000
 370 GO TO 200
1000 BORDER 0: PAPER 0: INK 7: CLS
1010 PRINT AT 0,2; INK 5;"DRIFT";AT 0,19; INK 7;"FREE FLIGHT"
1020 PRINT AT 1,2;"Turn. Burn. Coast. Brake."
1030 PLOT INK 1;15,24: DRAW INK 1;0,128: DRAW INK 1;225,0: DRAW INK 1;0,-128: DRAW INK 1;-225,0
1060 PRINT AT 19,2; INK 5;"O/P turn   SPACE thrust"
1070 PRINT AT 20,2; INK 7;"Turn back to slow down."
1080 PRINT AT 21,2; INK 5;"R retry              Q quit";
1090 RETURN
3000 LET px=INT (x+.5): LET py=INT (y+.5)
3010 PLOT INK 7; OVER 1;px+c(h),py+d(h)
3020 DRAW INK 7; OVER 1;e(h)-c(h),f(h)-d(h)
3030 DRAW INK 7; OVER 1;g(h)-e(h),j(h)-f(h)
3040 DRAW INK 7; OVER 1;c(h)-g(h),d(h)-j(h): RETURN
4000 PRINT AT 1,0;"                                ";AT 1,2; INK 6;e$
4010 PRINT AT 20,2;"R plays again. Q quits.      "
4020 IF INKEY$<>"" THEN GO TO 4020
4030 LET k$=INKEY$
4040 IF k$="q" OR k$="Q" THEN GO TO 9000
4050 IF k$="r" OR k$="R" THEN GO TO 100
4060 GO TO 4030
7000 DIM a(8): DIM b(8): DIM c(8): DIM d(8): DIM e(8): DIM f(8): DIM g(8): DIM j(8)
7010 FOR i=1 TO 8: LET angle=(i-1)*PI/4
7020 LET a(i)=COS angle: LET b(i)=SIN angle
7030 LET c(i)=INT (5 *a(i)+.5): LET d(i)=INT (5 *b(i)+.5)
7040 LET e(i)=INT (-3 *a(i)-3 *b(i)+.5): LET f(i)=INT (-3 *b(i)+3 *a(i)+.5)
7050 LET g(i)=INT (-3 *a(i)+3 *b(i)+.5): LET j(i)=INT (-3 *b(i)-3 *a(i)+.5)
7060 NEXT i: RETURN
9000 PAPER 0: INK 7: PRINT AT 21,0;"Finished. RUN to try again.     ";: STOP

Push against the motion

Start a fresh attempt with R. Give the northeast-facing ship a short burn, release SPACE and let it coast. Turn through four headings until the nose points southwest. Then give another short burn.

Watch the travel, not just the nose. The second burn should slow the northeast motion. Keep burning for too long and the ship begins moving southwest instead. The same thrust can accelerate, brake or reverse motion, depending on the velocity already present.

Turning takes updates too. The ship carries on travelling while you rotate, so begin well before the wall. There is no automatic drag and no separate brake key.

Count burns on paper

Here is a trace with exactly four northeast burn updates, followed later by exactly four southwest burn updates. Each component changes by about .1414 per burn; the values below are rounded for reading.

Event vx vy
Start 0 0
Four northeast burns .5657 .5657
Coast and turn to southwest .5657 .5657
One southwest burn .4243 .4243
Two southwest burns .2828 .2828
Three southwest burns .1414 .1414
Four southwest burns approximately 0 approximately 0

The equal and opposite changes cancel velocity. They do not cancel the distance already travelled. The ship stops somewhere ahead of its starting point.

This trace counts updates, not how many seconds a key is held. In manual play, two holds that feel equally long can be read a different number of times. Use the movement to judge the burn rather than expecting a stopwatch to guarantee a stop.

Inspect what remains

Press Q to stop, then enter this unnumbered command at the BASIC prompt:

BASIC
PRINT vx,vy

STOP leaves the variables available to inspect. If holding Q left repeated q characters in the editor, remove those before entering PRINT. Positive vx means east; negative vx means west. Positive vy means north; negative vy means south.

A small residual value can remain after apparently equal opposite burns because the calculations are approximate. Distinguish that tiny remainder from an extra whole burn’s worth of velocity. RUN gives you a fresh attempt.

The ship is moving east, but its nose points north. Will turning the nose south and burning bring it to rest?

Show the explanation

No. Southward thrust subtracts from vy, while the eastward vx remains. To oppose a purely eastward velocity, point west. The direction opposite the nose is useful for braking only when the nose was pointing along the current motion.

Try a second flight with burns in two different directions. Now the motion contains both contributions. You can remove them separately, or steer a burn against their combined direction. With only eight available headings, small corrections can take patience.

Our next change makes this much easier to read: the game will show the velocity we have just inspected.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 3. Chapter 3 introduces program control and STOP. The trace follows this program’s .2-per-burn rule, below the speed cap.

Run it here

This unit's program, built for you.

Program
drift.tap
Machine
ZX Spectrum 48K (PAL)

Download drift.tap