Sprite Basic
by Malachy Devlin
from Your Computer, Sept.  1984, pages 116-118
================================================================================
Equipping the Sinclair ZX-81 with Sprite Basic means you can have fast moving
graphics without soiling your hands with machine code. Malachy Devlin
explains further.

ALL BEGINNERS at computers learn to program using Basic; this is fine since it
is easy to learn and easy to use. However, its real disadvantage is that it is
usually slow in comparison to machine code. This means that if any fast moving
graphics are required in the program then they will have to scrap the idea, or
use my program which equips the ZX-81 16K with sprite Basic.

The sprite Basic can handle up to 256 sprites which can be at three levels:

*Underneath the characters
*Same level as the characters
*Over the characters.

Also when the sprite hits the edge of the screen it can either stop, bounce or
wrap-round. If this sounds like what you want then roll up your sleeves and
begin the hard work.

In order to test the program I must explain what the sprite commands are, how
to use them: and their syntax.
================================================================================
The first and longest is Sprite: it take: the form:
		LET Z = USR SPRITE
		PRINT number; on or off; velocity, flag$; xcoord; ycoord; xmove; char$

Number: this is the sprite number. Range: 0 to 255.
On or Off: this is either l for on or 0 For off.
Velocity: this is not really the true velocity but it is the number of times the
	  Move command is called before the sprite moves. N.B. if velocity is zero
	  the sprite will move with a velocity of 256. Range of velocity is 0 to 255.
Flag$: 	this is a string which is seven characters long and consists of 0s or 1s
	eg., '0010110' '0010110'
This tells the computer what level the sprite is at and what it does at the edge
of the screen.
If the seventh character is 1 then the sprite wraps around.
If the sixth character is 1 then the sprite bounces.
If the sixth and seventh character is a 0 then it stops at the edge of the screen.
If they are both 1s then it will wrap-round.
Now let us consider the fourth and fifth characters.
If the fifth is 1 it goes under the characters.
If the fourth is 1 it goes over the characters,  but if they are both 0s then it
travels at the same level as the characters on screen.
The third character is not used by the sprite Basic.
The second character is only used if the sprite is at the same level as the
characters on the screen. If this is 1 when a sprite collides with some
character on screen then it continues but if it is 0 the sprite will stop.
Finally if the first character is 1 it will remain stationary or if 0 it allows the
sprite to move normally.
Xcoord: this is the initial x co-ordinate.
Ycoord: this is the initial y co-ordinate also 24 lines of screen are used.
Xmove:  this is the distance the sprite will move horizontally.
Ymove:  same as xmove except vertically.
Char$:  this is a string four characters long which contains the characters
	that go to make up the sprite as follows:
		1 2
		3 4
	The number represents where that character is in the string, this also
	means that all sprites are 2x2.

Note this command can be abbreviated to
	LET Z=USR SPRITE
	PRINT number,on or off
in order to switch sprites on or off only This is the only possible
abbreviation.
================================================================================
The second command is Move and takes the form:
	LET Z = USR MOVE
This calls the sprite moving routine which moves sprite 0 to whatever
number specified by NUM command (see next command)
Num takes the form:
	POKE NUM, number of sprites + 1
Therefore to have no sprite's POKE NUM,1
================================================================================
Flag takes the form:
	LET Z = USR FLAG
	PRINT number of sprite
This puts either 0,1,2 or 3 into Z, depending on the condition of the
sprite.
If Z= 0 then the sprite Has hit nothing and is allowed to move
Z=1 sprite has stopped ie. if it is at the same level as characters then
it has hit a character over and under sprites won't affect this flag
simply because they will either go over or, under the characters and can't
hit them.
Z=2 sprite has collided; if it is a same level sprite and it collides with a
character on the screen then Z=2, or if the sprite has stopped at the edge
then Z=2 but the 'stopped' flag is not set.
Z=3 sprite has stopped and collided; see Z= 1 and Z =2
================================================================================
Coord takes the form:
	LET Z= USR COORD
	PRINT number of sprite
This returns with Z equal to ycoordinate + 256 * xcoordinate and
	PEEK 16507 = ycoordlnate
	PEEK 16508 = xccordinate
of the sprite defined in the Print statement.
================================================================================
Motion takes the form:
	LET Z= USR MOTION
	PRINT number of sprite
This is the same as Coord except that the x and y movements are found.
================================================================================
Init takes the form:
	LET Z=USR INIT
This allows the Move routine to be interrupt driven. So you could run a
program and when it has finished sprites can still be moving around.
However the drawback is that only two sprites can he used, any more
causes the screen to jump.

This command is turned off by the following commands: FAST,SLOW,LPRINT,LLIST,COPY,
LOAD, SAVE, DATA (new command) or when entering a program line.
================================================================================
Data takes the form:
	RAND USR DATA
	PRINT number of sprites
The data for the sprites is stored in a Rem statement after the machine code
and 16 bytes is required for each sprite and two 'Newline' characters at the
start to blank out the data. So to do this you use this command which
produces enough bytes for the number of sprites in the Print statement.
================================================================================
REPORT CODES
T: no line after command which requires PRINT statement
W: flag$ not correct length
U: char$ not correct length
C: data missing in PRINT statement
================================================================================
One other thing is that if an 'over' sprite passes over another sprite it may
leave part or all of that sprite behind but usually when it runs over anything
it will not do this, but some very good 3-D effects can now be easily created.

Any commands which require a Print statement after them will change this
Print to a Rem so if you edit the line with a Rem then this Rem must be
converted back to a Print.

You can now test the program by entering some commands for yourself or
type in the demonstration program. This program begins with four sprites
bouncing around, now hit a key and they will wrap-round. Hit a key again
and a sprite begins moving randomly, now every time a key is hit: another
sprite is added, the number of sprites on the screen is shown at the top
left-hand corner of the screen. If the 0 key is pressed then the program
ends but leaves two sprites moving around.