TAYLOR-MADE GRAPHICS
by Richard Taylor
from Big K #4, July 1984, page 46

THE SPECTRUM MANUAL makes no mention of animation or movement, as if
it is an area of computer science so unimportant and trivial that it
is beneath its notice. Anybody who has seen any arcade-type games on
the Spectrum would obviously realise that this is not the case; quite
the reverse. Animation and character movement is probably the most
important function of any microcomputer - let alone our trusty friend
the Spectrum. What would all those Space Invaders, Pac-Men and so on
be like if they were utterly motionless?

The movement that can he procured by the use of Sinclair BASIC can
hardly be any competition to the marauding groups of fierce aliens
elegantly transported around thee screen with the use of
breathtakingly fast Z80 Machine Code. Fear not, because BASIC, equips
you with some quite respectable animation if used to its full
potential.

By utilising every last idiosyncrasy and shortcut of BASIC for the
sake of pure speed it is possible to produce animation programs
written solely in BASIC which do not smack too highly of its slow
speed.

The biggest problem with BASIC is that it does nut possess any
commands specifically designed with movement in mind. To emulate such
commands requires a strange and complicated assortment of PRINT
commands. perhaps with the inclusion of the odd colour statement or
two.

The usual method of creating movement while using BASIC is to print
the object on the screen, wait a while, erase the object by
overprinting it with a space character, update its position in a
certain way and then go back to the first stage - rather cumbersome
but the only real solution. A simple program to perform such a task
is as follows:

110 LET a=0:             REM initial column
120 PRINT AT 10,a;"*":   REM print character
130 PAUSE 10:            REM wait a bit
140 PRINT AT 10,a;" ":   REM erase character
150 LET a=a+1: GO TO 120:REM update position

An asterisk plods along horizontally from the right to left and
eventually disappears off the screen leaving the computer to
impertinently reply with the error message "B Integer out of range".

By changing the value of the PAUSE statement residing at line 30 it
is indeed possible to change the speed at which the object moves.
Removing it altogether gives the asterisk a fair amount of speed, but
remember we are only moving one object and not doing anything else
besides that one menial task. The next short routine bounces a ball
(or rather the letter 'O') around the screen - a more interesting
movement,

210 LET x=16: LET y=11
220 LET a=1: LET b=-1
230 PRINT AT y,x;"O"
240 PAUSE 3
250 PRINT AT y,x;" "
260 LET x=x+a: IF x>31 OR x<0 THEN LET a=-a: GO TO 260
270 LET y=y+b: IF y>21 OR y<0 THEN LET b=-b: GO TO 270
280 GO TO 230

Here the updating section is a lot more complicated and involves
checking if the object has violated the boundaries of the screen and,
if so, changing its direction.

You may notice the flicker that is beginning to creep into the
program. This results from the fact that the object is blanked out
and therefore not present on the screen for a comparatively long time
due to the added complexity of the position updating part of the
program. To overcome this problem it is best to make the program
remember the old position of the object before finding out what the
new one is going to be, then the old position can be blanked out just
before the new one is printed, giving the impression of continuous
motion.

Now we have come to something a little more tricky, moving more than
one object around the screen at a time. Below is a modified version
of the bouncing ball routine which is able to cope with the strains
of such animation:

310 INPUT "Number of Objects? ";num
320 DIM x(num): DIM y(num)
330 DIM a(num): DIM b(num)
340 FOR n=1 TO num
350   LET a(n)=1: LET b(n)=-1
360   LET x(n)=RND*31: LET y(n)=RND*21:
    NEXT n
370 FOR n=1 TO num
380   LET oldx=x(n): LET oldy=y(n)
390   LET x(n)=x(n)+a(n):
      IF  x(n)>31 OR x(n)<0 THEN
        LET a(n)=-a(n): GO TO 390
400   LET y(n)=y(n)+b(n):
      IF  y(n)>21 OR y(n)<0 THEN
        LET b(n)=-b(n): GO TO 400
410   PRINT AT oldy,oldx;" "
420   PRINT AT y(n),x(n);"O"
430 NEXT n
440 GO TO 370

 This program utilises arrays to store the co-ordinates and direction
of each of the objects and uses a FOR/NEXT loop to update the
positions of each of the objects in turn. Notice how the old position
of each object, in turn, is held in oldx,oldy while the new
position is calculated. The old position can then be overprinted
immediately before the object is re-printed, avoiding any flicker.
You can demonstrate the sluggish speed of BASIC if you enter a
biggish number when the computer prompts you for the number of
objects that are to be moved.

0ne of the biggest problems with Sinclair BASIC is that you are
restricted to printing in the character grid of 32 columns and 22
rows. To produce smooth moving graphics you need to be able to make
full use of the Spectrum's high resolution capabilities and be able
to print anywhere on the high resolution grid of 256 x 176 pixels.
Next month I will be presenting a short machine code program to do
just that. I'll also be looking at much more complicated subjects
such as acceleration and complex movement patterns.
