Create Pong
Description: Write your own pong game using this simplified tutorial.
Author: John Bezanis
Added: January 28th 2007
Version: Flash 8
Total Views: 13321
Views in the Past 7 Days: 123

Start out by right-clicking the stage area and selecting document properties. Set the height to 200px, the width to 500px, and the frame rate to 30 frames per second. Select the Rectangle Tool. Set the stroke color to none and the fill color to black (#000000). Hold shift and draw a square on the screen. Select the Selection Tool. Right-click the black box anc click Convert to Symbol. Name it square, set the type to Movie clip, and in the Linkage section, select Export for Actionscript. This is the pong block. Open up the properties tab and set the instance name to pongblock

We are now going to create the paddles. Draw a rectangle on the screen, select the selection tool and right-click. Select convert to symbol. Name it paddle, and click Export for ActionScript. Click ok. In the properties tab, set the Instance Name to leftpaddle. Right-click the paddle and hit copy. Right-click paste a second paddle onto the right side of the stage. Set the instance name to rightpaddle. We now have both paddles and the pong block. We are now going to add in the actionscript to make the pong block. Click the pong block and insert the following code into the actions tab:
- //This section is only processed once.
- onClipEvent(load){
- //Create a variable which determines that the block will move right
- var xspeed=5;
- //Create a variable which determines that the block will move down
- var yspeed=5;
- }
- //This section is processed every frame
- onClipEvent(enterFrame){
- //Check if the pong block has gone below the stage area
- if((this._y+this._height)>Stage.height){
- //set the pong block to the bottom edge of the stage
- this._y=Stage.height-this._height;
- //set the pong block to move up
- yspeed*=(-1);
- }
- //Check if the pong block has gone above the stage area
- if(this._y<0){
- //set the pong block to the top edge of the stage
- this._y=0;
- //set the pong block to move down
- yspeed*=(-1);
- }
- //Check if the pong block has gone to the left or right of the stage area
- if((this._x+this._width<0) || (this._x>Stage.width)){
- //set the pong block to the middle of the stage
- this._x=Stage.width/2;
- //set the pong block to move in the opposite direction horizontally
- xspeed*=(-1);
- }
- //move the x and y positions of the pong block
- _x+=xspeed;
- _y+=yspeed;
- }
- //Process this code on each frame
- onClipEvent(enterFrame){
- //if the UP key is pressed, move the left paddle up
- if(Key.isDown(Key.UP)){
- this._y-=5;
- }
- //if the DOWN key is pressed, move the left paddle down
- if(Key.isDown(Key.DOWN)){
- this._y+=5;
- }
- //if the paddle moves below the stage area, move it back to the bottom of the stage
- if((this._y+this._height)>Stage.height){
- this._y=Stage.height-this._height;
- }
- //if the paddle moves above the stage area, move it back to the top of the stage
- if(this._y<0){
- this._y=0;
- }
- //Check if the paddle hits the pong block
- if(this.hitTest(_root.pongblock)){
- //set the pong block to move in the opposite direction
- _root.pongblock.xspeed*=(-1);
- //move the pong block to the edge of the paddle
- _root.pongblock._x=this._x+this._width;
- }
- }
- //Process this code on each frame
- onClipEvent(enterFrame){
- //some code for the ai to "chase" the block up and down
- if(_root.pongblock._y>(this._y+this._height/2)){
- //move the paddle down
- _y+=2;
- }else{
- //move the paddle up
- _y-=2;
- }
- //if the paddle moves below the stage area, move it back to the bottom of the stage
- if((this._y+this._height)>Stage.height){
- this._y=Stage.height-this._height;
- }
- //if the paddle moves above the stage area, move it back to the top of the stage
- if(this._y<0){
- this._y=0;
- }
- //Check if the paddle hits the pong block
- if(this.hitTest(_root.pongblock)){
- //set the pong block to move in the opposite direction
- _root.pongblock.xspeed*=(-1);
- //move the pong block to the edge of the paddle
- _root.pongblock._x=this._x-_root.pongblock._width;
- }
- }

We now have a game, but to make it a bit more interesting, we are going to keep score. Select the Text Tool and draw a box on the left half of the screen. You can double-click it and increase the width. Change the type to Dynamic Text. Set the var to leftscore. create another text box on the right side. Change the type to Dynamic Text and set the var to rightscore. Click the pong block and replace the code with this new code:
- //This section is only processed once.
- onClipEvent(load){
- //Create a variable which determines that the block will move right
- var xspeed=5;
- //Create a variable which determines that the block will move down
- var yspeed=5;
- }
- //This section is processed every frame
- onClipEvent(enterFrame){
- //Check if the pong block has gone below the stage area
- if((this._y+this._height)>Stage.height){
- //set the pong block to the bottom edge of the stage
- this._y=Stage.height-this._height;
- //set the pong block to move up
- yspeed*=(-1);
- }
- //Check if the pong block has gone above the stage area
- if(this._y<0){
- //set the pong block to the top edge of the stage
- this._y=0;
- //set the pong block to move down
- yspeed*=(-1);
- }
- //Check if the pong block has gone to the left or right of the stage area
- if((this._x+this._width<0) || (this._x>Stage.width)){
- //left side scored
- if(this._x>Stage.width){_root.leftscore++;}
- //right side scored
- if(this._x+this._width<0){_root.rightscore++;}
- //set the pong block to the middle of the stage
- this._x=Stage.width/2;
- //set the pong block to move in the opposite direction horizontally
- xspeed*=(-1);
- }
- //move the x and y positions of the pong block
- _x+=xspeed;
- _y+=yspeed;
- }
Download Source File
Questions and Suggestions
hey...probably a good tutorial...couldn't b assed doin it tho...
but i liked the game itself at the start...
April 7th 2007 06:04AM - sam
that was awsome but the tutorials not clear i had to guess alot on the steps but other than that it was fine
April 11th 2007 13:04PM - master of flash
The opponents movement is a bit ridiculous, it is relatively easy to create an opponent that responds to the position of the ball without too much hassle. Its just a case of using the variable position of the ball and then reducing the gap between the opponent and the ball using the opponents speed variable.
April 13th 2007 09:04AM - Steve
in response to steve, using the ball position and enemy speed variables to create ai is exactly wat this tutorial is teaching you, so your suggestion isnt any less ridiculous than the code there already. if you want something more advance, try making the enemy move in relation to how far away the ball is. to spice it up even more, use a couple random numbers to affect the enemy move rate
April 14th 2007 14:04PM - helper monkey
Get NaN in score boxes cannot fathom this problem. Have checked script etc.
April 22nd 2007 20:04PM - TC
Pretty good tutorial. But, whenever the game plays, the ball flies off into the bottom part of the playing field and to offscreen. (The source file is not working on Flash MX?)
April 26th 2007 16:04PM - RD
Thank you for the tutorial it is very good and it helped me understand actionscipt better
May 3rd 2007 14:05PM - CORY
Got the NaN problem too, but apart
from that this tutorial is great.
Works perfectly in Flash 8 :P
Thanks!
May 24th 2007 08:05AM - FR
once again someone filled a blankspot in my flash repitore
May 28th 2007 12:05PM - gart
This is a pretty good tutorial, but I have a suggestion. I made a Pong clone a while back, and you can play it if you click my name. The suggestion that I have is that you program the ball to bounce off the paddle at different angles according to the location of the paddle that the ball impacts. If you play my game, you'll see what I mean. The heavily commented .fla file is here:
http://www.swp.tifreakware.net/pong.fla
Pretty good tutorial, though!
June 2nd 2007 11:06AM - Egan
I figured out the NaN error. Next to the Var name there is an option default on, of Kerning. Remove the check, all is good again.
It seemed like a tutorial from someone who know what they were doing, as there are some steps not explained thoroughly. I was able to keep up, but tutorial might not be a good word for this.
June 3rd 2007 19:06PM - Sean
Ahh, good call Sean. Must have had that unchecked from a previous project. Thanks.
June 4th 2007 01:06AM - John
ya....its nice...i am a fresher in this field......i try to do this...thanks for the source..... tnkX....
June 29th 2007 07:06AM - Mali Dharam
I'm still getting the NaN error and the Auto Kern isn't selected!
September 27th 2007 17:09PM - Michael
Ok. This is how to fix the NaN problem:
Make sure the dynamic text areas have 0 already entered in them!
September 28th 2007 10:09AM - Michael
nice tutorial.. thanks.
http://www.myfreegamespot.com
October 16th 2007 17:10PM - Venkadesan Tharshan
hi i tried to do the tutorial and the al paddle isn't responding and the pong square goes straight threw the left paddle the score works but thats about it
i hav a shity flash from school and the source file won't load
could u possibly send me ur whole game as a flash file ???
October 17th 2007 00:10AM - michael
Good but the ball keeps falling off my screen then comes back!
And You did'nt ever do the rest of the explaining
November 11th 2007 11:11AM - Jonathan
omg dizzle dis is well bad init i reli liked and i hop tht oda people do but i dint lik dat munch cos i didnt init get it
November 23rd 2007 16:11PM - pizzle
This tutorial is really awesome! I was wondering how you would set a max score, like Firt person to 10 points wins. I've been looking for a good tutorial on a basic pong game.
February 26th 2008 09:02AM - Jonathan
Awsome Tut! I have a way to make a two player game. Just replace the opponent's code to this:
//Process this code on each frame
onClipEvent(enterFrame){
//if the UP key is pressed, move the left paddle up
if(Key.isDown(87)){
this._y-=5;
}
//if the DOWN key is pressed, move the left paddle down
if(Key.isDown(83)){
this._y+=5;
}
//if the paddle moves below the stage area, move it back to the bottom of the stage
if((this._y+this._height)>Stage.height){
this._y=Stage.height-this._height;
}
//if the paddle moves above the stage area, move it back to the top of the stage
if(this._y<0){
this._y=0;
}
//Check if the paddle hits the pong block
if(this.hitTest(_root.pongblock)){
//set the pong block to move in the opposite direction
_root.pongblock.xspeed*=(-1);
//move the pong block to the edge of the paddle
_root.pongblock._x=this._x-_root.pongblock._width;
}
}
March 2nd 2008 14:03PM - Ruiqi Mao
This was a great tutorial!This was my first game and I had no problems. I understand actionscript a little better now thanks to this. I was wondering though if you could set it up to say "game over" with a max score.
March 30th 2008 14:03PM - Cameron
Thanks for this tutorial, but I'm confused (maybe stupid?). Can someone please explain why the initial variables (xspeed and yspeed) were declared using the onClipEvent(load) process? Why not just declare them outright? Can the code even run if the clip isn't loaded? Do they not exist if the movieclip isn't loaded yet? I tried to do it directly and things didn't work, but I don't understand why. My real goal is to move all this code off the individual movie clips and into a single frame on an Actions layer. I'd be very glad to see that. Thanks.
April 14th 2008 11:04AM - Dan
I have the Nan problem and the square goes right through both paddles and the right paddle doesn't move.
June 6th 2008 09:06AM - banana
fortunately I didn't manage to get this working properly
How ever i'm a very noobish person who doesn't understand how a simple script like these works
thanks a lot for the tutorial
July 13th 2008 15:07PM - Menno
Add a Question or Suggestion






