|Liberty BASIC Help Online
Drawing and Collision Detection
UPDATING THE DISPLAY
The DRAWSPRITES command updates the display, causing all VISIBLE sprites to be drawn at their current LOCATIONS, moving them if the SPRITEMOVEXY command has been issued. They will display in their current SCALE and ORIENTATION. This command must be given each time it is necessary to draw another frame of animation. Sprite attributes may be changed in between the DRAWSPRITES commands, including their location, orientation, and scale. If the background image is to be moved, the BACKGROUNDXY command must be issued before the DRAWSPRITES command.
print #w.g, "drawsprites";
DRAWING WITH GRAPHICS COMMANDS
After the display has been updated with the DRAWSPRITES command, graphics may be drawn with regular Liberty BASIC graphics commands, like LINE, CIRCLE, BOXFILLED, etc. These commands must be reissued after each DRAWSPRITES command. Liberty BASIC graphics commands should be used sparingly, because this may result in flickering images.
To cause Liberty BASIC graphic entities to become a permanent part of the background the program must use the GETBMP command, and then the BACKGROUND command to reset the background. See the section on backgrounds for more information.
Most games require some form of collision detection, to ascertain when two sprites have touched. Liberty BASIC does this automatically! The SPRITECOLLIDES command is used with INPUT to get a string with names of sprites that overlap the current frame of the sprite named. The INPUT statement can be avoided if the list receiver variable is placed inside the quotation marks for the SPRITECOLLIDES command. The sprite names are returned in a single string with spaces between them.
print #w.g, "spritecollides smiley";
input #w.g, list$
print #w.g, "spritecollides smiley list$";
In the following example, the first line reports that "smiley" collided with "smiler", "smiled", and "smiles" during that frame of animation. The second line reports that "smiley" collided with "smiler" and "smiles" during that frame of animation. The third line reports that "smiley" collided only with the sprite named "smiler" during that frame of animation. The fourth line reports that "smiley" did not collide with any other sprites during that frame of animation.
list$ = "smiler smiled smiles"
list$ = "smiler smiles"
list$ = "smiler"
list$ = ""
Here is a picture of a sprite collision. The frog sprite has touched one of the bug sprites. Knowing this, the program would probably change the bug sprite's visibility property to OFF and move it away from the active playing field, or simply reset the location of the bug so that it appeared to be a new bug. If this were a game, a point would probably be added to the score here also.
Collision detection provided by Liberty BASIC uses the entire sprite image, all the way to the corners, to determine collisions,unless a SPRITEROUND command is issued.
ROUNDED SPRITES AND COLLISIONS
Sprite collisions are triggered when two sprites touch one another. If a sprite image fills the rectangle that contains it, the collision is triggered properly. Many sprite images do not fill the corners of the rectangles containing them, but are actually more rounded shapes. If a SPRITEROUND command is issued to a sprite and it collides with another sprite that has received the SPRITEROUND command, collisions are detected in an area that rounds off the corners, assuming that the actual sprite images are generally elliptical or round in shape. A SPRITEROUND command is issued like this:
print #w.g, "spriteround smiley"
INVISIBLE SPRITES AND COLLISIONS
Invisible sprites still trigger collisions. If a sprite is to be out of action for a time, in addition to being made invisible, it should also be located to a spot outside the active viewing area. Invisible sprites may be used to set up a screen area for collision detection. For instance, if it is necessary to know when a sprite is touching a doorway, an invisible sprite can be placed at that spot on the screen so that it can be checked for collisions.
It is sometimes necessary to remove sprites from the collection of sprites so that they no longer appear and so that they no longer trigger collisions. This might be done after a sprite collides with another sprite, so that it is no longer in play in the game. Remove sprites with the REMOVESPRITE command.
print #w.g, "removeSprite smiley"
DETECT SPRITE LOCATION
The SPRITEXY? command retrieves the current location of a sprite. If a CENTERSPRITE command has been issued, the x, y location returned indicate the center of the sprite, otherwise they indicate the upper left corner. Just like the SPRITECOLLIDES command, it can have two forms. It can be followed by an input statement, with two receiver variables for the x and y coordinates of the sprite, or the receiver variables can be included inside the quotation marks of the SPRITEXY? command:
print #w.g, "spritexy? smiley"
input #w.g, x, y
print #w.g, "spritexy? smiley x y"
To avoid flickering, sprite animation is done invisibly, in memory. A frame of animation is built entirely off-screen. A frame of animation is displayed on the screen only when the command DRAWSPRITES is called. For each frame of animation, perform all functions to set the background image, and to set or change a sprite's properties, then call the DRAWSPRITES command.
Sprite graphics are temporary. To learn about making sprite graphics remain in a graphicbox or graphics window, even if the window is covered or minized, read Flushing Sprite Graphics.
Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/