Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ;
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.events.TimerEvent;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.Lib;
- import flash.text.TextField;
- import flash.utils.Timer;
- /**
- * 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.
- * @author Corné Dorrestijn
- */
- class Main extends Bitmap
- {
- private var bmd:BitmapData;
- private var guys:Array<Guy>;
- private var goLeft:Bool;
- private var jump:Bool;
- private var goRight:Bool;
- private var groundEnemys:Array<GroundEnemy>;
- private var airEnemys:Array<AirEnemy>;
- private var scoreText:TextField;
- private var score:Float;
- private var healthy:Int;
- private var timer:Timer;
- public function new()
- {
- bmd = new BitmapData(320, 240, false, 0xFFFFFF);
- super(bmd);
- width = 640;
- height = 480;
- prepare();
- }
- private function prepare():Void
- {
- goLeft = false;
- goRight = false;
- jump = false;
- guys = new Array();
- groundEnemys = new Array();
- airEnemys = new Array();
- for(i in 0...10) guys.push(new Guy());
- guys[0].pos.x = 20;
- guys[0].pos.y = 190;
- for (i in 0...2) groundEnemys.push(new GroundEnemy());
- for (i in 0...10) airEnemys.push(new AirEnemy());
- healthy = guys.length;
- scoreText = new TextField();
- scoreText.width = 320;
- scoreText.multiline = true;
- 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>";
- bmd.draw(scoreText);
- bmd.fillRect(new Rectangle(0, 200, 320, 120), 0);
- for (i in 0...10)
- {
- var g:Guy = new Guy();
- g.pos.y = 190;
- g.pos.x = Math.round(Math.random() * 300) + 20;
- bmd.draw(g, g.getMatrix());
- }
- score = 0;
- timer = new Timer(5000, 1);
- timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
- timer.start();
- Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
- Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
- }
- private function onTimerComplete(e:TimerEvent)
- {
- addEventListener(Event.ENTER_FRAME, onEnterFrame);
- }
- private function onKeyDown(e:KeyboardEvent)
- {
- if (e.keyCode == 37) goLeft = true;
- if (e.keyCode == 38) jump = true;
- if (e.keyCode == 39) goRight = true;
- if (e.keyCode == 82)
- {
- removeEventListener(Event.ENTER_FRAME, onEnterFrame);
- removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
- removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
- for (a in guys) a = null;
- for (b in groundEnemys) b = null;
- for (c in airEnemys) c = null;
- guys = null;
- groundEnemys = null;
- airEnemys = null;
- bmd.fillRect(bmd.rect, 0xFFFFFF);
- timer.stop();
- timer = null;
- prepare();
- }
- }
- private inline function onKeyUp(e:KeyboardEvent)
- {
- if (e.keyCode == 37) goLeft = false;
- if (e.keyCode == 38) jump = false;
- if (e.keyCode == 39) goRight = false;
- }
- private function onEnterFrame(e:Event)
- {
- bmd.lock();
- bmd.fillRect(bmd.rect, 0xFFFFFF);
- for (g in groundEnemys)
- {
- bmd.draw(g, g.getMatrix());
- }
- for (a in airEnemys)
- {
- bmd.draw(a, a.getMatrix());
- }
- for (i in 0...guys.length)
- {
- if (i == 0)
- {
- if (goLeft && !goRight)
- {
- if (guys[0].xSpeed >= -2) guys[0].xSpeed -= 0.1;
- }
- if (!goLeft && goRight)
- {
- if (guys[0].xSpeed <= 2) guys[0].xSpeed += 0.1;
- }
- if (guys[i].pos.y < 190)
- {
- guys[0].ySpeed += 0.1;
- jump = false;
- }
- if (jump) guys[0].ySpeed = -3;
- if (guys[0].pos.y > 191)
- {
- guys[0].ySpeed = 0;
- guys[0].pos.y = 190;
- }
- if (!goLeft && !goRight) guys[0].xSpeed /= 1.1;
- if (guys[0].pos.x >= 0 && guys[0].pos.x < 315) guys[i].pos.x += guys[i].xSpeed;
- if (guys[0].pos.x < 0) guys[0].pos.x = 1;
- if (guys[0].pos.x >= 315) guys[0].pos.x = 313;
- guys[0].pos.y += guys[0].ySpeed;
- score += Math.floor(Math.abs(guys[0].xSpeed));
- }else if (i != 0)
- {
- guys[i].pos.x += (guys[i - 1].pos.x - guys[i].pos.x) / 4;
- guys[i].pos.y += Math.ceil((guys[i - 1].pos.y - guys[i].pos.y) / 4);
- for (a in airEnemys)
- {
- var r1:Bool = a.x <= guys[i].pos.x+7 && a.x >= guys[i].pos.x;
- var r2:Bool = a.y <= guys[i].pos.y+13 && a.y >= guys[i].pos.y;
- if (r1 && r2 && guys[i].alive)
- {
- guys[i].kill();
- healthy--;
- }
- }
- for (g in groundEnemys)
- {
- var r1:Bool = g.x <= guys[i].pos.x+7 && g.x >= guys[i].pos.x;
- var r2:Bool = 198 <= guys[i].pos.y+13 && 198 >= guys[i].pos.y;
- if (r1 && r2 && guys[i].alive)
- {
- guys[i].kill();
- healthy--;
- }
- }
- }
- bmd.draw(guys[i], guys[i].getMatrix());
- }
- scoreText.htmlText = "<font face=\"Arial\">Score: "+ score + " - Healthy: " + healthy + "</font>";
- if (healthy == 1)
- {
- removeEventListener(Event.ENTER_FRAME, onEnterFrame);
- 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>";
- }
- bmd.draw(scoreText);
- bmd.fillRect(new Rectangle(0, 200, 320, 120), 0);
- bmd.unlock();
- }
- static function main()
- {
- Lib.current.addChild(new Main());
- }
- }
- class Guy extends BitmapData
- {
- public var pos:Point;
- public var xSpeed:Float;
- public var ySpeed:Float;
- public var alive:Bool;
- public function new()
- {
- super(7, 13, true, 0x00000000);
- pos = new Point();
- fillRect(new Rectangle(2, 0, 3, 3), 0xFF000000);
- fillRect(new Rectangle(2, 4, 3, 4), 0xFF000000);
- fillRect(new Rectangle(2, 8, 1, 2), 0xFF000000);
- fillRect(new Rectangle(4, 8, 1, 2), 0xFF000000);
- fillRect(new Rectangle(0, 4, 7, 1), 0xFF000000);
- fillRect(new Rectangle(3, 3, 1, 1), 0xFF000000);
- setPixel(3, 1, 0xFFFFFFFF);
- setPixel(3, 5, 0xFFFFFFFF);
- setPixel(3, 6, 0xFFFFFFFF);
- xSpeed = 0;
- ySpeed = 0;
- alive = true;
- }
- inline public function kill():Void
- {
- alive = false;
- floodFill(2, 0, 0xFFFF0000);
- }
- inline public function getMatrix():Matrix
- {
- var mtr:Matrix = new Matrix();
- mtr.tx = Std.int(pos.x);
- mtr.ty = Std.int(pos.y);
- return mtr;
- }
- }
- class GroundEnemy extends BitmapData
- {
- public var x:Float;
- public var speed:Float;
- inline public function new()
- {
- super(4, 2, true, 0x00000000);
- fillRect(new Rectangle(1, 0, 2, 1), 0xFF000000);
- fillRect(new Rectangle(0, 1, 4, 1), 0xFF000000);
- speed = (Math.random() + 1)/2;
- x = (Math.random()*230) + 100;
- }
- inline public function getMatrix():Matrix
- {
- var mtr:Matrix = new Matrix();
- mtr.ty = 198;
- x -= speed;
- if (x <= 0) x = 390;
- mtr.tx = Std.int(x);
- return mtr;
- }
- }
- class AirEnemy extends BitmapData
- {
- public var y:Float;
- public var x:Float;
- public var speed:Float;
- inline public function new()
- {
- super(4, 4, true, 0x00000000);
- fillRect(new Rectangle(1, 0, 2, 1), 0xFF000000);
- fillRect(new Rectangle(1, 3, 2, 1), 0xFF000000);
- fillRect(new Rectangle(0, 1, 4, 2), 0xFF000000);
- speed = (Math.random() + 1)/2;
- x = Math.random() * 320;
- y = -10;
- }
- inline public function getMatrix():Matrix
- {
- var mtr:Matrix = new Matrix();
- mtr.tx = Std.int(x);
- y += speed;
- if (y >= 210)
- {
- y = -10;
- x = Math.random() * 320;
- }
- mtr.ty = Std.int(y);
- return mtr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement