Monkey Coder – XY Bouncing Ball

*********************
X POSITION MANAGEMENT
*********************

1. Notice:
———-
I have created on Class yourgame Extends App a new variable:

The code:
Field bXSpeed:Float = 3.0 ‘ X Speed (add 3.0 pixel every 1/60 sec)

2. Notice:
———-
I have created the Method BallUpdate:Int() to change the ball position. The ball has to stay inside the canvas. I verify the x position of the ball (bx variable) and I need to invert speed to create the bounce effect :)

The code:

‘ Update Ball START
Method BallUpdate:Int()
bX += bXSpeed ‘Add the X speed of the ball to its X position every 1/60 sec (frame rate: 60 fps)

If bX < 5.0 Then ' To control left bounce bX = 5.0 'Set the X position back to 5.0 bXSpeed *= -1 'Inverse the balls X speed Endif If bX > 635.0 Then ‘ To control right bounce
bX = 635.0 ‘Set the X position back to 635.0
bXSpeed *= -1 ‘Inverse the balls X speed
Endif

Return True
End
‘ Update Ball END

3. Notice:
———-
Very very Important: I put the BallUpdate() inside Method OnUpdate:Int() to execute BallUpdate().
Without this code the ball does not change its position!!!

The code:

‘ OnUpdate START
Method OnUpdate:Int()

BallUpdate()

Return True
End
‘ OnUpdate END

*********************
Y POSITION MANAGEMENT
*********************

I do the same job for variables “bY” ball Y position and “bYSpeed” ball Y position Speed.

' STEP 1 All games open with this first command
Strict

#rem
Script: pong.monkey
Description: it is a remake of an old game!
Author: Andrea Tonin
#End

' STEP 2 Import framework mojo - it is a 2D framamework to support graphics, sound, input, device events
Import mojo

' STEP 3 Creation of Class - yourgame - (we can use the name of the game)

' Class yourgame START *****************************************
Class yourgame Extends App

' Ball
Field bX:Float = 320.0 'X pos of the ball in the middle of canvas (canvas size: 640x480)
Field bY:Float = 240.0 'Y pos in the middle of the canvas (canvas size: 640x480)
Field bXSpeed:Float = 3.0 ' X Speed (add 3.0 every 1/60 sec)
Field bYSpeed:Float = 3.0 ' Y Speed (add 3.0 every 1/60 sec)

' STEP 4 Creation of Method OnCreate - OnUpdate - OnRender

' OnCreate START
Method OnCreate:Int()
SetUpdateRate(60)
Return True
End
' OnCreate END

' OnUpdate START
Method OnUpdate:Int()

BallUpdate()

Return True
End
' OnUpdate END

' Update Ball START
Method BallUpdate:Int()
bX += bXSpeed 'Add the X speed of the ball to its X position every 1/60 sec (frame rate: 60 fps)
bY += bYSpeed 'Add the Y speed of the ball to its Y position every 1/60 sec (frame rate: 60 fps)

If bX < 5.0 Then ' To control left bounce
	bX = 5.0 'Set the X position back to 5.0 
	bXSpeed *= -1 'Inverse the balls X speed
Endif

If bX > 635.0 Then ' To control right bounce
	bX = 635.0 'Set the X position back to 635.0
	bXSpeed *= -1 'Inverse the balls X speed
Endif

If bY > 475.0 Then ' To control bottom bounce
	bY = 475.0 'Set the Y position back to 635.0
	bYSpeed *= -1 'Inverse the balls Y speed
Endif

If bY < 5.0 Then ' To control top bounce
	bY = 5.0 'Set the Y position back to 635.0
	bYSpeed *= -1 'Inverse the balls Y speed
Endif

Return True
End
' Update Ball END

' OnRender START
Method OnRender:Int()
Cls 'Clear the canvas each frame

DrawCircle(bX, bY, 5) 'Draw the ball with a radius of 5

Return True
End
' OnRender END

End
' Class yourgame END ********************************************

' Step 5 Creation of Main function

Function Main:Int()
' Create a running istance from our class - the class - yourgame - has been created at STEP 3 
New yourgame
Return True
End