Example: Pong Style Game Engine
Click on the example above,
Controls
Click on the left controller and drag to hit the ball.
Add the following code to Scene_1.
|
onLoad () {
p2_speed = 0.9; // how fast should the ball move? ballYspeed = 10; ballXspeed = 10; } onEnterFrame() { if (p1 == '10') { user = true; nextSceneAndPlay(); } if (p2 == '10') { user = false; nextSceneAndPlay(); } //set the speed and position of the ball ball._y += ballYspeed; ball._x += ballXspeed; //if the ball hits the top or bottom //reverse its Y speed and position if (ball.hittest(bottom) || ball.hittest(top)) { ballYspeed *= -1; } //if the ball hits the left or right //reverse its X speed and position if (ball.hittest(left) || ball.hittest(right)) { ballXspeed *= -1; } } onFrame (2) { stop(); } |
Now create four rectangles name them top, right, bottom and left.
And place them in this order, you should have something looking like fig1.
Add the following code to the rectangle called "right".
|
onEnterFrame() {
//if the ball hits player1 use the //same script from when it hits the left or right wall. if (_root.ball.hittest(this)) { _root.p1++; } } |
Add the following code to the rectangle called "left".
|
onEnterFrame() {
//if the ball hits player2 use the //same script from when it hits the left or right wall. if (_root.ball.hittest(this)) { _root.p2++; } } |
Now you need to add two dynamic text boxes, these will hold the scores in them.
Call one p1score mark it as a target and give it the variable name of p1, place the text box in the center of the left hand side.
Now do the same again this time call the the text box p2score
Mark it as a target and add the variable name of p2 and place the text box in the top center of the right hand side like in fig2.
Ok lets add the ball, select the ellipse tool now press and hold down the shift button whilst dragging out the elliptical shape, this will keep it nice and uniform.
Keep dragging until you have the size of the ball you are happy with. Name the shape ball and mark it as a target. if you press Ctrl+T you should now have some movement.
Like in Fig3. below.
All you need to add now is the paddles, lets start with the computers paddle.
Using the rectangle tool create a rectangle big enough to hit the ball,
But not to big that it will make the game too easy. Name it player2
Add the following code to player2 paddle.
|
onEnterFrame() {
this.ty = (_root.ball._y-(_root.ball._height/2)); this._y += (this.ty-this._y)/_root.p2_speed; this._y += _root.ballYspeed; //if the ball hits player2 ...... //same script from when it hits the left or right wall. if (_root.ball.hittest(this)) { _root.ballXspeed *= -1; } } |
If you press Ctrl+T you should now have something looking like Fig4.
Ok nearly finished and so far its looking pretty good, just a couple of more things to do.
Like a player ones paddle, and also add a game over screen so when the score reaches 10 the games is over.
And a winner is announced. so lets add the player1 paddle, do the same as you did for the player2
But this time call it player1 and add the following code to it.
|
onEnterFrame() {
//if the ball hits player1 use the //same script from when it hits the left or right wall. if (_root.ball.hittest(this)) { _root.ballXspeed *= -1; } } on (press) { player1.startDragLocked(player1._X, player1._X,top._Y+(player1._height/2), bottom._Y-(player1._height/2) ); } on (release) { stopDrag(); } |
Now you should have a fully working game apart from their is no end to it at this time.
So lets add another scene to it by clicking the add scene icon.
Now we have Scene_2 on Scene_2 add the following code
|
onFrame (2) {
stop();{ }{ onLoad () {{ if (user) {{ over = "GAME OVERr YOU WON!";{ } else {{ over = "GAME OVERr CPU WON! ";{ }{ }{ |
Now create a dynamic text box make the font around 28 pixels center it on the screen name it over
Now create a static text box type "Play Again" and add the foolowing code to it.
|
on (release,releaseOutside) {
p1 = 0; p2 = 0; gotoSceneAndStop("Scene_1",1); } |
Thats it you're done you should have something like Fig5






