
上QQ阅读APP看书,第一时间看更新
Time for action — adding some data fields
- One of the elements of Pongo is the paddle, so we need to add fields to store its X and Y position at the beginning of the class definition. To add data fields we need to extend the pongo class.
Class pongo Extends App Field pX:Float = 630.0 'X pos on the right side of the canvas Field pY:Float = 240.0 'Y pos in the middle of the canvas
- Next will be the data fields for the ball. X/Y position and its X/Y speed.
Field pX:Float = 240.0 'Y pos in the middle of the canvas Field bX:Float = 320.0 'X pos of the ball in the middle of canvas Field bY:Float = 240.0 'Y pos in the middle of the canvas Field bdX:Float = 3.5 'X speed of the ball Field bdY:Float = 1.5 'Y speed of the ball
- For both enemy paddles, we need to add their data fields for the X/Y positions and the Y speed. We will use 1-dimension arrays for this.
Field bdY_Float = 1.5 'Y speed of the ball Field eX:Float[] = [5.0, 55.0] 'X pos of both paddles Field eY:Float[] = [240.0, 240.0] 'Y pos of both paddles Field edY:Float[] = [-10.0, 5.0] 'Y speed of both paddles
- The last thing to add are some fields to store—the game score, the mode the game is in, and a helper field for printing some text info.
Field edY:Float[] = [-10.0, 5.0] 'Y speed of both paddles Field pPoints:Int = 0 'Player points Field ePoints:Int = 0 'Enemy points Field gameMode:Int = 0 'Gamemode 0=Start game, 1=Game, 2=GameOver Field modeMessage:Int = 0 '0=Message can be printed Method OnCreate:Int()
- It's time to save your script again and test it, to see if you made any mistakes. For going further with a pre-built script, you can use the
Pongo_02.Monkey
file.
Now that we have all the data fields in place, we can see that we will create the methods of our class, which will render the actual game.