SPRITE EDITOR (aka. Spectrum Sprites)
-------------
by Robert Newman
from Your Computer, June 1985

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
improvements 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 he defined for each sprite and the routine
automatically switches 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.

A hex dump of the routine is given in Listing 2. Th loader program  Listing
1  allows you to enter the machine code one line at a time. After each line
you must input a checksum. If the checksum is incorrect the line must be
re-typed. When you have finished typing in the machine code it should be
saved to tape or microdrive before you run it.

The extended BASIC is switched on with:
	RAND 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 program 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; eg. $FE59. If a command cannot be executed
for any reason, the program stops with an error report as normal. Some of the
commands have new error messages; for example an !UNTIL which was not
preceded by !REPEAT gives the error "Mismatched Until".

!GRAPHIC character number: Character number must be in the range 1 to32. The
command tells the routine where to store the character pattern in the !Data
command.

!DATA line1,line 2 ... This command can be followed by up to 16 numeric
expressions, one for each line of the character 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 controls 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 coordinates 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 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 continuously.

!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 RAND 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 graphic 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 "invisible" 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 colour.

!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.

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

!NOISE length: This makes white noise for a time depending 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.
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 RAND USR 60000. This command switches on
the extended BASIC and lets the new commands be recognised and interpreted,
and 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 spites routine.

If you would like a copy of the programs on cassette, together with
information on using the routine, please send 3 to R.Newman [Address
Deleted].
