/*------------------------- *Pong Class *Developed by Carloz.Yanez *Creates a Pong Game --------------------------*/ package { import flash.display.*; import flash.ui.Mouse; import flash.events.TimerEvent; import flash.events.MouseEvent; import flash.text.TextField; import flash.text.TextFieldType; import flash.text.TextFormat; import flash.text.TextFieldAutoSize; import flash.utils.Timer; public class Pong extends MovieClip { public function Pong(bgColor:uint = 0x000000, playerColor:uint = 0xFFFFFF, enemyColor:uint = 0xFFFFFF, ballColor:uint = 0xFFFFFF, middleLineColor:uint = 0xFFFFFF, ballSpeed:int = 2, scoreColor:uint = 0xFFFFFF, msgColor:uint = 0xFFFFFF, msg:String = "Click here to start", scoreFont:String = "Arial", scoreSize:int = 30):void { //No Context menu stage.showDefaultContextMenu = false; //Stage Constants for Scaling const STAGE_WIDTH:int = stage.stageWidth; const STAGE_HEIGHT:int = stage.stageHeight; //Create a Background var bg:MovieClip = new MovieClip(); bg.graphics.beginFill(bgColor); bg.graphics.drawRect(0, 0, STAGE_WIDTH, STAGE_HEIGHT); bg.graphics.endFill(); addChild(bg); //Create the Player and set its position var player:MovieClip = new MovieClip(); player.graphics.beginFill(playerColor); player.graphics.drawRect(0, 0, 8, 50); player.graphics.endFill(); player.x = player.width; player.y = (STAGE_HEIGHT / 2) - (player.height / 2); addChild(player); //Create the Enemy and set its position var enemy:MovieClip = new MovieClip(); enemy.graphics.beginFill(enemyColor); enemy.graphics.drawRect(0, 0, 8, 50); enemy.graphics.endFill(); enemy.x = STAGE_WIDTH - enemy.width * 2; enemy.y = (STAGE_HEIGHT / 2) - (enemy.height / 2); addChild(enemy); //Create the Ball and set its position var ball:MovieClip = new MovieClip(); ball.graphics.beginFill(ballColor); ball.graphics.drawRect(0, 0, 10, 10); ball.graphics.endFill(); ball.x = STAGE_WIDTH / 2 - ball.width / 2; ball.y = STAGE_HEIGHT / 2 - ball.height / 2; addChild(ball); //Create the Middle line in stage var middleLine:MovieClip = new MovieClip(); middleLine.graphics.lineStyle(2, middleLineColor); middleLine.graphics.lineTo(0, STAGE_HEIGHT); middleLine.x = STAGE_WIDTH / 2; middleLine.y = 0; addChild(middleLine); //Set the Player´s movement to Mouse function playerMovement(event:TimerEvent):void { player.y = mouseY - player.height / 2; if(player.y >= STAGE_HEIGHT - player.height) { player.y = STAGE_HEIGHT - player.height; } if(player.y <= 0) { player.y = 0; } } //Enemy movement AI //enemy.speed = Math.floor(Math.random() * (5) + 1); function enemyAI(event:TimerEvent):void { /*if(ball.x >= middleLine.x && ball.y >= (STAGE_HEIGHT / 2)) { enemy.y += enemy.speed; } if(ball.x >= middleLine.x && ball.y <= (STAGE_HEIGHT / 2)) { enemy.y -= enemy.speed; }*/ enemy.y = ball.y - enemy.height / 2;//Invinsible mode if(enemy.y >= STAGE_HEIGHT - enemy.height) { enemy.y = STAGE_HEIGHT - enemy.height; } if(enemy.y <= 0) { enemy.y = 0; } } //Ball movement ball.speed = ballSpeed; ball.dir = 0; player.score = 0; enemy.score = 0; function ballMovement(event:TimerEvent):void { if(playin) { ball.x -= ball.speed; ball.y -= ball.dir; } if(ball.x <= player.x) { enemy.score++; restart(); } if(ball.x >= enemy.x) { player.score++; restart(); } if(ball.y > (STAGE_HEIGHT - ball.height / 2)) { ball.dir *= -1; } if(ball.y < ball.height / 2) { ball.dir *= -1; } } //Hit Testing function checkHit(event:TimerEvent):void { if(ball.hitTestObject(player) || ball.hitTestObject(enemy)) { ball.speed *= -1; ball.dir = (Math.random() * 1) + (Math.random() * -1); //enemy.speed = Math.floor(Math.random() * (5) + 1); } } //Score handler var scoreFormat:TextFormat = new TextFormat(); scoreFormat.color = scoreColor; scoreFormat.font = scoreFont; scoreFormat.size = scoreSize; var playerScore:TextField = new TextField(); playerScore.autoSize = TextFieldAutoSize.CENTER; playerScore.x = STAGE_WIDTH / 4; playerScore.y = STAGE_HEIGHT / 10; playerScore.selectable = false; playerScore.defaultTextFormat = scoreFormat; playerScore.type = TextFieldType.DYNAMIC; addChild(playerScore); var enemyScore:TextField = new TextField(); enemyScore.autoSize = TextFieldAutoSize.CENTER; enemyScore.x = (STAGE_WIDTH) - (STAGE_WIDTH / 4) enemyScore.y = STAGE_HEIGHT / 10; enemyScore.selectable = false; enemyScore.defaultTextFormat = scoreFormat; enemyScore.type = TextFieldType.DYNAMIC; addChild(enemyScore); function scoreHandler(event:TimerEvent) { playerScore.text = player.score; enemyScore.text = enemy.score; } //Start Message var startMsg:TextField = new TextField(); var startFormat:TextFormat = new TextFormat(); startFormat.color = msgColor; startFormat.bold = true; startMsg.x = 0; startMsg.y = (STAGE_HEIGHT) - (STAGE_HEIGHT / 12); startMsg.selectable = false; startMsg.type = TextFieldType.DYNAMIC; startMsg.defaultTextFormat = startFormat; startMsg.text = msg; addChild(startMsg); //Start handler var playin:Boolean = false; function startHandler(event:MouseEvent):void { Mouse.hide(); playin = true; timer.start(); startMsg.text = ""; } //Restart function function restart():void { ball.x = STAGE_WIDTH / 2 - ball.width / 2; ball.y = STAGE_HEIGHT / 2 - ball.height / 2; //enemy.speed = Math.floor(Math.random() * (5) + 1);//Invinsible mode on ball.speed = ballSpeed; ball.dir = 0; } //Apply startHandler function Event Listener stage.addEventListener(MouseEvent.CLICK, startHandler); //Apply Timer to Timer functions var timer:Timer = new Timer(1, 0); timer.addEventListener(TimerEvent.TIMER, playerMovement); timer.addEventListener(TimerEvent.TIMER, enemyAI); timer.addEventListener(TimerEvent.TIMER, ballMovement); timer.addEventListener(TimerEvent.TIMER, checkHit); timer.addEventListener(TimerEvent.TIMER, scoreHandler); } } }