SuperForth for the Tatung Einstein, showing a SuperForth 1.12 session defining a word
← Back to Courses
module
24

Sprites

Introduction

To move something you have drawn, you have to rub it out and draw it again, and mend whatever was underneath. The Einstein's video chip offers a way round all that. A sprite is a small shape that the chip lays over the picture by itself. You say where; it appears there. Say somewhere else and it moves, and nothing underneath is touched.

Loading, And A Shape

NEWFILE SPRITES
1 LOAD

A sprite needs a shape, and you already know how to make one. A sprite's pattern is simply a character, by its code. Whatever SHAPE has done to character 176 is what a sprite with pattern 176 looks like.

SPRITE

0 100 90 11 176 SPRITE

That is five numbers:

which spritethey are numbered from 0
x0 at the left
y0 at the TOP
colour0 to 15
patterna character code

Look hard at y. For sprites y counts down from the top of the screen. For the drawing words in S23 it counts up from the bottom. They are two different parts of the machine with two different habits, and nothing will warn you when you mix them up. A sprite and a plotted dot with the same numbers are in different places.

To move a sprite, call SPRITE again with the same sprite number and new co-ordinates.

A Sprite With A Name

Typing five numbers every time is tedious, and three of them never change. SPRITEVAR makes a word with those three built in:

0 11 176 SPRITEVAR ALIEN

Sprite 0, light yellow, pattern 176. From now on 100 90 ALIEN puts it at 100,90, and that is all moving it takes.

The Code

NEWFILE SPRITES
1 LOAD
CLS
165 90 36 255 219 126 60 24 176 SHAPE
0 11 176 SPRITEVAR ALIEN
: WAIT 200 0 DO LOOP ;
: FLY 240 0 DO I 90 ALIEN WAIT LOOP ;
FLY

Starting from

A fresh boot, with SuperForth just started.

What you should see

A small yellow invader crossing the screen from left to right, about halfway down, in a few seconds. It passes over the text without disturbing it, and stops at the right-hand side.

The invader partway across the screen, over the text of the listing

WAIT is a loop with nothing in it. It is there to waste a little time, because without it the flight is over before you can see it.

Bigger, And Gone

1 MAG doubles the size of every sprite. 0 MAG puts them back.

0 SPRITE-OFF removes sprite 0, and SPRITES-OFF removes them all. Sprites belong to the video chip, not to the text on the screen, so CLS does not remove them.

Change One Thing

  • Change the 90 in FLY to 20 and then to 170. Which is nearer the top?
  • Change WAIT to loop 1000 times, then 50. FORGET WAIT takes FLY with it, remember.
  • Type 1 MAG and FLY again.
  • Make a second sprite, sprite 1, in a different colour, and put both on the screen at once.

Exercises

24.1 Define DROP-IT to move the alien from the top of the screen to the bottom.

24.2 Define PATROL to fly the alien across and back again. You will want a loop that counts down.

24.3 Define STEER to move the alien left when you press Z and right when you press X, until you press Q. Keep its x in a variable. KEY and CASE will do the rest.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
The sprite is at the wrong heightSprite y counts from the top. Graphics y counts from the bottom.
A sprite shaped like a letterThe pattern number is a character code. 65 is an A.
CLS does not remove itUse SPRITE-OFF or SPRITES-OFF.
Nothing appearsThe colour is 0, which is transparent, or the same as the backdrop.

Summary

A sprite floats over the screen and moves without redrawing. Its pattern is a character, designed with SHAPE. SPRITE takes sprite number, x, y, colour and pattern, with y counted from the top; SPRITEVAR wraps the fixed three into a named word that takes just x and y.

Next

S25, Making A Noise - three voices, and a tune.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.