/**** * Classes ****/ //<Write imports for supported plugins here> 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 = 0; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; }; self.flap = function () { self.velocity = self.lift; LK.getSound('flap').play(); }; }); 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; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb // Init game with sky blue background }); /**** * Game Code ****/ //<Assets used in the game will automatically appear here> var bird = game.addChild(new Bird()); bird.x = 300; bird.y = 1366; // Start in the middle of the screen vertically var pipes = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); function spawnPipe() { var pipeGap = 400; var pipeHeight = Math.random() * 800 + 400; var topPipe = new Pipe(); topPipe.x = 2048; topPipe.y = pipeHeight / 2 - pipeGap / 2; pipes.push(topPipe); game.addChild(topPipe); var bottomPipe = new Pipe(); bottomPipe.x = 2048; bottomPipe.y = pipeHeight / 2 + pipeGap / 2 + 800; pipes.push(bottomPipe); game.addChild(bottomPipe); } game.down = function (x, y, obj) { bird.flap(); }; game.update = function () { bird.update(); if (LK.ticks % 90 === 0) { spawnPipe(); } for (var i = pipes.length - 1; i >= 0; i--) { var pipe = pipes[i]; pipe.update(); if (pipe.x < -100) { pipe.destroy(); pipes.splice(i, 1); continue; } if (bird.intersects(pipe)) { LK.getSound('hit').play(); LK.showGameOver(); return; } if (!pipe.scored && pipe.x < bird.x) { pipe.scored = true; score++; LK.getSound('score').play(); scoreTxt.setText(score); } } if (bird.y > 2732 || bird.y < 0) { LK.getSound('hit').play(); LK.showGameOver(); } };
/****
* Classes
****/
//<Write imports for supported plugins here>
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 = 0;
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
};
self.flap = function () {
self.velocity = self.lift;
LK.getSound('flap').play();
};
});
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;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb // Init game with sky blue background
});
/****
* Game Code
****/
//<Assets used in the game will automatically appear here>
var bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366; // Start in the middle of the screen vertically
var pipes = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function spawnPipe() {
var pipeGap = 400;
var pipeHeight = Math.random() * 800 + 400;
var topPipe = new Pipe();
topPipe.x = 2048;
topPipe.y = pipeHeight / 2 - pipeGap / 2;
pipes.push(topPipe);
game.addChild(topPipe);
var bottomPipe = new Pipe();
bottomPipe.x = 2048;
bottomPipe.y = pipeHeight / 2 + pipeGap / 2 + 800;
pipes.push(bottomPipe);
game.addChild(bottomPipe);
}
game.down = function (x, y, obj) {
bird.flap();
};
game.update = function () {
bird.update();
if (LK.ticks % 90 === 0) {
spawnPipe();
}
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
if (pipe.x < -100) {
pipe.destroy();
pipes.splice(i, 1);
continue;
}
if (bird.intersects(pipe)) {
LK.getSound('hit').play();
LK.showGameOver();
return;
}
if (!pipe.scored && pipe.x < bird.x) {
pipe.scored = true;
score++;
LK.getSound('score').play();
scoreTxt.setText(score);
}
}
if (bird.y > 2732 || bird.y < 0) {
LK.getSound('hit').play();
LK.showGameOver();
}
};