/**** * Classes ****/ // Bird class var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); self.gravity = 0.5; self.lift = -10; self.velocity = 8; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; if (self.y > 2732 - birdGraphics.height / 2) { self.y = 2732 - birdGraphics.height / 2; self.velocity = 0; } if (self.y < birdGraphics.height / 2) { self.y = birdGraphics.height / 2; self.velocity = 0; } }; self.flap = function () { self.velocity = self.lift; }; }); //<Assets used in the game will automatically appear here> // Pipe class var Pipe = Container.expand(function () { var self = Container.call(this); var pipeGraphics = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -pipeGraphics.width) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Init game with sky blue background }); /**** * Game Code ****/ var bird; var pipes = []; var score = 0; var scoreTxt; var pipeInterval; function resetGame() { bird = game.addChild(new Bird()); bird.x = 2048 / 4; bird.y = 2732 / 2; pipes.forEach(function (pipe) { pipe.destroy(); }); pipes = []; score = 0; scoreTxt.setText(score); if (pipeInterval) { LK.clearInterval(pipeInterval); } pipeInterval = LK.setInterval(function () { var pipeTop = new Pipe(); var pipeBottom = new Pipe(); var gap = 400; var pipeHeight = 800; var pipeY = Math.random() * (2732 - gap - pipeHeight * 2) + pipeHeight; pipeTop.x = 2048; pipeTop.y = pipeY - gap / 2 - pipeHeight; pipeBottom.x = 2048; pipeBottom.y = pipeY + gap / 2 + pipeHeight; pipes.push(pipeTop); pipes.push(pipeBottom); game.addChild(pipeTop); game.addChild(pipeBottom); }, 1500); } game.down = function (x, y, obj) { bird.flap(); }; game.update = function () { bird.update(); pipes.forEach(function (pipe) { pipe.update(); if (pipe.intersects(bird)) { LK.showGameOver(); } }); pipes = pipes.filter(function (pipe) { return pipe.parent; }); if (pipes.length > 0 && pipes[0].x < bird.x && !pipes[0].scored) { score++; scoreTxt.setText(score); pipes[0].scored = true; } }; scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); resetGame();
/****
* Classes
****/
// Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.gravity = 0.5;
self.lift = -10;
self.velocity = 8;
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
if (self.y > 2732 - birdGraphics.height / 2) {
self.y = 2732 - birdGraphics.height / 2;
self.velocity = 0;
}
if (self.y < birdGraphics.height / 2) {
self.y = birdGraphics.height / 2;
self.velocity = 0;
}
};
self.flap = function () {
self.velocity = self.lift;
};
});
//<Assets used in the game will automatically appear here>
// Pipe class
var Pipe = Container.expand(function () {
var self = Container.call(this);
var pipeGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -pipeGraphics.width) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Init game with sky blue background
});
/****
* Game Code
****/
var bird;
var pipes = [];
var score = 0;
var scoreTxt;
var pipeInterval;
function resetGame() {
bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
pipes.forEach(function (pipe) {
pipe.destroy();
});
pipes = [];
score = 0;
scoreTxt.setText(score);
if (pipeInterval) {
LK.clearInterval(pipeInterval);
}
pipeInterval = LK.setInterval(function () {
var pipeTop = new Pipe();
var pipeBottom = new Pipe();
var gap = 400;
var pipeHeight = 800;
var pipeY = Math.random() * (2732 - gap - pipeHeight * 2) + pipeHeight;
pipeTop.x = 2048;
pipeTop.y = pipeY - gap / 2 - pipeHeight;
pipeBottom.x = 2048;
pipeBottom.y = pipeY + gap / 2 + pipeHeight;
pipes.push(pipeTop);
pipes.push(pipeBottom);
game.addChild(pipeTop);
game.addChild(pipeBottom);
}, 1500);
}
game.down = function (x, y, obj) {
bird.flap();
};
game.update = function () {
bird.update();
pipes.forEach(function (pipe) {
pipe.update();
if (pipe.intersects(bird)) {
LK.showGameOver();
}
});
pipes = pipes.filter(function (pipe) {
return pipe.parent;
});
if (pipes.length > 0 && pipes[0].x < bird.x && !pipes[0].scored) {
score++;
scoreTxt.setText(score);
pipes[0].scored = true;
}
};
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
resetGame();