Kitchen Sync

See all articles from SYNC v2 n2

Using Fun Features to Make a Great Toy

The first thing we did when we finished building our ZX81 kits was to play with them—poking through the manual, pressing new keys to see what they did, writing little test programs to see what could be done. It did not take us long to decide that the ZX81 had some features that made it a lot of fun to have around.

Of all the new features good old Clive included in the bigger, better 8K Basic, our favorites are SLOW mode, the PLOT command, and the INKEY$ function. In this column we will play with these features a bit, and end up showing you how to use these features to write a drawing program (similar to the famous mechanical Etch-A-Sketch) that was suggested by Paul Merchant, a sophomore in the Merrimack Valley High School in Penacook, New Hampshire.

Let us look at our three favorite new features one at a time.

The SLOW Mode

If you have had your ZX81 more than a week, you have no doubt discovered SLOW mode (called “compute and display mode” by Sinclair—the word “slow” has bad connotations in connection with computers). If you have not quite grasped what happens in slow mode, the following program will help:

10 PRINT "E";
20 GOTO 10

Now switch to fast mode, by pressing the SHIFT and F keys and then pressing ENTER. The screen will go blank and 0/0 will appear at the bottom. Press RUN, then ENTER. The screen will go dark for a short while and then come back with a screen full of E’s.

Thrilling, no?

Press SHIFT D and ENTER. You are now in SLOW (sluggish, slothful, languorous—actually, we never saw much wrong with “SLOW” …) mode. Press RUN and ENTER again. You get the same end result, but this time you see the E’s zipping across the screen.

“That didn’t take much longer than doing it in fast mode,” you are thinking. You wonder why you would ever want to go back to FAST mode, right? Before you jump to conclusions, try the following program, first in FAST mode, then in SLOW mode:

10 FOR I=1 TO 15
20 PRINT I,SQR I
30 NEXT I

In FAST mode it should have taken about five seconds to print out the table of square roots, but in SLOW mode, over thirty. Obviously, when you are doing anything more than nothing at all, there is indeed a tradeoff in speed when you use SLOW mode, since your computer’s mighty little Z80 microprocessor has to stop running your program every fraction of a second to make sure that the screen display is up-to-date.

It is important to realize that one of the most interesting features of FAST and SLOW is that they are commands, just like any of the others on the ZX81 keyboard, and, as such, they can be used within a program. In other words, you can execute most of a program in SLOW mode, but then go into “warp-drive” when you need to do heavy computations.

The PLOT Command

The next fun feature of the ZX81 is the PLOT command. In geometry-talk, PLOT plots x- and y-coordinates in the first quadrant. In real life, PLOT puts graphics “dots” on the screen. A “dot” is one quarter of a character-space. If you enter

PLOT 1,1

you get a dot in the lower left corner of the screen.

PLOT provides you with a very versatile tool for creating figures and drawings. You have the graphics set of the ZX81, which contains every possible combination of “quarters-of-a-character-space”(a.k.a. “dots”) turned on or off, along with the ability to put (or “unput”) a dot anywhere on the screen.

Run the following program in SLOW mode. (By the way, we have found that it is a lot easier to enter programs while in FAST mode, and use the screen flicker feedback to know when we have pressed a key. You will soon find that the slow mode can get awfully slow when you are entering a long program . . . try it and see.)

10 FOR I=1 TO 40
20 PLOT I,I
30 NEXT I
40 FOR I=1 TO 40
50 UNPLOT I,I
60 NEXT I
70 GOTO 10

If you do not get too excited by straight lines, try this recurring sine wave program:

10 FOR N=0 TO 63
20 PLOT N,10*SIN (4*N*PI/63)+20
30 NEXT N
40 FOR N=0 TO 63
50 UNPLOT N,10*SIN (4*N*PI/63)+20
60 NEXT N
70 GOTO 10

The INKEY$ Function

Last comes the INKEY$ function which is probably the most useful command you have available for making your ZX81 respond easily to input. The value of INKEY$ is a character string that contains the key currently being pressed on the keyboard. If no key is being pressed, INKEY$ returns a null (empty) string.

To test out INKEY$, run the following program in SLOW mode:

10 PRINT INKEY$;
20 GOTO 10

The screen will blank out, and you will probably see a couple of characters in the upper left corner—do not worry about them. If you press a key, that key will be displayed on the screen. But try pressing a key and holding it down. See lots of characters on the screen? INKEY$ keeps checking to see if a key is pressed, and keeps finding the one you are pressing.

By now, you have probably figured out that the few spurious ?’s in the upper left corner of the screen came from holding down the ENTER key too long after pressing RUN. If you did not get any question marks, you have a remarkably light and quick typing touch.

Putting SLOW, PLOT, and INKEY$ Together

Now that we have told you a little about our three favorite commands, let’s tie them all together and write a program that will do something interesting. The following program will keep children, guests, and even computer haters busy for hours, doodling away—if you stop playing with it yourself long enough for them to get a chance.

10 LET X=31
20 LEY Y=21
30 PLOT X,Y
40 LET K$=INKEY$
50 IF K$="5" THEN LET X=X-1
60 IF K$="6" THEN LET Y=Y-1
70 IF K$="7" THEN LET Y=Y+1
80 IF K$="8" THEN LET X=X+1
90 IF K$="E" THEN CLS
200 GOTO 30

