
上QQ阅读APP看书,第一时间看更新
Time for action — player paddle movement
First, we will create a new method called ControlPlayer
.
- This method will check for keyboard input and move the player paddle according to it. Add this method to the
pongo
class.Method ControlPlayer:Int()
- When the player presses the up key, we are moving the player paddle by 5 pixels, upwards.
If KeyDown(KEY_UP) Then 'check if UP key is pressed pY -= 5.0 'subtract 5 pixel from Y position
As the paddle should stop at the top, we check if its Y position is less than 25 pixels away (paddle height is equal to 50 pixel) and set its Y position back to 25 pixels.
If pY < 25.0 Then pY = 25.0 'Check against top wall Endif:Now we check if the DOWN key is pressed and move the paddle accordingly. Again, we check if it reaches the bottom wall. If KeyDown(KEY_DOWN) Then 'Check if DOWN key is pressed pY += 5.0 'Add 5 pixels to Y position If pY > 455.0 Then pY = 455.0 'Check against bottom wall Endif
- Now, close the method.
Return True End
- To actually be able to control the paddle, we need to call up the
ControlPlayer
method. You need to do this during theOnUpdate
event. We could call it from there, but we need to implement the game mode logic, soon. So, we will create anUpdateGame
method that will be called by itself from theOnUpdate
method. - Create the
UpdateGame
method that calls theControlPlayer
method:Method UpdateGame:Int() ControllPlayer() 'Control the player up an down Return True End
- Next, call
UpdateGame
from within theOnUpdate
event:Method OnUpdate:Int() UpdateGame() Return True
- This is a good time to save again. For further progress, you can load up the pre-made script called
Pongo_05.Monkey
.
Slowly, we are getting some animation into the game. Next will be the enemy paddles.