Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The project was made to learn true Flash OOP and built using the MXMLC compiler.
- 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.
- An example of this is that the AI code is in the main class because it wouldn't have access
- to the ball's data otherwise.
- AI explanation:
- 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.
- When the bat goes offscreen, it will start moving to the middle of the screen.
- */
- package{
- import flash.display.MovieClip;
- import flash.text.TextField;
- import flash.events.Event;
- public class Pong extends MovieClip{
- var playerBat:PlayerBat = new PlayerBat;
- var enemyBat:EnemyBat = new EnemyBat;
- var ball:Ball = new Ball;
- var aidifficulty = 100;
- public function Pong(){
- addEventListener(Event.ENTER_FRAME, update);
- //Initialize the player's bat
- playerBat.x=10;
- playerBat.y=200-playerBat.height;
- addChild(playerBat);
- playerBat.init();
- //Initialize the enemy bat
- enemyBat.x = 490-enemyBat.width;
- enemyBat.y = 200-enemyBat.height;
- addChild(enemyBat);
- //Initialize the ball
- ball.x = Math.random()*300+150;
- ball.y = Math.random()*400;
- addChild(ball);
- }
- private function update(e:Event){
- //Update the ball and bats
- playerBat.update();
- ball.update();
- enemyBat.update();
- //Check for ball collision
- if(ball.y <0){
- ball.ydir=5;
- }
- if(ball.y > 400-ball.height){
- ball.ydir=-5;
- }
- if(ball.hitTestObject(playerBat)){
- ball.xdir = 5;
- }
- if(ball.hitTestObject(enemyBat)){
- ball.xdir = -5;
- }
- //Someone scored!
- if(ball.x < 0){
- ball.x = 275;
- }
- if(ball.x > 550){
- ball.x = 275;
- }
- //Enemy AI
- if(enemyBat.y>400-enemyBat.height || enemyBat.y<0){
- enemyBat.goal = 200;
- }
- if(ball.x > enemyBat.x-aidifficulty && ball.x < enemyBat.x -(aidifficulty-10) && ball.xdir > 0){
- enemyBat.goal -= enemyBat.height;
- if(ball.ydir>0){
- enemyBat.goal = Math.random()*80 +ball.y-(ball.x-enemyBat.x)-40;
- }
- if(ball.ydir<0){
- enemyBat.goal = Math.random()*80 +ball.y+(ball.x-enemyBat.x)-40;
- }
- trace("lol");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment