learn


What is a Sprite?
Loading Sprites
Displaying and Moving Sprites
Animating Sprites
Stopping a Sprite
Sprite Masks
Special Effects

What is a Sprite?
Sprites are graphics (pictures) that can be moved around the screen.  They are frequently used in games (spaceships, bullets and characters are all usually sprites) because they can be moved around quickly and easily and can be any shape or size.

TNT Basic has in-built support for sprites and allows you to do special effects with them too.

Loading Sprites
A Sprite can be placed into a TNT basic program by adding a picture resource ('PICT') to the program.  In order to be able to load the sprites though, they need to be added into an Image Bank ('ImBk') resource.  Once this is done, they can be loaded using the command Load Images.

Load Images 128

This loads all the sprites from image bank 128 into memory, ready to be displayed.

Unlike other banks, more than one image bank can be loaded at any time.  Therefore, the following is possible:

Load Images 128
Load Images 129

Both image banks will now be loaded.  Any amount of image banks can be loaded, you are only limited by the amount of memory available.

Displaying and Moving Sprites
A sprite can be displayed very easily using the Sprite command.

Sprite 0,0,0,0

This command prepares a sprite so it is drawn on calls to Draw Frame.

This first number is the index of the sprite.  The has to be unique for that particular sprite, you will see why later.
The second number is the x co-ordinate of where to put the sprite.
The third number is the y co-ordinate of where to put the sprite.
The fourth number is the index of the sprite to get out of the image bank.  The image is taken from the default sprite bank (usually the last one loaded).

When Draw Frame is called all the currently defined sprites are drawn to the screen.

To create the effect of a moving sprite, the position of the sprite simply has to be changed between calls to Draw Frame.  This can be done by redefining sprite 0 to be in a different position.

int x=0
while not Mouse Button

   Sprite
0,x,0,0
   x=x+1
   Draw Frame

wend

This program creates the effect of sprite 0 moving across the screen from left to right.

Now you can see why the sprite's index has to be unique for each item displayed on the screen.  If every sprite used index 0 then only the most recently defined sprite would be drawn.  This is because sprite 0 would keep having it's position and image redefined.  Fortunately, sprite indexes can be whatever number you want but just remember to keep each different one unique.

Animating Sprites
Sprites can also be animated easily.  This can be done simply by changing the image the sprite is currently displaying.

int image=0

while not Mouse Button

   ' Set the position and image of the sprite
   Sprite 0,0,0,image

   ' Increase the frame of animation displayed by the sprite
   image=image+1
   if image>4
      image=0
   
endif

   ' Redraw the contents of the screen
   Draw Frame

wend

This creates a simple looping animation of a sprite.  Each of the the images from 0 to 4 are displayed by the sprite in turn.  This is usually done so quickly by the computer that the user just sees the sprite animate smoothly.

Stopping a Sprite
Once a sprite has been defined it will be drawn in the most recently specified position every single time Draw Frame is called.  If you want to stop this from happening for some reason (e.g. the sprite gets destroyed) then the sprite can be turned off using the command Sprite Off.

Sprite Off 0

This turns off sprite 0 so it will not be drawn any more.  If no number is specified after this command then all the sprites are turned off

Sprite Masks
Sprites can be any shape or size, however, the sprites you have done so far have all been rectangular. This is because TNT Basic is just taking the picture (which is rectangular) and placing it on the screen. In order to produce a sprite with a different shape a 'mask' needs to be specified.

There are two kinds of mask in TNT Basic...

Colour Masks
These masks are quite simple to produce.  A colour can be specified for each image in the image bank, this colour tells TNT Basic which colour in the picture should be used as transparent.  This means that wherever that colour occurs in the picture will be left blank so only all the other colours will be drawn.  This means that if you have a picture of a spacecraft on a black background, by specifying black as the transparent colour, only the spacecraft will be drawn.

Note: This also means that black cannot be used on the spacecraft unless you want that area to be transparent.

Alpha Channel Masks
Alpha channel masks are slightly more complicated than colour masks.  An alpha channel mask can be specified for an image by giving an Image Mask ('IMsk') resource the same id as a picture.  When the picture is loaded into a sprite bank the mask with the same id is also loaded.

The Image Mask is just a grey scale (black, white and all the greys) picture. This tells TNT Basic which areas of the sprite should be drawn. The white areas should be completely visible and the black areas should be invisible.

Special Effects
Sprites do no necessarily have to be drawn directly as they are.  TNT Basic provides some special features which allow you to create additional graphical effects.

Transparencies
Sprites can be drawn to the screen as if they were slightly transparent using the command Set Sprite Transparency.

Set Sprite Transparency 0,50

This command sets sprite 0's transparency level to 50 (50% opaque). Any value between 0 (completely invisible) to 100 (completely opaque). When a sprite 0 is drawn it will appear to be see through.

This effect is useful for creating lasers, hidden items and ghost effects.

Colours
Instead of drawing the sprite, a block of colour can be drawn in the shape of the sprite instead using the command Set Sprite Colour.

Set Sprite Colour 0,Make Colour (0,0,0)

This changes sprite 0 so that next time it is drawn it is just a block of solid black in the shape of sprite 0.

This can combine with transparency to produce shadow effects.

Set Sprite Transparency 0,50
Set Sprite Colour 0,Make Colour (0,0,0)

This will produce a semi-transparent black sprite in the shape of sprite 0..... just like a shadow of sprite 0 would look.

If you have finished using the colour effect it can be turned off.

Set Sprite Colour Off


Table of contents