Notes »
BASIC programming
I bought ZX Spectrum Games Code Club: Twenty fun games to code and learn to initiate my younger son to programming during the holidays, but the idea has a couple of problems:
- The keyboard layout of the ZX Spectrum doesn’t match a modern qwerty keyboard, so there are some weird key combinations to get some characters.
- The listings are too long –my son is 6 years old–.
Even if is not perfect, I still like it. These are some notes and short programs to get started.
The ZX Spectrum keyboard
The layout makes things harder, but with the 128K BASIC is still nice enough to give it a go.
Some quick ones –this is in Linux using FUSE, ZXSEC uses CTRL instead of ALT–:
- ALT + p is
"
- ALT + z is
:
- ALT + 8 is
(
and ALT + 9 is)
The boy seems to remember this faster than me!
Tips and tricks
Use the 128K basic, because the editor is quite nice.
Emulate a ZX Spectrum +3 so you can save the programs to 3"
discs:
- Add a new disk to the A: drive
- Format with
format "a:"
cat
to show the disc contentssave "name.bas"
to save the programload "name.bas"
to load the program
If you want to use your own editor and skip the ZX Spectrum editing experience, you can also cross-compile from text to tape and test the output on an emulator.
Programs
Most of these don’t have anything specific to Sinclair BASIC and should work in other interpreters (e.g. “Guess a number” works as is on an Amstrad CPC).
Guess a number
10 REM Guess a number
20 CLS: RANDOMIZE
30 LET n=INT (RND * 9)+1 : LET t=1
100 INPUT "Guess a number from 1 to 9", g
120 PRINT "Your guess is "; g
130 IF g<n THEN PRINT "The number is bigger"
140 IF g>n THEN PRINT "The number is smaller"
150 IF g=n THEN GO TO 200
160 LET t=t+1: GO TO 100
200 PRINT "You won in "; t; " turns"
Guess a word
10 REM Guess the word
20 INPUT "Enter the word: ", w$
30 LET g$=w$: FOR i=1 TO LEN (g$): LET g$(i)="-": NEXT i
40 LET t=0: LET u$="": CLS
100 PRINT " Word: "; g$: PRINT "Turns: "; t: PRINT " Used: "; u$
110 INPUT "Enter a letter (empty to exit): "; l$
115 IF l$="" THEN STOP
120 IF LEN (l$)<>1 THEN PRINT "Enter only ONE letter": GO TO 110
130 FOR i=1 TO LEN (w$)
135 IF w$(i)=l$(1) THEN LET g$(i)=l$(1)
140 NEXT i
150 LET t=t+1: LET u$=u$ + l$(1)
160 IF w$=g$ THEN GO TO 200
170 GO TO 100
200 PRINT "You won in "; t; " turns, the word was: "; w$
Squash bugs!
When a space bug appears on the radar, press the right key for that quadrant to fire (keys from 1 to 8).
If you press the right one, the bug will be squashed. If you don’t (or are too slow), the bug will escape.
10 REM Space bug hunt by JJM and VMP. Use 1 to 8 to squash bugs!
15 RANDOMIZE
20 REM Load bug graphic
30 RESTORE 50
40 FOR i=0 TO 7: READ b: POKE USR "E"+i,b: NEXT i
50 DATA 32, 24, 165, 90, 90, 165, 24, 32
60 REM Bug placing functions
70 DEF FN x(n)=16+6*SIN (n/4*PI)
80 DEF FN y(n)=10-6*COS (n/4*PI)
90 REM Draw bug radar
100 PAPER 0: BORDER 0: CLS
110 INK 5: CIRCLE 130, 92, 70: CIRCLE 130, 92, 20: INK 3
120 FOR n=1 TO 8
130 PRINT AT 10-10*COS (n/4*PI),16+10*SIN (n/4*PI); n
140 NEXT n
150 LET score=0
200 REM Main loop (number of bugs)
220 FOR c=1 TO 10
225 REM Random pause
230 FOR i=0 TO INT (RND*300) +10: NEXT i
240 REM Quadrant to have the bug
250 LET q=INT (RND*8)+1
260 INK 2: PRINT AT FN y (q), FN x (q); "{E}"
265 REM Read input (smaller number for a harder game)
270 FOR i=0 TO 120
280 LET r$=INKEY$
285 IF r$<>"" THEN GO TO 295
290 NEXT i
295 REM Did we kill the bug?
300 IF VAL ("0"+r$)<>q THEN BORDER 2: BEEP .2,-30: BORDER 0: GO TO 320
305 REM Yes! Add score
310 LET score=score+1: BEEP .1,10: BEEP 0.1,20
315 REM Erase the bug
320 PRINT AT FN y (q), FN x (q); " "
330 NEXT c
335 REM End of game
340 CLS: INK 7: PRINT "You squashed "; score; " bugs!"
350 FOR i=0 TO 200: NEXT i: REM to let the user see the message
360 INPUT "Play again? (y/n)", r$
370 IF r$="y" THEN GO TO 100
Links to resources
- Sinclair ZX Spectrum BASIC Programming
- Usborne 1980s Computer Books
- JSSpeccy 3: a ZX Spectrum emulator for the browser