/* Pong 190m - Roy McElmurry */ package { import flash.display.Sprite; import flash.text.*; import flash.display.LoaderInfo; import flash.display.Shape; import flash.utils.Timer; import flash.events.*; import flash.geom.Point; import flash.ui.Keyboard; import flash.display.Bitmap; [SWF(backgroundColor="#ffffff", frameRate="24", width="550", height="400")] public class PongSolution extends Sprite { private const SPEED:Number = 5; private const BAR_SPEED:Number = 3; private const BALL_SIZE:Number = 40; private var p1:TextField; private var p2:TextField; private var p1bar:Shape; private var p2bar:Shape; [Embed(source="/ball.jpg")] private var ballImg:Class; private var ball:Bitmap; private var dir:Point; private var p1score:int; private var p2score:int; private var p1ScoreText:TextField; private var p2ScoreText:TextField; public function PongSolution():void { //Create Components p1 = new TextField(); p2 = new TextField(); p1bar = new Shape(); p2bar = new Shape(); ball = new ballImg() as Bitmap; dir = new Point(Math.random() * 2 - 1, Math.random() * 2 - 1); dir.normalize(1); p1score = 0; p2score = 0; p1ScoreText = new TextField(); p2ScoreText = new TextField(); //Pull out FlashVars and format the labels handleFlashVars(); formatLabels(); //Draw components setUp(); //Start game engine var engineTicker:Timer = new Timer(10); engineTicker.addEventListener(TimerEvent.TIMER, runGameEngine); engineTicker.start(); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyListener); addChild(p1); addChild(p2); addChild(p1bar); addChild(p2bar); addChild(ball); addChild(p1ScoreText); addChild(p2ScoreText); } //Grab the FlashVars and use them to set the name fields /* params = {"p1name": "name1", "p2name": "name2", ...} */ public function handleFlashVars():void { var params:Object = LoaderInfo(this.root.loaderInfo).parameters; for (var keyString:String in params) { if (keyString == "p1name") { p1.text = "P1: " + params[keyString]; } else if (keyString == "p2name") { p2.text = "P2: " + params[keyString]; } } } //Run game engine, move ball and interact ball with environment //Also updates the score public function runGameEngine(e:TimerEvent):void { ball.x += SPEED * dir.x; ball.y += SPEED * dir.y; if (ball.x < 0 ) { dir.x *= -1; p2score++; setBall(); } else if (ball.x > stage.stageWidth) { dir.x *= -1; p1score++; setBall(); } else if (ball.y < 0 || ball.y > stage.stageHeight) { dir.y *= -1; } else if (ball.hitTestObject(p1bar) || ball.hitTestObject(p2bar)) { dir.x *= -1; } updateScore(); } //Sets the score text based on the score public function updateScore():void { p1ScoreText.text = "Score: " + p1score; p2ScoreText.text = "Score: " + p2score; p1ScoreText.autoSize = TextFieldAutoSize.LEFT; p1ScoreText.setTextFormat(new TextFormat(null,24)); p1ScoreText.x = p1.x + p1.width + 20; p2ScoreText.autoSize = TextFieldAutoSize.LEFT; p2ScoreText.setTextFormat(new TextFormat(null,24)); p2ScoreText.x = p2.x + p2.width + 20; } //Start the ball in the middle of the stage with a random motion vector public function setBall():void { dir = new Point(Math.random() * 2 - 1, Math.random() * 2 - 1); dir.normalize(1); ball.width = BALL_SIZE; ball.height = BALL_SIZE; ball.x = stage.stageWidth / 2; ball.y = stage.stageHeight / 2; } //Respond to key presses and check bounds for the bars //w and s keys are hard-coded because constants exist only in Adobe Air /* Kinda buggy since pressing a second key interupts the first key event There is also delay between the initial press and the hold of the key Advanced key detection methods are needed to solve both issues */ public function keyListener(e:KeyboardEvent):void { if (e.keyCode == Keyboard.UP) { p2bar.y -= SPEED * BAR_SPEED; } else if (e.keyCode == Keyboard.DOWN) { p2bar.y += SPEED * BAR_SPEED; } else if (e.keyCode == 87) { //w p1bar.y -= SPEED * BAR_SPEED; } else if (e.keyCode == 83) { //s p1bar.y += SPEED * BAR_SPEED; } if (p1bar.y < 0) { p1bar.y = 0; } else if (p1bar.y > stage.stageHeight - p1bar.height) { p1bar.y = stage.stageHeight - p1bar.height; } else if (p2bar.y < 0) { p2bar.y = 0; } else if (p2bar.y > stage.stageHeight - p2bar.height) { p2bar.y = stage.stageHeight - p2bar.height; } } //Format the name labels public function formatLabels():void { p1.autoSize = TextFieldAutoSize.LEFT; p1.setTextFormat(new TextFormat(null,24)); p1.x = 0 p2.autoSize = TextFieldAutoSize.LEFT; p2.setTextFormat(new TextFormat(null,24)); p2.x = stage.stageWidth / 2; } //Draw the components and place them public function setUp():void { p1bar.graphics.beginFill(0x000000); p1bar.graphics.drawRect(0, 0, 15, 100); p1bar.x = 20; p1bar.y = stage.stageHeight / 2 - p1bar.height / 2; p2bar.graphics.beginFill(0x000000); p2bar.graphics.drawRect(0, 0, 15, 100); p2bar.x = stage.stageWidth - p2bar.width - 20; p2bar.y = stage.stageHeight / 2 - p2bar.height / 2; setBall(); } } }