Flash - Pong

How to build a Pong game.

Variables

var horizontalSpeed:Number;
var verticalSpeed:Number;
var paddleSpeed:Number;
var opponentSpeed:Number;
var radius:Number=ball.width/2;
var servePos=player.width+ball.width;
var playerScore:Number;
var opponentScore:Number;
var gameOver:Boolean;

Function Calls

init();
player.addEventListener(Event.ENTER_FRAME,movePlayer);
opponent.addEventListener(Event.ENTER_FRAME,moveOpponent);
ball.addEventListener(Event.ENTER_FRAME,moveBall);

Functions

function init(){
    Mouse.hide();
    horizontalSpeed = 15;
    verticalSpeed = 5;
    paddleSpeed = 10;
    opponentSpeed = 10;
    playerScore = 0;
    opponentScore = 0;
    gameOver = false;
    playerScoreDisplay.text = "0";
    opponentScoreDisplay.text = "0";
}

function moveOpponent(event:Event){
    //move ball with paddle
    if (ball.y < opponent.y) //if ball is over paddle, move paddle up
        opponentSpeed = - paddleSpeed;
    else
    opponentSpeed = paddleSpeed;
    opponent.y += opponentSpeed;
}

function moveBall(event:Event){
    ball.moveIt();
    if(!gameOver)
        ball.checkPaddles();
    ball.checkBoundaries();
}

ball.moveIt = function(){                                   
    ball.x  += horizontalSpeed;                            
    ball.y  += verticalSpeed;
}

ball.checkBoundaries = function(){
    if (ball.y < radius || ball.y  > stage.stageHeight-radius) //top/bottom
        verticalSpeed = - verticalSpeed;
     //left of stage
    if (ball.x < 0){
        opponentScore ++;
        opponentScoreDisplay.text = String(opponentScore);
        if (opponentScore >= 3){ 
            ball.removeEventListener(Event.ENTER_FRAME,moveBall)
            player.removeEventListener(Event.ENTER_FRAME, movePlayer)
            opponent.removeEventListener(Event.ENTER_FRAME, moveOpponent)
            gameOver=true;
            gotoAndStop("lose");
        }
        ball.x = opponent.x-servePos; //opponent serves
        ball.y = opponent.y;
        verticalSpeed=0;
    }
    //right
    else if (ball.x > stage.stageWidth){
        playerScore ++;
        playerScoreDisplay.text = String(playerScore);
        if (playerScore >= 3){ 
            ball.removeEventListener(Event.ENTER_FRAME,moveBall)
            player.removeEventListener(Event.ENTER_FRAME, movePlayer)
            opponent.removeEventListener(Event.ENTER_FRAME, moveOpponent)
            gameOver=true;
            gotoAndStop("win");
        }
        ball.x = player.x+servePos; //player serves
        ball.y = player.y;
        verticalSpeed=0;
    }
}

ball.checkPaddles = function(){ 
    //check if ball touches paddle
    // player’s  paddle
    if (ball.hitTestObject(player)){ 
        horizontalSpeed = - horizontalSpeed;
        verticalSpeed = changeVerticalSpeed(player);
    }
    //opponent's paddle
    if (ball.hitTestObject(opponent)){
        horizontalSpeed = - horizontalSpeed;
        verticalSpeed = changeVerticalSpeed(opponent);
    }
}

function changeVerticalSpeed(paddle){
    var position:Number = ball.y - paddle.y; 
    var angle:Number = position / paddle.height;
    var speed:Number = angle * 30;
    return speed;
}

Game Scene

Pong