Advertisement
cornedor

KingsExercise

May 2nd, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ;
  2.  
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.events.Event;
  6. import flash.events.KeyboardEvent;
  7. import flash.events.TimerEvent;
  8. import flash.geom.Matrix;
  9. import flash.geom.Point;
  10. import flash.geom.Rectangle;
  11. import flash.Lib;
  12. import flash.text.TextField;
  13. import flash.utils.Timer;
  14.  
  15. /**
  16.  * KingsExersice is a game where you need to run as much as possible without being hit by any obstacels. You can compile it using haxe.
  17.  * @author Corné Dorrestijn
  18.  */
  19.  
  20. class Main extends Bitmap
  21. {
  22.     private var bmd:BitmapData;
  23.     private var guys:Array<Guy>;
  24.     private var goLeft:Bool;
  25.     private var jump:Bool;
  26.     private var goRight:Bool;
  27.     private var groundEnemys:Array<GroundEnemy>;
  28.     private var airEnemys:Array<AirEnemy>;
  29.     private var scoreText:TextField;
  30.     private var score:Float;
  31.     private var healthy:Int;
  32.     private var timer:Timer;
  33.     public function new()
  34.     {
  35.         bmd = new BitmapData(320, 240, false, 0xFFFFFF);
  36.         super(bmd);
  37.         width = 640;
  38.         height = 480;
  39.         prepare();
  40.     }
  41.     private function prepare():Void
  42.     {
  43.         goLeft = false;
  44.         goRight = false;
  45.         jump = false;
  46.         guys = new Array();
  47.         groundEnemys = new Array();
  48.         airEnemys = new Array();
  49.         for(i in 0...10) guys.push(new Guy());
  50.         guys[0].pos.x = 20;
  51.         guys[0].pos.y = 190;
  52.         for (i in 0...2) groundEnemys.push(new GroundEnemy());
  53.         for (i in 0...10) airEnemys.push(new AirEnemy());
  54.         healthy = guys.length;
  55.         scoreText = new TextField();
  56.         scoreText.width = 320;
  57.         scoreText.multiline = true;
  58.         scoreText.htmlText = "<font face=\"Arial\" color=\"#FF0000\" size=\"10\">The King: It’s Dangerous to go Alone! Take this men!<p>\nSon of the king: Thank you father, Now i can exercise safely</font>";
  59.         bmd.draw(scoreText);
  60.         bmd.fillRect(new Rectangle(0, 200, 320, 120), 0);
  61.         for (i in 0...10)
  62.         {
  63.             var g:Guy = new Guy();
  64.             g.pos.y = 190;
  65.             g.pos.x = Math.round(Math.random() * 300) + 20;
  66.             bmd.draw(g, g.getMatrix());
  67.         }
  68.         score = 0;
  69.         timer = new Timer(5000, 1);
  70.         timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
  71.         timer.start();
  72.         Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
  73.         Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
  74.     }
  75.     private function onTimerComplete(e:TimerEvent)
  76.     {
  77.         addEventListener(Event.ENTER_FRAME, onEnterFrame);
  78.     }
  79.     private function onKeyDown(e:KeyboardEvent)
  80.     {
  81.         if (e.keyCode == 37) goLeft = true;
  82.         if (e.keyCode == 38) jump = true;
  83.         if (e.keyCode == 39) goRight = true;
  84.         if (e.keyCode == 82)
  85.         {
  86.             removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  87.             removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
  88.             removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
  89.             for (a in guys) a = null;
  90.             for (b in groundEnemys) b = null;
  91.             for (c in airEnemys) c = null;
  92.             guys = null;
  93.             groundEnemys = null;
  94.             airEnemys = null;
  95.             bmd.fillRect(bmd.rect, 0xFFFFFF);
  96.             timer.stop();
  97.             timer = null;
  98.            
  99.             prepare();
  100.            
  101.         }
  102.     }
  103.     private inline function onKeyUp(e:KeyboardEvent)
  104.     {
  105.         if (e.keyCode == 37) goLeft = false;
  106.         if (e.keyCode == 38) jump = false;
  107.         if (e.keyCode == 39) goRight = false;
  108.     }
  109.     private function onEnterFrame(e:Event)
  110.     {
  111.         bmd.lock();
  112.         bmd.fillRect(bmd.rect, 0xFFFFFF);
  113.         for (g in groundEnemys)
  114.         {
  115.             bmd.draw(g, g.getMatrix());
  116.         }
  117.         for (a in airEnemys)
  118.         {
  119.             bmd.draw(a, a.getMatrix());
  120.         }
  121.         for (i in 0...guys.length)
  122.         {
  123.             if (i == 0)
  124.             {
  125.                 if (goLeft && !goRight)
  126.                 {
  127.                     if (guys[0].xSpeed >= -2) guys[0].xSpeed -= 0.1;
  128.                    
  129.                 }
  130.                 if (!goLeft && goRight)
  131.                 {
  132.                     if (guys[0].xSpeed <= 2) guys[0].xSpeed += 0.1;
  133.                 }
  134.                 if (guys[i].pos.y < 190)
  135.                 {
  136.                     guys[0].ySpeed += 0.1;
  137.                     jump = false;
  138.                 }
  139.                 if (jump) guys[0].ySpeed = -3;
  140.                 if (guys[0].pos.y > 191)
  141.                 {
  142.                     guys[0].ySpeed = 0;
  143.                     guys[0].pos.y = 190;
  144.                    
  145.                 }
  146.                 if (!goLeft && !goRight) guys[0].xSpeed /= 1.1;
  147.                 if (guys[0].pos.x >= 0 && guys[0].pos.x < 315) guys[i].pos.x += guys[i].xSpeed;
  148.                 if (guys[0].pos.x < 0) guys[0].pos.x = 1;
  149.                 if (guys[0].pos.x >= 315) guys[0].pos.x = 313;
  150.                 guys[0].pos.y += guys[0].ySpeed;
  151.                 score += Math.floor(Math.abs(guys[0].xSpeed));
  152.             }else if (i != 0)
  153.             {
  154.                 guys[i].pos.x += (guys[i - 1].pos.x - guys[i].pos.x) / 4;
  155.                 guys[i].pos.y += Math.ceil((guys[i - 1].pos.y - guys[i].pos.y) / 4);
  156.                 for (a in airEnemys)
  157.                 {
  158.                     var r1:Bool = a.x <= guys[i].pos.x+7 && a.x >= guys[i].pos.x;
  159.                     var r2:Bool = a.y <= guys[i].pos.y+13 && a.y >= guys[i].pos.y;
  160.                     if (r1 && r2 && guys[i].alive)
  161.                     {
  162.                         guys[i].kill();
  163.                         healthy--;                     
  164.                     }
  165.                 }
  166.                 for (g in groundEnemys)
  167.                 {
  168.                     var r1:Bool = g.x <= guys[i].pos.x+7 && g.x >= guys[i].pos.x;
  169.                     var r2:Bool = 198 <= guys[i].pos.y+13 && 198 >= guys[i].pos.y;
  170.                     if (r1 && r2 && guys[i].alive)
  171.                     {
  172.                         guys[i].kill();
  173.                         healthy--;                     
  174.                     }
  175.                 }
  176.             }
  177.             bmd.draw(guys[i], guys[i].getMatrix());
  178.         }
  179.         scoreText.htmlText = "<font face=\"Arial\">Score: "+ score + " - Healthy: " + healthy + "</font>";
  180.         if (healthy == 1)
  181.         {
  182.             removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  183.             scoreText.htmlText = "<font face=\"Arial\">Son of the king: This weak people are all hurt.\nThe King: Oh son.. Press R and you can get more people.\nScore: " + score + "</font>\n\n<font size=\"9\">(C) corne.info</font>";
  184.         }
  185.        
  186.         bmd.draw(scoreText);
  187.         bmd.fillRect(new Rectangle(0, 200, 320, 120), 0);
  188.         bmd.unlock();
  189.     }
  190.     static function main()
  191.     {
  192.         Lib.current.addChild(new Main());
  193.     }  
  194. }
  195. class Guy extends BitmapData
  196. {
  197.     public var pos:Point;
  198.     public var xSpeed:Float;
  199.     public var ySpeed:Float;
  200.     public var alive:Bool;
  201.     public function new()
  202.     {
  203.         super(7, 13, true, 0x00000000);
  204.         pos = new Point();
  205.         fillRect(new Rectangle(2, 0, 3, 3), 0xFF000000);
  206.         fillRect(new Rectangle(2, 4, 3, 4), 0xFF000000);
  207.         fillRect(new Rectangle(2, 8, 1, 2), 0xFF000000);
  208.         fillRect(new Rectangle(4, 8, 1, 2), 0xFF000000);
  209.         fillRect(new Rectangle(0, 4, 7, 1), 0xFF000000);
  210.         fillRect(new Rectangle(3, 3, 1, 1), 0xFF000000);
  211.         setPixel(3, 1, 0xFFFFFFFF);
  212.         setPixel(3, 5, 0xFFFFFFFF);
  213.         setPixel(3, 6, 0xFFFFFFFF);
  214.         xSpeed = 0;
  215.         ySpeed = 0;
  216.         alive = true;
  217.     }
  218.     inline public function kill():Void
  219.     {
  220.         alive = false;
  221.         floodFill(2, 0, 0xFFFF0000);
  222.     }
  223.     inline public function getMatrix():Matrix
  224.     {
  225.         var mtr:Matrix = new Matrix();
  226.         mtr.tx = Std.int(pos.x);
  227.         mtr.ty = Std.int(pos.y);
  228.         return mtr;
  229.     }
  230.    
  231. }
  232. class GroundEnemy extends BitmapData
  233. {
  234.     public var x:Float;
  235.     public var speed:Float;
  236.     inline public function new()
  237.     {
  238.         super(4, 2, true, 0x00000000);
  239.         fillRect(new Rectangle(1, 0, 2, 1), 0xFF000000);
  240.         fillRect(new Rectangle(0, 1, 4, 1), 0xFF000000);
  241.         speed = (Math.random() + 1)/2;
  242.         x = (Math.random()*230) + 100;
  243.     }
  244.     inline public function getMatrix():Matrix
  245.     {
  246.         var mtr:Matrix = new Matrix();
  247.         mtr.ty = 198;
  248.         x -= speed;
  249.         if (x <= 0) x = 390;
  250.         mtr.tx = Std.int(x);
  251.         return mtr;
  252.     }
  253. }
  254. class AirEnemy extends BitmapData
  255. {
  256.     public var y:Float;
  257.     public var x:Float;
  258.     public var speed:Float;
  259.     inline public function new()
  260.     {
  261.         super(4, 4, true, 0x00000000);
  262.         fillRect(new Rectangle(1, 0, 2, 1), 0xFF000000);
  263.         fillRect(new Rectangle(1, 3, 2, 1), 0xFF000000);
  264.         fillRect(new Rectangle(0, 1, 4, 2), 0xFF000000);
  265.         speed = (Math.random() + 1)/2;
  266.         x = Math.random() * 320;
  267.         y = -10;
  268.     }
  269.     inline public function getMatrix():Matrix
  270.     {
  271.         var mtr:Matrix = new Matrix();
  272.         mtr.tx = Std.int(x);
  273.         y += speed;
  274.         if (y >= 210)
  275.         {
  276.             y = -10;
  277.             x = Math.random() * 320;
  278.         }
  279.         mtr.ty = Std.int(y);
  280.         return mtr;
  281.     }
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement