Monkey Game Development:Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action — player paddle movement

First, we will create a new method called ControlPlayer.

  1. 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()
    
  2. 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
    
  3. Now, close the method.
    Return True
    End
    
  4. To actually be able to control the paddle, we need to call up the ControlPlayer method. You need to do this during the OnUpdate event. We could call it from there, but we need to implement the game mode logic, soon. So, we will create an UpdateGame method that will be called by itself from the OnUpdate method.
  5. Create the UpdateGame method that calls the ControlPlayer method:
    Method UpdateGame:Int()
    ControllPlayer() 'Control the player up an down
    Return True
    End
    
  6. Next, call UpdateGame from within the OnUpdate event:
    Method OnUpdate:Int()
    UpdateGame()
    Return True
    
  7. 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.