MOVING GRAPHICS (16K ZX81)
from ZX Computing 1, Summer 1982, pages 109-110

What do you do when you want aliens and asteroids to burn around your TV
screen? The PRINT AT on the ZX81 gives you one solution.

You make an object move - or appear to move - on the TV screen by printing it
in one position, holding the display for a moment, then unprinting the old
position just as you reprint in a new position.

The PRINT AT function on the ZX81, although it is slow, makes it easy to
position an object just where you want it. For our first simple program, load
PROGRAM ONE and press RUN to see it in action. What you'll see, all being well,
is a black square move down the screen diagonally. Note that you can 'chain'
PRINT AT commands as in line 30.

This program shows the simplest kind of moving graphics programming, and
although far from satisfactory in terms of the result displayed on the screen,
at least it should give you a clear idea of how to produce one kind of effect.
Its main disadvantage is that the screen is blank while the computer is working
out the new PRINT AT position for the black blob. As you can see from running
this program, the RND function - when running the computer in SLOW - is
particularly slow. As you begin to elaborate the program you're working on, and
you want the computer to do more and more, you'll find this blank between
subsequent PRINT ATs of the blob becomes intolerable. Try, for example, adding
35 LET Z = X**Y and see how slow the program becomes. This can be overcome to
some extent by assigning two other variables to the PRINT AT position which
hold it for unprinting while the computer is working out new positions.

Run program two, and you'll see how much more satisfactory it is. If you want
it to run forever, add:
 57 IF X greater than 19 THEN RUN.

Now moving a blob down the screen in an irregular slide is not of much use to
anyone. Load and run program number three, which makes a ball bounce around the
screen. Although this does not use a second set of variables to unprint, it is
fairly satisfactory. You'll see that lines 70 and 80 check each time through
the program to ensure that the ball has not 'hit the sides', and if it has done
so, ensures that the variables which determine the next position of the ball (C
and D) are changed.

Program four does use new variables (E and F) to hold the old position while
the new one is being worked out, and these are used not as an 'unprint' but
rather to print a trailing series of full stops. Run these programs a few
times, and examine the listings, and you're sure to be able to work out ways to
make them more effective.

A second way of approaching the moving graphics problem is to use SCROLL to
move the screen up a line, before reprinting what you need on the line it has
just left. Load and run program five, HAILSTORM, and you'll see how it works in
practice. In this program, you are the black block, PRINT AT-ted in line 30.
You control the position of the blob by using INKEY$, pressing on keys "8" (to
move the blob right) and "5" (to move it left). The whole of line 80 ensures
that the blob will not move off the screen, and saves valuable memory by
placing the interpretation of both INKEY$ and checking the screen limits on
either side, within one line. Line 990 prints the blank to remove the black
blob. This blank is printed just before the screen SCROLLS upward, to move all
the zeroes upward. Your aim in the game - needless to say - is to avoid the
hailstones. You'll remember that at the start of this article we mentioned that
the RND function was fairly slow. To minimise the delay, we've used a single
pair of random numbers (generated in lines 60 and 65) four times in line 70.

Lines 100 and 110 are very interesting, and are crucial for producing a
worthwhile program using the SCROLL facility. Line 100 moves the PRINT AT
position to A+C, B which is a line down from where the black blob is at
present. Using line 110, the computer then looks up this position in the
display file, to see if anything is there. If it finds the code for "0" in that
position - that is one line below the black blob - it knows the black blob is
going to hit a hailstone, and prints out your score, the value of the variable
S. The Q at the end of this line causes the ZX81 to stop with a 2 error code,
meaning an unassigned variable has been encountered. This is 'cheaper', in
memory terms, than running the line twice, with THEN STOP at the end of the
second line, or of saying IF ... THEN GOTO 1020, where line 1020 reads PRINT S.
Other memory-saving devices you can glean from this listing include the use of
VAL a string, instead of using a number. Believe it or not, LET P=1 uses up
more memory, because of the way the ZX81 memory works, than LET P=VAL "1", and
CODE "0" is cheaper than the number 28.

In our final program, SQUASH (program number six), a ball (which looks
remarkably like an asterisk) is bounced off a black blob at the bottom of the
screen, which you move right and left by using the "Q" and "P" keys. If you
manage to 'bounce it', your score (shown in the top right hand corner of the
screen) increases by one, and the game continues. Miss the ball, and the fun is
over. Load and run this program to see how it works. Note that because a random
number does not have to be generated between successive reprints off the ball,
the time the screen is blank is very short (only while the computer processes
lines 65 and 70), so a second pair of variables to 'hold the ball' are not
needed.

Line 5 initialises the vertical position of the ball, and line 10 sets up the
player (black blob) starting position. Line 15 starts the count at -1, which
quickly becomes zero at line 30, before the first score is printed. Line 20
initialises the ball's horizontal position, and 25 initialises the horizontal
direction of the ball. Minus one sends it left, zero sends it straight up and
plus one sends it right. Line 30 increments, the score count. Line 85 checks to
see if the ball and the bat are fairly close, and if they are, allows the game
to continue.

There are a number of simple ideas presented in this article for you to
implement in your own moving graphics games on the ZX81. We'd be pleased to see
your favourite program which you've developed after reading this article.
