daily pastebin goal
27%
SHARE
TWEET

A Simple Game

a guest Mar 9th, 2010 455 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********************************************
  2.  *      A Simple Game by Ian Guthridge      *
  3.  ********************************************/
  4.  
  5. //total time since the game started
  6. var ttlTime:Number = 0;
  7. //time that has elapsed since the last frame
  8. var elapsed:Number;
  9. //sime since the last blocks dropped
  10. var timeSinceDrop:Number =0;
  11. var gameOver:Boolean = false;
  12. var score:uint = 0;
  13. var rate:Number = 1;
  14. //hex color codes for our falling blocks
  15. var colors:Array = [0xcccc00, 0xff8509, 0xcc0000, 0xcc00cc, 0x0066cc, 0x00cc66, 0x66cc00];
  16. var nextBlock:int;
  17.  
  18. var fallingBlocks:Array = new Array();
  19. //initialize
  20. for(var j:int =0;j<3;j++){
  21.         //Generate a random number
  22.         nextBlock = int(Math.floor(.5+Math.random()*5));
  23.         //create a new block
  24.         var block:Box = new Box();
  25.         var colorTransform:ColorTransform = block.transform.colorTransform;
  26.         colorTransform.color = colors[nextBlock];
  27.         block.transform.colorTransform = colorTransform;
  28.         block.y = 30;
  29.         block.x = int(Math.floor(.5+Math.random()*220))+10;
  30.         fallingBlocks.push(block);
  31.         addChild(block);
  32. }
  33.  
  34. var player:Box = new Box();
  35. colorTransform = player.transform.colorTransform;
  36. colorTransform.color = 0x000000;
  37. player.transform.colorTransform = colorTransform;
  38. player.x = 130;
  39. player.y = 470;
  40. addChild(player);
  41.  
  42. this.addEventListener(Event.ENTER_FRAME, enterFunc);
  43. //the next 2 event listeners must be addeed to the stage!
  44. stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
  45. stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
  46. /*********************
  47.  * Keyboard utilities*
  48.  *********************/
  49. //Aliases for the keycodes of the arrow keys. This will save on typing.
  50. //Further, you could remap the keys without much work.
  51. var left:int = Keyboard.LEFT;
  52. var right:int = Keyboard.RIGHT;
  53. var up:int = Keyboard.UP;
  54. var down:int = Keyboard.DOWN;
  55.  
  56. //code for a fancy smancy input handler
  57. var keyArray:Array = new Array();
  58. //initialize array with "false" (in other words, no key is pressed)
  59. for(var i:int =0;i<222;i++){
  60.         keyArray.push(false);
  61. }
  62.  
  63. function checkKeyDown(event:KeyboardEvent):void{
  64.         keyArray[event.keyCode]=true;
  65. }
  66. function checkKeyUp(event:KeyboardEvent):void{
  67.         keyArray[event.keyCode]=false;
  68. }
  69. function isKeyDown(keyCode:int):Boolean{
  70.         return keyArray[keyCode];
  71. }
  72.  
  73. /*********************
  74.  *     Game Logic    *
  75.  *********************/
  76. function enterFunc(event:Event){
  77.         /*The next few lines calculate how much time has elapsed
  78.          *since the last frame. Flash should keep things at
  79.          *clost to 30 fps, but we want to make sure nothing acts wacky*/
  80.         var t:uint = getTimer();
  81.         //convert to seconds
  82.         elapsed = (t-ttlTime)/1000.0;
  83.         ttlTime += elapsed*1000;       
  84.         if(!gameOver)
  85.         {
  86.                 if(timeSinceDrop >= 2) {
  87.                         for(var j:int =0;j<3;j++){
  88.                                 //Generate a random number
  89.                                 nextBlock = int(Math.floor(.5+Math.random()*5));
  90.                                 //create a new block
  91.                                 var block:Box = new Box();             
  92.                                 var colorTransform:ColorTransform = block.transform.colorTransform;
  93.                                 colorTransform.color = colors[nextBlock];
  94.                                 block.transform.colorTransform = colorTransform;
  95.                                 block.y = 30;
  96.                                 block.x = int(Math.floor(.5+Math.random()*220))+10;
  97.                                 fallingBlocks.push(block);
  98.                                 addChild(block);
  99.                         }
  100.                         timeSinceDrop = 0;
  101.                 }
  102.                 else {
  103.                         timeSinceDrop += elapsed;
  104.                 }      
  105.                 for(var i:int = 0; i < fallingBlocks.length; i++){
  106.                         if(fallingBlocks[i].hitTestObject(player)) gameOver = true;
  107.                         else if(fallingBlocks[i].y >=510) {
  108.                                 removeChild(fallingBlocks[i]);
  109.                                 fallingBlocks.splice(i,1);
  110.                         }
  111.                         else fallingBlocks[i].y += 20*rate*elapsed;
  112.                 }
  113.                 if(isKeyDown(left) && player.x >= 10) player.x -= 100*elapsed; //move left at 40 pixels a second
  114.                 if(isKeyDown(right) && player.x <= 230) player.x += 100*elapsed; //move left at 40 pixels a second
  115.                 if(player.x < 10) player.x = 10;
  116.                 else if(player.x > 230) player.x = 230;
  117.                 if(isKeyDown(up) && player.y >= 310) player.y -= 100*elapsed; //move left at 40 pixels a second
  118.                 if(isKeyDown(down) && player.y <= 490) player.y += 100*elapsed; //move left at 40 pixels a second
  119.                 if(player.y < 310) player.y = 310;
  120.                 else if(player.y > 490) player.y = 490;
  121.                 score++;
  122.                 rate += .001;
  123.         }
  124. }
RAW Paste Data
Top