130 %s=%1-s
140 SPRITE 0,152, %255-a,%s,1
141 PAUSE 3
145 NEXT %a
150 GO TO 106: REM you'll need to
    stop this with BREAK

Execute with GO TO 100 and you'll see our spaceship fire up its engines and cross the screen from top to bottom. Now for something much fancier as promised, move line 106 to 120 and add these lines:

106 PROC initSXSineMov()
560 STOP
570 DEFPROC initXSineMov()
580 FOR f=0 TO 319:%a[ INT {f}]=% INT
    {159* SIN (f/159* PI )}: NEXT f
590 ENDPROC

Finally modify lines 140 and 150 as follows:

140 SPRITE 0,%159+a[a], %255-a, %s,1
150 GO TO 120

before executing again with GO TO 100. The spaceship now will move in a sinusoidal pattern from the bottom to the top of the screen before wrapping around and coming from the bottom. The way we did this, was by precalculating an integer array (See Chapter 12) to hold all possible x values within our visible Sprite System coordinates. To avoid B Integer out of range errors, we made sure the possible values of both the SIN function results and line 140 that positions the spaceship in the x,y axis stay within acceptable range. To switch the initial direction of movement, instead of a + you can start with a - in line 140 as follows:

140 SPRITE 0,%159-a[a],%255-a,%s,1

Note that our integer array %a is using the brackets [ ] variant instead of the parentheses ( ) variant and that's because we have more than a potential 64 values. That means also that integer arrays %a (), %b (), %c (), %d () and %e () have been used up by %a[ ].

It's obvious by this example that very complex animation patterns can be created with relative ease using the Sprite System. Before we move on to scrolling, it's useful to also cover a few more subjects we did not address in the course of our example.

The first thing is the ability to use palettes with the Sprite System. These are indistinguishable from other palettes in the ZX Spectrum Next palette control system2 and they too are also governed by the PALETTE DIM keyword to set them up as 8 or 9 bit. Like the LAYER PALETTE equivalent, the Sprite System has its own keyword combinations: SPRITE PALETTE and SPRITE PALETTE BANK. Their syntax is as follows:

SPRITE PALETTE n[,i,v]

where n is the palette number (0 for first and 1 for second) while the optional i, v are the colour index (0 to 255) and colour value (expressed in 9-bit RRRGGGBBB format regardless of the PALETTE DIM setting).

SPRITE PALETTE n BANK b, o

will operate like it's LAYER counterpart, assigning palette n from offset o in bank b. As with the LAYER version, palettes are 512 bytes long if 9-bit and 256 bytes long if 8-bit (as set with PALETTE DIM).

2 See Chapter 15 for the Layer 2 notable palette exception


ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.