Wolfos

Pong.as

Feb 11th, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The project was made to learn true Flash OOP and built using the MXMLC compiler.
  3. It was built in a mix between clean code and quick code, where the easiest solution was chosen for code location even if it wasn't the cleanest.
  4. An example of this is that the AI code is in the main class because it wouldn't have access
  5. to the ball's data otherwise.
  6.  
  7. AI explanation:
  8. Every time the distance between the ball and the enemy is aidifficulty, it estimates where the ball is going to end up. Math.random is used to randomize this estimate.
  9. When the bat goes offscreen, it will start moving to the middle of the screen.
  10. */
  11.  
  12.  
  13. package{
  14.     import flash.display.MovieClip;
  15.     import flash.text.TextField;
  16.     import flash.events.Event;
  17.    
  18.     public class Pong extends MovieClip{
  19.         var playerBat:PlayerBat = new PlayerBat;
  20.         var enemyBat:EnemyBat = new EnemyBat;
  21.         var ball:Ball = new Ball;
  22.         var aidifficulty = 100;
  23.         public function Pong(){
  24.             addEventListener(Event.ENTER_FRAME, update);
  25.            
  26.             //Initialize the player's bat
  27.             playerBat.x=10;
  28.             playerBat.y=200-playerBat.height;
  29.             addChild(playerBat);
  30.             playerBat.init();
  31.            
  32.             //Initialize the enemy bat
  33.             enemyBat.x = 490-enemyBat.width;
  34.             enemyBat.y = 200-enemyBat.height;
  35.             addChild(enemyBat);
  36.            
  37.             //Initialize the ball
  38.             ball.x = Math.random()*300+150;
  39.             ball.y = Math.random()*400;
  40.             addChild(ball);
  41.            
  42.         }
  43.         private function update(e:Event){
  44.             //Update the ball and bats
  45.             playerBat.update();
  46.             ball.update();
  47.             enemyBat.update();
  48.            
  49.             //Check for ball collision
  50.             if(ball.y <0){
  51.                 ball.ydir=5;
  52.             }
  53.             if(ball.y > 400-ball.height){
  54.                 ball.ydir=-5;
  55.             }
  56.             if(ball.hitTestObject(playerBat)){
  57.                 ball.xdir = 5;
  58.             }
  59.             if(ball.hitTestObject(enemyBat)){
  60.                 ball.xdir = -5;
  61.             }
  62.             //Someone scored!
  63.             if(ball.x < 0){
  64.                 ball.x = 275;
  65.             }
  66.             if(ball.x > 550){
  67.                 ball.x = 275;
  68.             }
  69.            
  70.             //Enemy AI
  71.             if(enemyBat.y>400-enemyBat.height || enemyBat.y<0){
  72.                 enemyBat.goal = 200;
  73.             }
  74.            
  75.             if(ball.x > enemyBat.x-aidifficulty && ball.x < enemyBat.x -(aidifficulty-10) && ball.xdir > 0){
  76.                 enemyBat.goal -= enemyBat.height;
  77.                 if(ball.ydir>0){
  78.                     enemyBat.goal = Math.random()*80 +ball.y-(ball.x-enemyBat.x)-40;
  79.                 }
  80.                 if(ball.ydir<0){
  81.                     enemyBat.goal = Math.random()*80 +ball.y+(ball.x-enemyBat.x)-40;
  82.                 }
  83.                 trace("lol");
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment