Skip to content

Instantly share code, notes, and snippets.

@cornedor
Created April 28, 2013 13:52
Show Gist options
  • Save cornedor/5476948 to your computer and use it in GitHub Desktop.
Save cornedor/5476948 to your computer and use it in GitHub Desktop.
Mondriaan LD26 entry
package;
import flash.display.MovieClip;
import flash.display.CapsStyle;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.external.ExternalInterface;
import flash.Lib;
class Main extends MovieClip
{
public var player:Player;
public var blocks:Array<Block>;
public var left:Bool;
public var right:Bool;
public var jump:Bool;
public var jumping:Bool;
public var frame:Int;
public var next:Int;
public var gameover:Bool;
public function new()
{
super();
player = new Player(380, -50, 40, 30, 0xFF0000);
addChild(player);
left = false;
right = false;
jump = false;
jumping = false;
gameover = false;
frame = 0;
next = 0;
blocks = new Array<Block>();
blocks.push(new Block(0, 0, 820, 40, 0x000000));
addChild(blocks[0]);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
public function onKeyDown(e:KeyboardEvent)
{
if(e.keyCode == 37) left = true;
if(e.keyCode == 39) right = true;
if(e.keyCode == 38 && !jumping) jump = true;
}
public function onKeyUp(e:KeyboardEvent)
{
if(e.keyCode == 37) left = false;
if(e.keyCode == 39) right = false;
}
public function onEnterFrame(e:Event)
{
frame++;
if(!left && right) player.speedX += 0.2;
else if(left && !right) player.speedX -= 0.2;
else player.speedX *= 0.89;
if(jump) {
jump = false;
jumping = true;
player.y -= 1;
player.speedY = -6;
}
player.speedY += 0.1;
for(block in blocks)
{
if(block.x < player.x + player.getWidth() && block.x + block.getWidth() > player.x &&
block.y < player.y + player.getHeight() && block.y + player.speedY + 2 > player.y + player.getHeight() &&
player.speedY > 0)
{
player.y = block.y - player.getHeight();
player.speedY = 0;
jumping = false;
if(block.getColor() == 0xFF0000) player.setWidth(player.getWidth() + 0.2);
else player.setWidth(player.getWidth() - 0.1);
}
block.y++;
}
if(--next < 0)
{
var color = [0x00FF00, 0xFF00FF, 0xFFFF00, 0x00FFFF, 0xFF0000, 0x0000FF][Math.floor(Math.random()*5.999)];
var height = Math.round(Math.random()*50) + 20;
var block:Block = new Block(Math.random() * 800, -40 - height, 50 + (Math.random()*50), height, color);
blocks.push(block);
next = height;
addChild(block);
setChildIndex(player, this.numChildren-1);
}
player.y++;
player.y += player.speedY;
player.x += player.speedX;
if(player.x < 0 && player.speedX < 0) player.x = 799 - player.x;
if(player.x > 800 && player.speedX > 0) player.x = player.x - 800;
if(player.y > 480 && !gameover)
{
trace("Game Over");
gameover = true;
ExternalInterface.call("gameover", frame);
}
}
static function main()
{
Lib.current.addChild(new Main());
}
}
class Player extends MovieClip
{
public var speedY:Float;
public var speedX:Float;
private var _width:Float;
private var _height:Float;
private var _color:UInt;
public function new(x:Float, y:Float, width:Float, height:Float, color:UInt)
{
super();
renderBlock(width, height, color);
this.x = x;
this.y = y;
this._width = width;
this._height = height;
this._color = color;
this.speedY = 0;
this.speedX = 0;
}
public inline function getWidth() {
return _width;
}
public inline function getHeight() {
return _height;
}
public function setWidth(width:Float)
{
if(width < 10 || width > 80) return;
this._width = width;
renderBlock(_width, _height, _color);
}
public function renderBlock(width:Float, height:Float, color:UInt)
{
this.graphics.clear();
this.graphics.beginFill(color);
this.graphics.drawRect(0, 0, width, height);
this.graphics.endFill();
this.graphics.lineStyle(6);
this.graphics.moveTo(0, -800);
this.graphics.lineTo(0, 800);
this.graphics.lineStyle(4);
this.graphics.moveTo(0, 0);
this.graphics.lineTo(width, 0);
this.graphics.lineStyle(2);
this.graphics.moveTo(0, height);
this.graphics.lineTo(width, height);
this.graphics.lineStyle(6);
this.graphics.moveTo(width, -800);
this.graphics.lineTo(width, 800);
}
}
class Block extends MovieClip
{
private var _width:Float;
private var _height:Float;
private var _color:UInt;
public function new(x:Float, y:Float, width:Float, height:Float, color:UInt)
{
renderBlock(x, y, width, height, color, this);
this.x = x;
this.y = y;
this._width = width;
this._height = height;
this._color = color;
super();
}
public inline function getWidth() {
return _width;
}
public inline function getHeight() {
return _height;
}
public inline function getColor() {
return _color;
}
public static inline function renderBlock(x:Float, y:Float, width:Float, height:Float, color:UInt, mc:MovieClip)
{
mc.graphics.beginFill(color);
mc.graphics.drawRect(0, 0, width, height);
mc.graphics.endFill();
mc.graphics.lineStyle(6);
mc.graphics.moveTo(-800, 0);
mc.graphics.lineTo(800, 0);
mc.graphics.lineStyle((4*Math.random()) + 2);
mc.graphics.moveTo(0, 0);
mc.graphics.lineTo(0, height);
mc.graphics.lineStyle((4*Math.random()) + 2);
mc.graphics.moveTo(width, 0);
mc.graphics.lineTo(width, height);
mc.graphics.lineStyle(6);
mc.graphics.moveTo(-800, height);
mc.graphics.lineTo(800, height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment