TUTORIAL EIGHT
WALKING SPRITE
The purpose of this program is to demonstrate how to take a bitmap animation sequence and extract the images to provide a sprite with animation. The sprite will automatically run across the screen, using the animation sequence extracted from the bitmap.
This tutorial program can be loaded directly into the editor by clicking
SAMPLE08.DBA.
rem Load character into hidden bitmap
LOAD BITMAP "runner.bmp",1
rem Grab images for character animation
FOR y=0 to 1
FOR x=0 TO 6
GET IMAGE 1+x+(y*7),(x*89),(y*120),(x*89)+89,(y*120)+120
NEXT x
NEXT y
rem Delete character bitmap
DELETE BITMAP 1
rem Set player variables
xpos=0
ypos=300
image=1
rem Activate manual syncronization
SYNC ON
rem Begin Loop
DO
rem Run right and wrap
xpos=xpos+6 : IF xpos>640 THEN xpos=-64
Rem Animate runner and wrap
image=image+1 : IF image>12 THEN image=2
rem Update sprite
SPRITE 1,xpos,ypos,image
rem Refresh screen now
SYNC : SLEEP 20
rem End Loop
LOOP
The first part of the program loads the bitmap animation sequence into bitmap 1. Bitmap numbers 1 through 32 are offscreen bitmaps and are not visible. A bitmap animation sequence is a normal bitmap file with a grid of single images side by side that provide the effect of animation when displayed in sequence. A FOR NEXT loop is used to extract images from the bitmap. The bitmap is then deleted as the images are now safely stored in memory.
The second part of the program sets values to our variables and prepares to enter the main loop. The SYNC ON command informs the system that the program will take responsibility for refreshing the screen. We do this to ensure the screen refresh rate is smooth and controlled. Leaving this out would cause the program to run so fast you would hardly see the sprite at all!
The main loop begins with the increment of two variables. Both are given maximum values, beyond which the variable will wrap around. The SPRITE command displays sprite 1 at the latest coordinate and image value. The XPOS variable makes sure the sprite is continually moving across the screen. The IMAGE variable keeps changing the image value to make the sprite animate from frame to frame.
The last part of the loop synchronizes the refresh of the screen and forces a small delay to prevent the sprite from moving too fast.
Final Step : Things for you to do
1. Change the program to position the character nearer the top of the screen
2. Change the program to make the sprite move faster
3. Change the program to make the animation slower without slowing down movement
You can skip to the next tutorial by selecting TUTORIAL NINE.