Go Back   Random Stuff Forums > Technical > Programming

Programming Discuss any type of programming here.

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 10-07-2009, 12:17 PM
Matt's Avatar
(Offline)
Administrator
 
Join Date: Jan 2007
Location: United States - Ohio
Posts: 3,820
Matt is an unknown quantity at this point
Default [Source][FlexBuilder / AS3] RSF Pong

Not the best source, but I hadn't used Flex in a while, so give me a break

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  applicationComplete="init()" viewSourceURL="srcview/index.html">
<mx:Script>
	<![CDATA[
	//Import resources
	import mx.controls.Image;
	import mx.controls.Alert;
	import flash.display.*;
	import flash.events.TimerEvent;
	import flash.events.*;
	import flash.utils.*;
	//Embed images
        [Embed(source="ball.gif")]
        public var ballClass:Class;
        [Embed(source="bar.gif")] 
        public var barClass:Class; 
        //Create bindable vars
        [Bindable]
        public var score1:int = 0;
        [Bindable]
        public var score2:int = 0;
        [Bindable]
        public var ballSpeed:Number = 0;
	//Create public vars
        public var AICenter:int;
        public var checkCre:int;
        public var difficulty:int = 1;
        public var initDirection:Boolean; 
        public var travelTimer:Timer = new Timer(50);
        public var xChange:Number;
        public var xCheck:int = booleanReturn();
       	public var yChange:Number;
       	public var yCheck:int = booleanReturn();   		
	//Begin functions
		private function addEvents():void //Run on "start" button click
		{
			init();
			this.addEventListener(KeyboardEvent.KEY_DOWN,movePaddle);
			this.travelTimer.addEventListener(TimerEvent.TIMER,startBall);
			this.travelTimer.start();
			start.enabled = false;
		}
		private function AI():void //Easy AI
		{
			if (playBall.y <= (bar2.y + AICenter) && Math.abs(playBall.y - (bar2.y + AICenter)) > 5 && bar2.y >= 5)
			{
				bar2.y -= 5;
			}
			else if (playBall.y > (bar2.y + AICenter) && Math.abs(playBall.y - (bar2.y + AICenter)) > 5  && bar2.y <= 239)
			{
				bar2.y += 5;
			}
		}
		private function AIInsane():void //Hard AI
		{	
			if (xCheck < 0)
			{
				if (bar2.y < 116)
				{
					bar2.y += 5;
				}	
				else if (bar2.y > 126)
				{
					bar2.y -= 5;
				}
			}
			else 
			{	
					if (playBall.y <= (bar2.y + AICenter) && Math.abs(playBall.y - (bar2.y + AICenter)) > 9 && bar2.y >= 5)
					{
						bar2.y -= 9;
					}
					else if (playBall.y <= (bar2.y + AICenter) && Math.abs(playBall.y - (bar2.y + AICenter)) > 5 && bar2.y >= 5)
					{
						bar2.y -= 5;
					}			
					else if (playBall.y > (bar2.y + AICenter) && Math.abs(playBall.y - (bar2.y + AICenter)) > 9  && bar2.y <= 239)
					{
						bar2.y += 9;
					}
					else if (playBall.y > (bar2.y + AICenter) && Math.abs(playBall.y - (bar2.y + AICenter)) > 5  && bar2.y <= 239)
					{
						bar2.y += 5;
					}
			}
		}	
		private function ballSpeedFunc():void
		{
			ballSpeed = 10 * Math.round(Math.sqrt(Math.pow((yCheck * yChange), 2) + Math.pow((xCheck * xChange), 2)));
		}
		private function booleanReturn():int //Returns either 1 or -1 
		{
			initDirection = Boolean(Math.floor(Math.random()*2));//Generates a random #: 0 or 1 - 0 = left, 1 = right
			if (initDirection)
			{
				checkCre = -1; 
			}
			else if (!initDirection)
			{
				checkCre = 1;
			}
			return checkCre;
		}			
		private function init():void //Misc. placements
		{
			playBall.x = 240;
			playBall.y = 151;
			bar1.x = 5;
			bar1.y = 121;
			bar2.x = 470;
			bar2.y = 121;
			xCheck = booleanReturn(); 
       		yCheck = booleanReturn();   
			xChange = 5;
			yChange = 2;
			AICenter = 30;
		}
		private function movePaddle(e:KeyboardEvent):void //User movement (bar1)
		{
			if (e.keyCode == 40 && bar1.y <= 239)//Move down
			{
				bar1.y += 5;
			}
			else if (e.keyCode == 38 && bar1.y >= 5)//Move up
			{
				bar1.y -= 5;
			}
		}
		private function resetGame():void //Runs on "reset" button click
		{
			score1 = 0;
			score2 = 0;
			start.enabled = true;
			win.text = "";
			init();
		}
		private function startBall(ring:TimerEvent):void //Ball movement and AI init.  Runs every .1 sec -> Timer
		{
			ballSpeedFunc();
			if (difficulty == 1)
			{
				AI();
			}
			else if (difficulty == 3)
			{
				AIInsane();
			}
			playBall.y += yCheck * yChange;
			playBall.x += xCheck * xChange; 	
			if (playBall.x > (bar2.x + xChange) || playBall.x < (bar1.x - xChange))
			{
				if  (playBall.x > (bar2.x + xChange))
				{
					score1++;
					start.enabled = true;
					ballSpeed = 0;
					winCheck();
				}
				else if (playBall.x < (bar1.x - xChange))
				{
					score2++;
					start.enabled = true;
					ballSpeed = 0;
					winCheck();
				}
				playBall.x = 240;
				playBall.y = 151;
				travelTimer.stop();
			}
			else if (playBall.y >= bar1.y && playBall.y <= (bar1.y + 60) && playBall.x <= 5)
			{
				xCheck = -xCheck;
				xChange++;
				yCheck = Math.abs(yCheck);
				yChange = -Math.round(((bar1.y + 30) - playBall.y) / 3);
			}
			else if (playBall.y >= bar2.y && playBall.y <= (bar2.y + 60) && playBall.x >= 470)
			{
				xCheck = -xCheck;
				xChange++;
				yCheck = Math.abs(yCheck);
				yChange = -Math.round(((bar2.y + 30) - playBall.y) / 3);
				AICenter = Math.floor(Math.random()*60);
			}		
			else if (playBall.y <= 0  || playBall.y >= 304)
			{
				yCheck = -yCheck;
			}
			xChangeReg();
		}
		private function winCheck():void //Checks if anyone has one and modifies gui accordingly
		{
			if (score1 >= 5)
			{
				win.text = "You win!";
				start.enabled = false;
			}
			else if (score2 >= 5)
			{
				win.text = "You lose!";
				start.enabled = false;
			}
		}
		private function xChangeReg():void //Caps speed in the x-direction
		{
			if (xChange >= 48)
			{
				xChange = 48;
			}
		}		
	]]>
