Time for action — moving the enemy paddles

Computer-controlled movements, or the so-called Artificial Intelligence (AI) , are sometimes very hard to create. But for a start, we will keep it very simple. And simple will be the key here. Our computer-controlled movement will look like this. One paddle will move with a speed of 10 pixels up and down, the other with a speed of 5 pixels in the opposite direction.

  1. For this, you need to create a new method, called ControlEnemies.
    Method ControlEnemies:Int()
    
  2. Next, we update the paddles' Y positions. As we have two paddles to control, we will use a FOR loop for this, so we don't have to repeat the code. Remember that arrays in Monkey are zero-based.
    For Local ep:Int = 0 to 1
    eY[ep] += edY[ep] 'Update the paddles Y position
    
  3. Next, we will check if a paddle reaches the top wall.
    If eY[ep] < 25.0 Then 'Check if paddles reaches top wall
    eY[ep] = 25.0
    edY[ep] *= -1 'Revers its Y speed
    Endif
    
  4. Now, we will check if a paddle reaches the bottom wall.
    If eY[ep] > 455.0 Then 'Check if paddles reaches bottom wall
    eY[ep] = 455.0
    edY[ep] *= -1 'Revers its Y speed
    Endif
    
  5. Close the FOR loop and the method.
    Next
    Return True
    End
    
  6. To actually get the enemy paddles moving, we need to call our new method from within the UpdateGame method.
    ControlPlayer()
    ControlEnemies() 'Control the enemy
    Return True
    
  7. Again, and like always, it is a good time to save your changes. For further progress, you can load up the file Pongo_06.Monkey.

Cool! All paddles are moving. Now only the ball is missing. Let's get rollin'!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset