19. Time and motion

Quite often you will want to make the program take a specified length of time, and for this you will find the PAUSE statement useful (especially in fast mode):

PAUSE n

stops computing and displays the picture for n frames of the television (at 50 frames per second, or 60 in America). n can be up to 32767, which gives you just under 11 minutes; if n is any bigger then it means 'PAUSE for ever'.

A pause can always be cut short by pressing a key (note that a space, or £, will cause a break as well). You have to press the key down after the pause has started.

At the end of the pause, the screen will flash.

If a PAUSE statement is used in a program that will be run in FAST mode or on the old ZX80 with the new 8K ROM, then the PAUSE statement must be followed by POKE 16437,355. The PAUSE will appear to work without doing this, but it will probably result in your program being wiped out.

This program works the second hand (here just a single dot on the edge) of a clock.

5 REM FIRST WE DRAW THE CLOCK FACE
10 FOR N=1 TO 12
20 PRINT AT 10-10*COS (N/6*PI),10+10*SIN (N/6*PI);N
30 NEXT N
35 REM NOW WE START THE CLOCK
40 FOR T=0 TO 10000
45 REM T IS THE TIME IN SECONDS
50 LET A=T/30*PI
60 LET SX=21+18*SIN A
70 LET SY=22+18*COS A
200 PLOT SX,SY
300 PAUSE 42
310 POKE 16437,255
320 UNPLOT SX,SY
400 NEXT T

(Miss out the REM statements unless you have a memory expansion board.)

This clock will run down after about 2 3/4 hours because of line 40, but you can easily make it run longer. Note how the timing is controlled by line 300. You might expect PAUSE 50 to make it tick once a second, but the computing takes a bit of time as well and has to be allowed for. This is best done by trial and error, timing the computer clock against a real one, and adjusting line 300 until they agree. (You can't do this very accurately; an adjustment of one frame in one second is 2% or half an hour a day.)

The function INKEY$ (which has no argument) reads the keyboard. If you are pressing exactly one key (or the shift key and one other key) then the result is the character that the key gives in  mode; otherwise the result is the empty string. The control characters do not have their usual effect, but give results like CHR$ 118 for newline - they are printed as "?".

Try this program, which works like a typewriter.

10 IF INKEY$<>"" THEN GOTO 10
20 IF INKEY$="" THEN GOTO 20
30 PRINT INKEY$;
40 GOTO 10

Here line 10 waits for you to lift your finger off the keyboard and line 20 waits for you to press a new key.

Remember that unlike INPUT, INKEY$ doesn't wait for you. So you don't type newline, but on the other hand if you don't type anything at all then you've missed your chance.

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 and 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, and 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, and 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 and 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