/**** * Classes ****/ var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = LK.getAsset('bird', {}); birdGraphics.anchor.set(0.5, 0.5); self.addChild(birdGraphics); self.velocity = 0; self.gravity = 0.5; self.lift = -15; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; if (self.y < 0) { self.y = 0; self.velocity = 0; } self.rotation += (Math.atan(self.velocity / 10) - self.rotation) * 0.1; }; self.flap = function () { self.velocity += self.lift; }; }); var Pipe = Container.expand(function () { var self = Container.call(this); self.passed = false; var topPipeGraphics = LK.getAsset('pipe', {}); topPipeGraphics.anchor.set(0.5, 1); self.addChild(topPipeGraphics); var bottomPipeGraphics = LK.getAsset('pipe', {}); bottomPipeGraphics.anchor.set(0.5, 0); self.addChild(bottomPipeGraphics); self.setGap = function (gapSize, gapPosition) { topPipeGraphics.y = gapPosition - gapSize / 2; bottomPipeGraphics.y = gapPosition + gapSize / 2; }; self.speed = -10; self.update = function () { self.x += self.speed; }; }); var Ground = Container.expand(function () { var self = Container.call(this); var groundGraphics = LK.getAsset('ground', {}); groundGraphics.anchor.set(0, 0); self.addChild(groundGraphics); self.speed = -2; self.update = function () { self.x += self.speed; if (self.x <= -self.width / 2) { self.x = 0; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ game.setBackgroundColor(0x008080); var bird = game.addChild(new Bird()); bird.x = 2048 / 4; bird.y = 2732 / 2; var ground = game.addChild(new Ground()); ground.y = 2732; var pipes = []; function spawnPipe() { var pipe = new Pipe(); pipe.x = game.width; var gapPosition = Math.random() * (2732 - 400) + 200; pipe.setGap(1200, gapPosition); pipes.push(pipe); game.addChildAt(pipe, 0); } function updatePipes() { for (var i = 0; i < pipes.length; i++) { pipes[i].update(); pipes[i].speed = -10 - score * 0.5; if (pipes[i].x < -game.width) { game.removeChild(pipes[i]); pipes.splice(i, 1); i--; } } } function checkCollision() { for (var i = 0; i < pipes.length; i++) { if (bird.intersects(pipes[i])) { return true; } } return false; } function restartGame() { bird.y = 2732 / 2; bird.velocity = 0; for (var i = 0; i < pipes.length; i++) { game.removeChild(pipes[i]); } pipes = []; score = 0; scoreText.setText(score); } var spawnPipeInterval = LK.setInterval(spawnPipe, 2000); var score = 0; var scoreText = new Text2('0', { size: 150, font: "'GillSans-Bold',Impact,'Arial Black',Tahoma", fill: "#ffffff" }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); LK.on('tick', function () { bird.update(); ground.update(); updatePipes(); if (checkCollision() || bird.y + bird.height / 2 >= ground.y) { LK.showGameOver(restartGame); } else { for (var i = 0; i < pipes.length; i++) { if (pipes[i].x + pipes[i].width / 2 < bird.x && !pipes[i].passed) { pipes[i].passed = true; score++; LK.setScore(score); scoreText.setText(score); } } } }); game.on('down', function () { bird.flap(); });
/****
* Classes
****/
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = LK.getAsset('bird', {});
birdGraphics.anchor.set(0.5, 0.5);
self.addChild(birdGraphics);
self.velocity = 0;
self.gravity = 0.5;
self.lift = -15;
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
if (self.y < 0) {
self.y = 0;
self.velocity = 0;
}
self.rotation += (Math.atan(self.velocity / 10) - self.rotation) * 0.1;
};
self.flap = function () {
self.velocity += self.lift;
};
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.passed = false;
var topPipeGraphics = LK.getAsset('pipe', {});
topPipeGraphics.anchor.set(0.5, 1);
self.addChild(topPipeGraphics);
var bottomPipeGraphics = LK.getAsset('pipe', {});
bottomPipeGraphics.anchor.set(0.5, 0);
self.addChild(bottomPipeGraphics);
self.setGap = function (gapSize, gapPosition) {
topPipeGraphics.y = gapPosition - gapSize / 2;
bottomPipeGraphics.y = gapPosition + gapSize / 2;
};
self.speed = -10;
self.update = function () {
self.x += self.speed;
};
});
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = LK.getAsset('ground', {});
groundGraphics.anchor.set(0, 0);
self.addChild(groundGraphics);
self.speed = -2;
self.update = function () {
self.x += self.speed;
if (self.x <= -self.width / 2) {
self.x = 0;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
game.setBackgroundColor(0x008080);
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
var ground = game.addChild(new Ground());
ground.y = 2732;
var pipes = [];
function spawnPipe() {
var pipe = new Pipe();
pipe.x = game.width;
var gapPosition = Math.random() * (2732 - 400) + 200;
pipe.setGap(1200, gapPosition);
pipes.push(pipe);
game.addChildAt(pipe, 0);
}
function updatePipes() {
for (var i = 0; i < pipes.length; i++) {
pipes[i].update();
pipes[i].speed = -10 - score * 0.5;
if (pipes[i].x < -game.width) {
game.removeChild(pipes[i]);
pipes.splice(i, 1);
i--;
}
}
}
function checkCollision() {
for (var i = 0; i < pipes.length; i++) {
if (bird.intersects(pipes[i])) {
return true;
}
}
return false;
}
function restartGame() {
bird.y = 2732 / 2;
bird.velocity = 0;
for (var i = 0; i < pipes.length; i++) {
game.removeChild(pipes[i]);
}
pipes = [];
score = 0;
scoreText.setText(score);
}
var spawnPipeInterval = LK.setInterval(spawnPipe, 2000);
var score = 0;
var scoreText = new Text2('0', {
size: 150,
font: "'GillSans-Bold',Impact,'Arial Black',Tahoma",
fill: "#ffffff"
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
LK.on('tick', function () {
bird.update();
ground.update();
updatePipes();
if (checkCollision() || bird.y + bird.height / 2 >= ground.y) {
LK.showGameOver(restartGame);
} else {
for (var i = 0; i < pipes.length; i++) {
if (pipes[i].x + pipes[i].width / 2 < bird.x && !pipes[i].passed) {
pipes[i].passed = true;
score++;
LK.setScore(score);
scoreText.setText(score);
}
}
}
});
game.on('down', function () {
bird.flap();
});