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 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);
                        stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown);
                        addChild(ball);
                        addChild(playerone);
                }
                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(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;
                                }
                        }
                }
        }
}