Space Ship 1

Home

The next program introduces a type of animation (moving drawings around the screen) called sprite animation.  KPL comes with a set of drawings that you can use in your programs. You can look at all the drawings by clicking open the Pictures folder in the list on the right side of the screen.   In this case, the program takes a drawing called enemyship1.gif and moves it around a black screen.

Program SpaceShip

    Method Main()

        Var i As Int

        Clear(8)

        LoadSprite("ship", "enemyship1.gif" )

        MoveSpriteToPoint("ship" , 50 , 50)

        RotateSprite("ship", 90)

        ShowSprite("ship")

        For i = 1 To 400

            MoveSpriteToPoint("ship" , i , 50)

            Delay(10)

        Next

    End Method

End Program

 

 

Download KPL

Lets take a look at the entire program line by line:

The first line creates a variable called i.  A variable is a name for a number that will change as the program runs.  No matter what the value of the number is, we can always call the number i and the computer will know which number we are referring to.

 Var i As Int

The next line makes the screen black

Clear(8)

The next line tells the program to open the drawing called enemyship.gif.  We will call this drawing, or sprite, by the name ship.

LoadSprite("ship", "enemyship1.gif" )

The next two lines moves ship to a spot on the screen near the top, rotate it, and show the sprite to you.

MoveSpriteToPoint("ship" , 50 , 50)

RotateSprite("ship", 90)

ShowSprite("ship")

 

The next lines create a loop called a for loop.  The loop will go around 400 times.  Each time, the counter variable i will increase in value from 1 to 400.  First it will be one, then two, then three, etc.  Each time the loop goes around, we move our ship to position i.  Since i will be different each time the loop goes around, our ship will change position every time the loop moves.  Right now, the program is written to that the ship moves across the screen (the vertical position stays the same -- 50 -- each time).

For i = 1 To 400

    MoveSpriteToPoint("ship" , i , 50)

    Delay(10)

Next

Can you change the program so that the ship moves down rather than to the side? 

Can you change the program so tha the ship moves diagonally?

Try this:

Save your program to your user folder.  Now open the PAINT program (start, programs, accessories, paint) and create a new space ship.  Make sure your background is black.  Make your drawing small.  Save your document with the name ship.bmp.   Now change the line in your program to read

LoadSprite("ship", "ship.bmp" )

Your drawing will be the space ship.

Our next program will allow us to use the arrow keys to move the ship around.