
上QQ阅读APP看书,第一时间看更新
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.
- For this, you need to create a new method, called
ControlEnemies
.Method ControlEnemies:Int()
- 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
- 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
- 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
- Close the
FOR
loop and the method.Next Return True End
- 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
- 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'!