When you run the program in slow mode, a dot appears in the center of the screen. Press one of the arrow keys (unshifted). You will have a short line in the direction of the arrow you chose. Hold an arrow key down, and a longer line is drawn. Press E and the screen is erased, leaving the dot where you last left it. Go crazy.

If you can tear yourself away from this great toy (and it took us hours to tire), you might want to try a few improvements to the program. Remember, though, that the larger the program gets, the less memory there is for the screen display, and the slower the program runs. That is a fact of life not only with the ZX81, but with virtually every computer.

First improvement: get rid of the bug. What is it? You probably found out already. If you hold down the 8 key (right arrow), eventually the dot stops at the right edge of the screen and the program stops, leaving a “B/30” on the bottom of the screen. This means, “You tried to go off the screen.” The same thing will happen with the 7 key (up arrow). But something even stranger happens if you go too far to the left, or too far down the screen.

Run the program again and bring the dot fairly close to the left of the screen (with the 5 key). Press “E” to erase the screen, then hold down the 5 key again. If you hold it long enough, the dot will reach the left side of the screen, then bounce back and begin moving to the right every time you hit the left arrow. If you move the dot down a couple of lines, you will find that now the dot moves left when you press the right arrow. Why does it go left when you want right, and vice-versa? Because every time you press the left arrow key, the value of x is decremented. X becomes a negative number. But the PLOT command uses the absolute value of the coordinates (the number, without the plus or minus sign), so -5 turns out to be 5, which moves the dot left, thus the mirror image effect.

You can do something about this, of course. If you add lines to the program to check whether the dot has reached an edge of the screen, you are doing what computer people call “bounds checking.” If you try to move the dot off the edge of the screen (“out of bounds”), bounds checking notices, and tells the program not to let you do it.

Add the following lines to the program:

100 IF X<0 THEN LET X=0
110 IF X>63 THEN LET X=63
120 IF Y<0 THEN LET Y=0
130 IF Y>43 THEN LET Y=43

Now if you reach the edge of the screen, the dot stops, and further commands to move in that direction are ignored. And, if you think back to childhood (or to your children’s childhood, if you were born before Etch-A-Sketch . . .), you will remember that the controls of an Etch-A-Sketch work just like this.

While you are thinking of that mechanical Etch-A-Sketch, did you ever wish that there was a way to move the pen without drawing a line, so that you could draw pictures or letters that did not touch each other? Well, here is one way a computer beats the mechanical model—it is easy to fix this program so that you can “lift the pen.” Add these lines to your program:

5 LET PEN=1
30 IF PEN=1 THEN PLOT X,Y
35 IF PEN=2 THEN UNPLOT X,Y
92 IF K$="D" THEN LET PEN=1
94 IF K$="U" THEN LET PEN=2
96 IF K$="M" THEN LET PEN=0

With these changes, you can move the dot drawing a line, move it erasing a line, and move it without changing the screen. All we have done is to create a new variable (we called it PEN) that keeps track of the state of the “pen” (which is what we will call the dot from here on). Every time the pen gets to a new location, the program checks the PEN variable to see whether to draw a dot, erase one, or do nothing. If you press the D key, PEN will be set equal to 1, which means “draw a dot.” If you press the U key, PEN will be set to 2, which means “erase (undraw) a dot.” And the M key sets PEN to 0, which means “move, but do not do anything,” so pen movements are invisible on the screen.

As soon as you have tried moving the pen in “M” mode, you will want another feature—some way of figuring out where the pen is without drawing any lines. The following two program lines make a blinking dot appear at the current location of the pen whenever you press the F key:

133 IF K$="F" THEN PLOT X,Y
135 IF K$="F" THEN UNPLOT X,Y

This works in both “Draw” and “Erase” modes, but always turns the current position off when in “Move” mode. Why? We will leave you to figure that one out. If you want to always see where the pen is, you might want to remove the conditional parts of lines 133 and 135

IF K$="F" THEN

and make the current pen position always blink.

What else can you add? Keys to draw diagonal lines might be nice, and you can probably think of a lot more. But, as you have probably noticed by now, every new feature added leaves less room on the screen and makes the program run slower. If you have lots of memory, you will not run out of screen space, and only the decrease in speed will be a factor.

The Improved Sketch Program

If you have just 1K of memory, you might want to try this improved version of the program that uses less memory:

10 LET PL=0
20 LET X=31
30 LET Y=21
40 LET K$=INKEY$
50 IF K$="5" AND X>0 THEN LET X=X-1
60 IF K$="6" AND Y>0 THEN LET Y=Y-1
70 IF K$="7" AND Y<43 THEN LET Y=Y+1
80 IF K$="8" AND X<63 THEN LET X=X+1
90 IF K$="E" THEN CLS
100 IF K$="P" THEN PL=1-PL
200 IF PL=1 THEN GOTO 250
210 PLOT X,Y
220 UNPLOT X,Y
230 GOTO 40
250 PLOT X,Y
260 PLOT X,Y
270 GOTO 40

This version has a blinking cursor to mark the current position of the pen, and also lets you see the pen location when you cross over a black line. Pressing the P key turns the “write” function of the pen on or off. You can turn the pen off for going from one part of the screen to another without leaving a line, or use the turned-off pen to erase lines.