</mx:Script>
	<mx:Panel x="161" y="10" width="500" height="344" layout="absolute" id="playareaSuper" label="playarea" title="RSF Pong" horizontalScrollPolicy="off" verticalScrollPolicy="off">
		<mx:Canvas x="0" y="0" id="playarea" width="480" height="304" horizontalScrollPolicy="off" verticalScrollPolicy="off">
			<mx:Image x="0" y="0" id="playBall" source="ball.gif"/>
			<mx:Image x="0" y="0" id="bar1" source="bar.gif"/>
			<mx:Image x="0" y="0" id="bar2" source="bar.gif"/>
		</mx:Canvas>
	</mx:Panel>	
	<mx:Button x="669" y="10" label="Start" click="addEvents()" id="start"/>
	<mx:Text x="24.5" y="199" width="128.5" height="30" id="play1" text="{score1}" color="#FF0000" fontSize="18"/>
	<mx:Text x="689" y="199" id="play2" width="124" height="30" text="{score2}" color="#FF0000" fontSize="18"/>
	<mx:Button x="669" y="40" click="resetGame()" id="reset" label="Reset"/>
	<mx:Text x="846" y="16" text="AI Difficulty&#xa;" color="#42FF00" fontSize="14"/>
	<mx:RadioButton x="860" y="40" id="easy" click="difficulty=1" label="Easy" selected="true"/>
	<mx:RadioButton x="860" y="60" id="hard" click="difficulty=3" label="Hard"/>
	<mx:Text x="817" y="116" text="Current Ball Speed (in px/s)"/>
	<mx:Text x="861" y="142" text="{ballSpeed}"/>
	<mx:Text x="788" y="287" id="win" width="145" height="49" color="#FF0000" fontSize="18"/>
</mx:Application>
__________________
"Facts do not cease to exist because they are ignored."
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 04:29 AM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
Copyright © 2006 - 2010, Rsforums.org