program HowToMoveASprite; uses SwinGame,sgTypes; procedure Main(); var ball : Sprite; begin OpenGraphicsWindow('Move a Sprite' ,150 ,150 ); LoadBitmapNamed('ball' ,'ball_small.png' ); ball := CreateSprite(BitmapNamed('ball' ) ); SpriteSetX(ball ,60 ); SpriteSetY(ball ,0 ); SpriteSetDx(ball ,0 ); SpriteSetDy(ball ,0.05 ); repeat ClearScreen(ColorWhite ); if SpriteOffscreen(ball ) then begin SpriteSetX(ball ,60 ); SpriteSetY(ball ,-30 ); end; DrawSprite(ball ); UpdateSprite(ball ); RefreshScreen(); ProcessEvents(); until WindowCloseRequested(); FreeSprite(ball ); ReleaseAllResources(); end; begin Main(); end.
How To Move A Sprite
This article will show you how to move a sprite in SwinGame.
In a game it is important to move almost all the characters and objects. Once a sprite is created SwinGame lets you move that sprite anyway you want. The code below shows how you can move a sprite and to do that SwinGame performs a few steps.
- Get the velocity of the sprite
- Introduce a game loop where it clears the screen with the color you want
- Place the sprite back to the screen if it goes off the screen
- Draw and update the sprite
- Have an end condition of the game loop
If the object is moving, there is a possibility of it being disappear from the screen. SwinGame has a function to detect if the sprite is off the screen. Use it wisely!
You can make lots of games using this principal. You can have all the people running around the screen and when you click on them they stop.