ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 19.2 - Time & motion

Exercises

1. What happens if you miss out line 10 in the typewriter program?

2. Why can you not type space or £ in the typewriter program?

Here is a modified program that gives you a space if you type cursor right (shifted 8).

10 IF INKEY$<>"" THEN GOTO 10

20 IF INKEY$="" THEN GOTO 20

30 LET A$=INKEY$

40 IF A$=CHR$ 115 THEN GOTO 110

90 PRINT A$;

100 GOTO 10

110 PRINT " ";

120 GOTO 10

Note how we read INKEY$ into A$ in line 30. It would be possible to miss this out & replace A$ by INKEY$ in lines 40 & 0, but there would always be a chance that INKEY$ could change between the lines.

Add some more programs so that if you type NEWLINE (CHR$ 118) it gives you a new line.

3. Another way of using INKEY$ is in conjunction with PAUSE, as in this alternative typewriter program.

10 PAUSE 40000

20 POKE 16437,255

30 PRINT INKEY$

40 GOTO 10

To make this work, why is it essential that a pause should not finish if it finds you already pressing a key when it starts?

This method has the disadvantage that the screen flashes, but in fast mode it is the only way of doing it. Run the program in fast mode, & notice that the computer takes the opportunity of a pause to display the television picture.

4. This one will drive you mad. The computer displays a number, which you (or an innocent victim) shall type back. To begin with you have a second to do it in, but if you get it wrong you get a longer time for the next number, while if you get it right you get less time for the next one. The idea is to get it going as fast as possible, & then press Q to see your score - the higher the better.

10 LET T=50

15 REM T=NUMBER OF FRAMES PER GO - INITIALLY 50 FOR 1 SECOND

20 SCROLL

30 LET A$=CHR$ INT (RND*10+CODE "0")

35 REM A$ IS A RANDOM DIGIT

40 PRINT A$

45 PAUSE T

50 POKE 16437,255

60 LET B$=INKEY$

70 IF B$="Q" THEN GOTO 200

80 IF A$=B$ THEN GOTO 150

90 PRINT "NO GOOD"

100 LET T=T*1.1

110 GOTO 20

150 PRINT "OK"

160 LET T=T*0.9

170 GOTO 20

200 SCROLL

210 PRINT "YOUR SCORE IS "; INT (500/T)

5. (Only for those with extra RAM.) Using the straight line routine in chapter 15, change the second hand program so that it also shows minute & hour hands, drawing them every minute. (Make the hour hand shorter.) If you're feeling ambitious, arrange so that every quarter of an hour it puts on some kind of a show.

6. (For fun.) Try this:

10 IF INKEY$ = "" THEN GOTO 10

20 PRINT AT 11.14; "OUCH"

30 IF INKEY$<>"" THEN GOTO 30

40 PRINT AT 11,14; "      "

50 GOTO 10

Sinclair ZX Spectrum

  Previous Page Back Next Page