Space Ship 2

Home

The next program introduces two ideas to our space ship program, keyboard control of the ship and sound.  Let's take a look

Program SpaceShip2

 

    Method Main()

 

        Var X As Int

        Var Y As Int

 

        Clear(8)

 

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

        MoveSpriteToPoint("ship" , 50 , 50)

        RotateSprite("ship", 90)

        ShowSprite("ship")

 

        While IsKeyDown( "escape") = False

 

            If IsKeyDown( "Right" ) Then

                X = X + 2

                PlaySound("Missile.wav")

            End If

 

            If IsKeyDown( "Left" ) Then

                X = X - 2

                PlaySound("Missile.wav")

            End If

 

            If IsKeyDown( "Up" ) Then

                Y = Y - 2

                PlaySound("Missile.wav")

            End If

 

            If IsKeyDown( "Down" ) Then

                Y = Y + 2

                PlaySound("Missile.wav")

            End If

 

            MoveSpriteToPoint("ship" , X , Y)

            Delay(10)

        End While

    End Method

End Program

 

 

Download KPL

 

The first two lines of the program introduce two variables.  The variable called X holds the horizontal position of the ship and the one called Y holds the vertical position of the ship.

 

Var X As Int

Var Y As Int

 

 

The next lines of the program make the screen black and set up the sprite the way we did in our first program.

 

Clear(8)

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

MoveSpriteToPoint("ship" , 50 , 50)

RotateSprite("ship", 90)

ShowSprite("ship")

 

The next line begins a new kind of loop.  This one called a while loop will repeat until the user presses the escape key on the keyboard.

 

While IsKeyDown( "escape") = False

 

The next lines look for a keydown event.  This happens when our user (you) presses a key.  If the key is the right arrow, then we make X larger.  If it is the left arrow, we make X smaller.  If the key is the up arrow, we make Y smaller.  If it is the down arrow, we make Y larger.  Here is one example:

 

If IsKeyDown( "Down" ) Then

Y = Y + 2

 

The next lines play a sound.  KPL comes with many sounds you can use in your programs.  You can hear them by clicking the Sounds folder open on the right side of your screen and double clicking each file you find. 

 

PlaySound("Missile.wav")

 

Finally, we move our sprite to the new point X, Y on our screen and we wait a little so the program does not move too fast.  Then we end our loop.

 

 MoveSpriteToPoint("ship" , X , Y)

 Delay(10)

 End While

 

 

Try This:

 

1.  Make your own sprite as we did in our last program.

2.  Change the sound that plays by changing the name of the file in all the lines that read:   PlaySound("Missile.wav")

3.  Change the speed that the ship moves by changing all the lines that read:  Y = Y + 2 and X = X + 2

4.  Change the keys that control the ship

5.  Chang the color of the background.

 

 

Our final program is a full arcade game called Lander written in KPL.  You can change the program and see what happens.