Begin Programming Logo
Home | KPL | Object Pascal | General Programming | Code Database | About Us
   
Using Sprites
   
   

A sprite is the name of an image built into KPL, which you can use as a character in your games.  To view the sprite built into KPL, click ‘Files’ in the bottom right hand corner of the IDE.  Expand the ‘Pictures’ section and you will see the sprites listed.  Double click on them to view them.

To load a sprite you use the following code:

LoadSprite(“SpaceShip”, “UFO.gif”)

You will notice that there are two pieces of information in the brackets, separated by a comma.  We will start by looking at the second bit of information, “Ship.gif”.  This is the filename of the sprite you want to load, so that KPL knows where to load the sprite from on the harddisk.  You get this from looking at the list of sprites in the IDE.

The first part of the brackets, where we have written ‘SpaceShip’ is a lot more confusing, but vital you understand.  You can write whatever you want here, but to be inline with house style, it should be a name which describes you sprite, for example ‘SpaceShip’ clearly indicates that the sprite is a spaceship.  What you write here is essentially the name of that sprite, which you will use in your program, whenever you want to use that sprite.

For example, in the game below, we have two sprite and we will give them easy to understand names
Diagram showing 2 sprites
We do not use the filename here, for a simple reason; we may have more than one space ship and so we do not want to have to create a new version of the graphic for each space ship in our game, so instead we can open “UFO.gif” multiple times, but give the loaded sprite a different name.

After we have loaded the sprite, we do not use the filename anymore, only the name of the loaded sprite.

When we have loaded the sprite we need to display it.  This uses the following code:

ShowSprite(“SpaceShip”)

Run your program and you should see a UFO.

Exercises

  • Show a picture of a train on the screen
  • Show a picture of a car and the Earth on the screen – this uses 2 separate sprites but just do as you would normally, but repeat some code.

We can now start to move sprites around the screen.  Again, this uses the co-ordinate system, like the pen.

KPL has a built in command, as below:

MoveSpriteToPoint(“SpaceShip”, 100, 100)

Notice that in the brackets we have things bit of information, separated by commas (called parameters), firstly we need to tell KPL the name of the sprite (not the file name!), secondly the new x co-ordinate and thirdly the new y co-ordinate.

If you add this line of code to your program and run it, you will find that your sprite loads at a different point.

If you were to add this line of code again, but with different co-ordinate value, like below you would find, perhaps unexpectedly that the sprite still only goes to one position.

Is KPL ignoring our 1st MoveSpriteToPoint?  No, what is happening is that the computer is processing your code so fast that it simply moves to the 2nd position before you have even had chance to see it.  What we can therefore use is the command:

Delay(1000)

Delay will make the program stop running for the number of milliseconds (1ms = 0.001 seconds).  If we put this in between the 2 MoveSpriteToPoints, it will allow us time to see the sprite in its first position before it moves to the next position.

Exercises

  • Load a sprite and move it to 3 different points on the screen, but allow the user to see the sprite in each position before moving it on
  • Draw a nice big square or rectangle on the screen, using the pen. Next load a sprite and get the sprite to move to each corner of the shape you have drawn before coming back to the original corner. Use a delay command to slow the picture down.
   

Valid XHTML 1.0 Transitional Valid CSS!