                     Spectrum Sprites



          Robert Newman controls sprites in Basic



This article describes a machine-code routine which adds a

number of new commands to Spectrum Basic including several

which permit the design and control of sprite graphics.

  A previous article of mine on the subject of sprite

graphics appeared in the January 1984 issue of Your

Computer. This new routine has a number of new improve-

ments over the earlier version. Because the sprites can now

be controlled with Basic commands, instead of using Pokes,

programming is simpler and the program itself is easier to

read.

  Sprites can now be animated. Up to four frames can now be

defined for each sprite and the routine automatically swit-

ches from one frame to the next at a chosen speed. The

method used for printing sprites has also been made faster,

and sprites can move over other subjects on the screen

without erasing them.

  The routine can control up to eight sprites on the screen

at a time. An interrupt routine which is called every 1/50

second while your program is running handles all the work

needed to move the sprites, animate them, and detect

collisions.

  [At this point there was a paragraph explaining how to

enter the machine code routine, but not, amusingly, how

to save it or load it back - although admittedly those are

pretty elementary procedures. Neither was any basic loader

given for the code, unlike for some similar systems by the

same author. I've provided almost the simplest possible

one; it's on the .tzx file which goes with this text under

the name of "Sprites". The machine code itself follows,

called "sprites" with a lower-case 'S'.]

  The extended Basic is switched on with:

        RANDOMIZE USR 60000

This command must be given before you can use any of the

new commands, and it should be the first line of any pro-

gram which uses the extended Basic. The new commands are

all preceded by an exclamation mark and can be typed in

capital or lower case letters.

  Most of the commands must be followed by several numeric

expressions - a number or a variable. The !Doke and !Data

commands will also accept hex numbers as a dollar sign

followed by up to four hex digits - e.g., $FE59.

  If a command cannot be executed for any reason, the pro-

gram stops with an error report as normal. Some of the

commands have new error messages, for example an !Until

which was not precedented by !Repeat gives the error

"Mismatched Until".

  !GRAPHIC character number: Character number must be in

the range 1 to 32. This command tells the routine where to

store the character pattern in the !Data command.

  !DATA line1, line2...: this command can be followed by up

to 16 numeric expressions, one for each line of the charac-

ter being defined. Each numeric expression can be a decimal

number - maximum 65535 - a hex number, or a Bin number.

  !CHAR sprite number, character 1 (, character 2...): This

command shows what graphic character to use when printing

the sprite. If you want the sprite to be animated, you can

give a list of up to four character numbers, and the

routine will automatically switch from one frame to the

next at a speed set by the !Frames command.

  !FRAMES sprite number, delay: Delay can take values from

1 to 255, and controls the speed that the routine switches

frames for an animated sprite.

  !ON EDGE sprite number, edge action: This command con-

trols what happens if a sprite reaches the edge of the

screen. If edge action = 0, the sprite continues moving

with wrap-around. A value of 1 causes the sprite to stop.

A value of 2 makes it bounce.

  !ON HIT sprite number, hit action: If hit action = 0, the

sprite continues moving if it hits another object on the

screen. A value of 1 makes it stop.

  !SPRITE sprite number, x, y: The sprite is printed on the

screen, where x and y are the pixel co-ordinates of the top

left-hand corner of the sprite's position.

  !SPRITE sprite number, x, y, xmove, ymove, steps: The

sprite is printed at position (x,y) and it then moves away

for a specified number of steps - 1 to 254. It moves by

xmove and ymove pixels at each step - possible values are

from -8 to +8. If steps = 255, the sprite moves continu-

ously.

  !ERASE sprite number: The specified sprite is erased.

!ERASE 0 will erase all active sprites.

  !DI and !EI: The interrupt routine which moves the

sprites is switched on automatically by the RANDOMIZE USR

60000 command, and normally continues running while the

Basic program runs. It can be switched off by !DI and re-

enabled by !EI.

  To find out the position of a sprite at any time, or to

examine its collision flag, the following user-defined

functions can be used - where S=sprite number from 1 to 8:

  DEF FN x(s) = PEEK(63727 + 32*s): REM X co-ordinate

  DEF FN y(s) = PEEK(63728 + 32*s): REM Y co-ordinate

  DEF FN c(s) = PEEK(63722 + 32*s): REM collision flag

  The collision value is zero for no collision, 1 at the

edge of the screen, and 128 when colliding with background

or another sprite.

  !PUT character number, x, y: The specified graphics

character is printed on the screen at position (x,y).

  !REPEAT : !UNTIL condition: These two commands are found

in Pascal and structured Basics any commands between the

Repeat and the Until will be repeatedly executed until the

finishing condition becomes true.

  !DOKE address, value: This command does a double POKE of

a 16-bit value to two consecutive addresses. Hex numbers

can be used for the address and for the value to be poked.

  !SET INK old colour, new colour: This command changes one

ink colour on the screen to another colour. This can be

used to make a picture appear instantly on the screen by

first drawing it "invisibly" with its ink colour the same

as the background paper colour and then changing the ink

colour to make the picture appear.

  !SET PAPER old colour, new colour: This command changes

paper colours.

  !SCREEN paper colour, ink colour: This command changes

the colour of the screen without erasing its contents.

  !BREAK OFF/!BREAK ON: These commands disable or enable

the break key. [These seem to have been left off the

internal command table; they are not accepted.]

  !ZAP: This command makes a short zapping sound like a

laser.

  !NOISE length: This makes white noise for a time depen-

ding upon length, which can take values from 1 to 255.

  You can test the routine by typing in the short demo

program in listing 3 [on the .tzx as "Demo"]. This program

shows how to set up sprites and get them moving, either

under computer control, or in response to the keyboard.

  Before you can type in any of the new commands, you must

type RANDOMIZE USR 60000. This command switches on the

extended Basic and lets the new commands be recognised and

interpreted, so it must also appear as the first line of

any program.

  Listing 4 is a graphic character editor program which you

can use to design graphics instead of using the !Graphic

and !Data commands. The 32 graphics characters are stored

in memory between addresses 64280 and 65367. The editor

program lets you design graphics on a large 16 by 16 grid

and store them in the memory. The character set can be

saved and loaded into another program to be used by the

sprites routine. [This program is on the .tzx as "Editor.

Unfortunately it uses IN to read the keyboard, so it will

only work on an Issue 2 Spectrum.]