code in begin frame


//Pong

//By Andy Harris

//For Flash Game Programming for Dummies

//March '05

//Most code in "play" frame



stop();



btnPlay.onRelease = function(){

  _root.gotoAndStop("play");

} // end button

code in play frame


//pong

//working game with win and lose statess



init();



function init(){

  //turn off the mouse pointer

  Mouse.hide();



  //initialize ball speed

  ball.dx = 15;

  ball.dy = 5;



  //opp speed is opponent speed

  //determines max dy for opponent.

  //15 is nearly flawless

  //0 is dead

  opp.speed = 14;



  //reset scores

  playerScore = 0;

  oppScore = 0;

  

} // end init



player.onEnterFrame = function(){

  player._y = _root._ymouse;

} // end enterFrame



opp.onEnterFrame = function(){

  //opp._y = ball._y;

  //check to see if ball is above or below opp

  if (ball._y < opp._y){

    //move opp up

    opp.dy = - opp.speed;

  } else {

    //move opp down

    opp.dy = opp.speed;

  } // end if



  opp._y += opp.dy;

  

} // end function



ball.onEnterFrame = function(){

  ball.move();

  ball.checkBoundaries();

  ball.checkPaddles();

} // end enterFrame



ball.move = function(){

  ball._x += ball.dx;

  ball._y += ball.dy;

} // end move



ball.checkBoundaries = function(){

  //bounce off top and bottom walls

  if (ball._y < 0){

    ball.dy = -ball.dy;

  } // end if



  if (ball._y > Stage.height){

    ball.dy = -ball.dy;

  } // end if



  //if past left of screen, opponent scores 

  if (ball._x < 0){

    trace("Opponent Scores");

    oppScore++;

    //check for opponent win

    if (oppScore >= 3){

      gotoAndStop("lose");

    } // endif

    //opponent serves

    ball._x = opp._x - 50;

    ball._y = opp_y;

    ball.dy = 0;



  } // end if



  //if past right of stage, player scores, 

  if (ball._x > Stage.width){

    trace ("Player scores");

    playerScore++;

    if (playerScore >= 3){

      gotoAndStop("win");

    } // end if

    //player serves

    ball._x = player._x + 50;

    ball._y = player._y;

    ball.dy =0;    

  } // end if

} // end checkboundaries



ball.checkPaddles = function(){

  //check to see if ball touches paddle

  if (ball.hitTest(player)){

    //simply bounce off for now

    ball.dx = -ball.dx;

    ball.dy = getDy(player);

  } // end if



  if (ball.hitTest(opp)){

    ball.dx = -ball.dx;

    ball.dy = getDy(opp);

  } // end if

} // end checkPaddles

  

function getDy(paddle){

  //determines dy based on where ball hits paddle.

  

  //relY is relative Y of ball to paddle

  relY = ball._y - paddle._y;

  //trace relY



  //relPerc is relY / height of paddle

  //will range from -.5 to +.5

  relPerc = relY / paddle._height;

  //trace (relPerc);



  //new DY ranges from -15 to +15

  newDy = relPerc * 30;

  //trace (newDy);

  return newDy;



} // end getDy

code in win frame


Mouse.show();



btnPlay.onRelease = function(){

  _root.gotoAndStop("play");

} // end button

code in lose frame


Mouse.show();



btnPlay.onRelease = function(){

  _root.gotoAndStop("play");

} // end button