package {
        import flash.display.MovieClip;
        import flash.events.*;
        import flash.ui.Keyboard;
        import flash.text.TextField;
        public class Game extends MovieClip {
                public var playerone:MovieClip = new MovieClip;
                public var ball:MovieClip = new MovieClip;
                public var ai:MovieClip = new MovieClip;
                public var xspeed:Number = -0.5;
                public var yspeed:Number = 0;
                public var udist:Number = 0;
                public var ufraction:Number = 0;
                public var edist:Number = 0;
                public var efraction:Number = 0;
                public var fullspeed:Number = 10;
                public var maxspeed:Number = 5;
                public function Game() {
                        stage.focus = this;
                        playerone.graphics.beginFill(0xffffff);
                        playerone.graphics.drawRect(0,0,20,100);
                        playerone.x = 10;
                        playerone.y = 140;
                        playerone.graphics.endFill();
                        ball.graphics.beginFill(0xffffff);
                        ball.graphics.drawCircle(270,190,20);
                        ball.graphics.endFill();
                        ball.addEventListener(Event.ENTER_FRAME,ballmovement);
                        ai.graphics.beginFill(0xffffff);
                        ai.graphics.drawRect(520,140,20,100);
                        ai.graphics.endFill();
                        ai.addEventListener(Event.ENTER_FRAME,aimovement);
                        stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
                        stage.addEventListener(Event.ENTER_FRAME,resetHandler);
                        addChild(ball);
                        addChild(playerone);
                        addChild(ai);
                }
                public function aimovement(e:Event):void {
                        if (ball.y>ai.y){
                                ai.y += 6;
                        }
                        if (ball.y<ai.y){
                                ai.y -= 6;
                        }
                }
                public function onKeyboardDown(event:KeyboardEvent):void {
                        if (event.keyCode == Keyboard.UP) {
                                if (playerone.y > 0) {
                                        playerone.y -= 5;
                                }
                        } else if (event.keyCode == Keyboard.DOWN) {
                                if (playerone.y < 300) {
                                        playerone.y +=5;
                                }
                        }
                }
                public function ballmovement(e:Event):void {
 
                        if (xspeed < maxspeed && xspeed >= 0) {
                                xspeed += 0.05;
                        }
                        if (xspeed > -maxspeed && xspeed < 0) {
                                xspeed -= 0.05;
                        }
 
                        ball.x += xspeed;
                        ball.y += yspeed;
 
                        if (ball.y>=((stage.stageHeight/2)-10)&&yspeed>0) {
                                yspeed = -yspeed;
                        }
                        if (ball.y<=-((stage.stageHeight/2)-30)&&yspeed<0) {
                                yspeed = -yspeed;
                        }
                        if (ball.hitTestObject(ai)) {//if ball hits AI paddle
                                edist=(ai.y + 5)-(ball.y + 5);
                                if (edist!=0) {
                                        efraction=edist/(ai.height/2);
                                        yspeed=-((1-Math.abs(efraction))*fullspeed);
                                        xspeed=efraction*fullspeed;
 
                                } else {
                                        xspeed=fullspeed;
                                }
                                if (xspeed>0) {
                                        xspeed=-xspeed;
                                }
                                if (xspeed > maxspeed) {
                                        xspeed = maxspeed;
                                }
                        }
                        if (ball.hitTestObject(playerone)) {//if ball hits player paddle
                                udist=(playerone.y) - (ball.y + 5);
 
                                if (udist!=0) {
                                        ufraction=udist/(playerone.height/2);
                                        yspeed=-((1-Math.abs(ufraction))*fullspeed);
                                        var oldxspeed = xspeed;
                                        xspeed=ufraction*fullspeed;
                                        if (xspeed < oldxspeed) {
                                                xspeed = oldxspeed;
                                                yspeed *= (oldxspeed / xspeed);
                                        }
                                } else if (udist==0) {
                                        xspeed=-fullspeed;
                                }
                                if (xspeed<0) {
                                        xspeed=-xspeed;
                                }
                                if (xspeed > maxspeed) {
                                        xspeed = maxspeed;
                                }
                        }
                }
                public function resetHandler(e:Event){
                        if(ball.x < (0 – 270)){
                                scoreText2.text = String(Number(scoreText2.text ) + 1)
                                reset();
                        }
                        if(ball.x > (550270)){
                                scoreText1.text = String(Number(scoreText1.text ) + 1)
                                reset();
                        }
                }
                function reset(){
                        xspeed = -0.5;
                        yspeed = 0;
                        udist = 0;
                        ufraction = 0;
                        edist = 0;
                        efraction = 0;
                        fullspeed = 10;
                        maxspeed = 5;
                        ball.y = 0;
                        ball.x = 0;
                        playerone.y = 140;
                }
        }
}