Skip to content

Instantly share code, notes, and snippets.

@PifyZ
Created August 13, 2014 15:21
Show Gist options
  • Save PifyZ/d650091aba8fd5876a39 to your computer and use it in GitHub Desktop.
Save PifyZ/d650091aba8fd5876a39 to your computer and use it in GitHub Desktop.
Pify Bird en Haxe
package pifybird;
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import openfl.display.Tilesheet;
import openfl.Assets;
class Bird extends Sprite {
private var bitmap:BitmapData;
private var sheet:Tilesheet;
private var delta:Int;
private var frame:Int;
public var dy:Float;
public function new() {
super();
x = 28;
y = 50;
dy = 0;
bitmap = Assets.getBitmapData("gfx/bird.png");
sheet = new Tilesheet(bitmap);
delta = 0;
frame = 0;
sheet.addTileRect(new Rectangle(0, 0, 17, 12));
sheet.addTileRect(new Rectangle(17, 0, 17, 12));
sheet.addTileRect(new Rectangle(34, 0, 17, 12));
}
public function update(dt:Int) {
delta += dt;
dy += Game.GRAVITY;
if (rotation < 90)
rotation += Game.GRAVITY * 2.5;
if (dy > Game.MAX_GRAVITY)
dy = Game.MAX_GRAVITY;
if (y < 0)
dy = 1;
if (y > 500)
dy = -1;
y += dy;
if (delta > 120) {
frame++;
delta = 0;
if (frame > 2)
frame = 0;
}
}
public function draw() {
graphics.clear();
sheet.drawTiles(graphics, [ -(bitmap.width / 3 / 2), -bitmap.height / 2, frame ]);
}
}
package pifybird;
typedef Box = {
x:Float,
y:Float,
width:Float,
height:Float
};
typedef Circle = {
x:Float,
y:Float,
r:Float
};
class Game {
public static inline var GRAVITY:Float = 0.7;
public static inline var MAX_GRAVITY:Float = 10;
public static function box_hit_circle(box:Box, circle:Circle):Bool {
var half_width:Float = box.width / 2;
var half_height:Float = box.height / 2;
var cx:Float = Math.abs(circle.x - box.x - half_width);
var xDist:Float = half_width + circle.r;
if (cx > xDist)
return false;
var cy:Float = Math.abs(circle.y - box.y - half_height);
var yDist:Float = half_height + circle.r;
if (cy > yDist)
return false;
if (cx <= half_width || cy <= half_height)
return true;
var xCornerDist:Float = cx - half_width;
var yCornerDist:Float = cy - half_height;
var xCornerDistSq:Float = xCornerDist * xCornerDist;
var yCornerDistSq:Float = yCornerDist * yCornerDist;
var maxCornerDistSq:Float = circle.r * circle.r;
return xCornerDistSq + yCornerDistSq <= maxCornerDistSq;
}
}
package pifybird;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.Lib;
class PifyBird extends Sprite {
private var bird:Bird;
private var timer:Stopwatch;
private var pipes:List<Pipe>;
private var pipes_container:Sprite;
private var bg:MovieClip;
private var scale:Int;
public function new() {
super();
scale = 2;
scaleX = scale;
scaleY = scale;
bg = new MovieClip();
addChild(bg);
on_resize(null);
pipes = new List<Pipe>();
pipes_container = new Sprite();
addChild(pipes_container);
bird = new Bird();
addChild(bird);
timer = new Stopwatch();
stage.addEventListener(Event.RESIZE, on_resize);
addEventListener(Event.ENTER_FRAME, update);
addEventListener(MouseEvent.MOUSE_DOWN, on_click);
#if android
stage.addEventListener(KeyboardEvent.KEY_UP, on_keyup);
#end
}
public function update(e:Event) {
timer.update();
var last_pipe = pipes.first();
//if (last_pipe != null)
// trace(last_pipe.x);
//trace(pipes.length);
if (pipes.length == 0 || last_pipe != null) {
if (pipes.length == 0 || last_pipe.x < Std.int(stage.stageWidth / scale) - 160) {
var a_pipe = new Pipe(Std.int(stage.stageWidth / scale), Std.int(16 + Math.random() * (stage.stageHeight / scale - 90)));
pipes.push(a_pipe);
pipes_container.addChild(a_pipe);
}
}
for (pipe in pipes) {
pipe.update(timer.dt);
#if debug
trace({
y1: pipe.y,
y2: pipe.y + pipe.top_height + 140
});
#end
if (Game.box_hit_circle({
x: pipe.x,
y: pipe.y,
width: pipe.width,
height: pipe.top_height / scale
}, {
x: bird.x,
y: bird.y,
r: 8
}) || Game.box_hit_circle({
x: pipe.x,
y: pipe.y + pipe.top_height + 140,
width: pipe.width,
height: stage.stageHeight
}, {
x: bird.x,
y: bird.y,
r: 8
})
) {
pipes.clear();
pipes_container.removeChildren();
timer = new Stopwatch();
}
if (pipe.x + 50 < 0) {
pipes.remove(pipe);
pipes_container.removeChild(pipe);
}
}
bird.update(timer.dt);
draw(e);
}
public inline function draw(e:Event) {
bird.draw();
}
public function on_click(e:Event) {
bird.rotation = -34;
bird.dy = -8;
}
private function on_keyup(e:KeyboardEvent):Void {
#if android
//if (e.keyCode == 27) {
e.stopImmediatePropagation();
Lib.exit();
//}
#end
}
private function on_resize(e:Event):Void {
bg.graphics.clear();
bg.graphics.beginFill(0x71C5CF);
bg.graphics.moveTo(0, 0);
bg.graphics.lineTo(stage.stageWidth, 0);
bg.graphics.lineTo(stage.stageWidth, stage.stageHeight);
bg.graphics.lineTo(0, stage.stageHeight);
bg.graphics.endFill();
}
}
package pifybird;
import flash.display.Sprite;
import flash.display.Bitmap;
import openfl.Assets;
class Pipe extends Sprite {
private var img_pipe:Bitmap;
private var img_pipe_end:Bitmap;
private var img_top_pipe:Bitmap;
private var img_top_pipe_end:Bitmap;
public var top_height:Int;
public function new(init_x:Int, h:Int) {
super();
x = init_x;
y = 0;
top_height = h * 2;
img_top_pipe = new Bitmap(Assets.getBitmapData("gfx/pipe.png"));
img_top_pipe.height = h;
width = img_top_pipe.width;
img_top_pipe_end = new Bitmap(Assets.getBitmapData("gfx/pipe_end.png"));
img_top_pipe_end.x = -1;
img_top_pipe_end.y = h - img_top_pipe_end.height;
img_pipe = new Bitmap(Assets.getBitmapData("gfx/pipe.png"));
img_pipe.y = h + 70;
img_pipe.height = 800;
img_pipe_end = new Bitmap(Assets.getBitmapData("gfx/pipe_end.png"));
img_pipe_end.x = -1;
img_pipe_end.scaleY = -1;
img_pipe_end.y = h + 70 + img_pipe_end.height;
addChild(img_top_pipe);
addChild(img_top_pipe_end);
addChild(img_pipe);
addChild(img_pipe_end);
scaleX = 1;
scaleY = 1;
}
public inline function update(dt:Int) {
x -= 4;
}
}
package pifybird;
import flash.Lib;
class Stopwatch {
public var dt:Int;
private var last:Int;
private var time:Int;
public function new() {
dt = 0;
last = Lib.getTimer();
time = 0;
}
public inline function update() {
dt = Lib.getTimer() - last;
last = Lib.getTimer();
time += dt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment