/****
* Initialize Game
****/
/**** Initialize Game ****/
var game = new LK.Game({
backgroundColor: 0x70c5ce
});
/****
* Game Code
****/
/**** Background ****/
var bg = LK.getAsset('background', {
x: 0,
y: 0
});
game.addChild(bg);
var ground = LK.getAsset('ground', {
x: 0,
y: 2532
});
game.addChild(ground);
var groundDirt = LK.getAsset('groundDirt', {
x: 0,
y: 2672
});
game.addChild(groundDirt);
/**** Bird ****/
var bird = new Container();
bird.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
bird.x = 400;
bird.y = 800;
game.addChild(bird);
var velocity = 0;
var gravity = 0.8;
var jump = -15;
var maxFall = 20;
/**** Pipes ****/
var pipes = [];
var pipeGap = 400;
var pipeSpeed = 10;
/**** Score ****/
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0);
scoreTxt.x = 1950;
scoreTxt.y = 50;
LK.gui.top.addChild(scoreTxt);
/**** Game Over Text ****/
var gameOverTxt = new Text2('GAME OVER', {
size: 120,
fill: 0xff0000
});
gameOverTxt.anchor.set(0.5);
gameOverTxt.x = 1024;
gameOverTxt.y = 900;
gameOverTxt.visible = false;
LK.gui.center.addChild(gameOverTxt);
/**** Restart Button ****/
var restartButton = LK.getAsset('restartButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1300
});
restartButton.visible = false;
restartButton.interactive = true;
restartButton.on('pointerdown', resetGame);
LK.gui.center.addChild(restartButton);
/**** Spawn Pipes ****/
function spawnPipes() {
var gapY = 500 + Math.random() * 1000;
var topPipe = new Container();
topPipe.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
topPipe.x = 2200;
topPipe.y = gapY - pipeGap / 2;
var bottomPipe = new Container();
bottomPipe.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
bottomPipe.x = 2200;
bottomPipe.y = gapY + pipeGap / 2;
topPipe.passed = false;
game.addChild(topPipe);
game.addChild(bottomPipe);
pipes.push({
top: topPipe,
bottom: bottomPipe
});
}
/**** Collision ****/
function hitTest(bird, pipe) {
return bird.x + 50 > pipe.x - pipe.width / 2 && bird.x - 50 < pipe.x + pipe.width / 2 && bird.y + 40 > pipe.y - pipe.height * pipe.anchorY && bird.y - 40 < pipe.y + pipe.height * (1 - pipe.anchorY);
}
/**** Controls ****/
game.down = function () {
if (gameOver) {
return;
}
velocity = jump;
};
/**** Game Loop ****/
var frame = 0;
var gameOver = false;
game.update = function () {
if (gameOver) {
return;
}
// Gravity
velocity += gravity;
if (velocity > maxFall) {
velocity = maxFall;
}
bird.y += velocity;
// Rotation
bird.rotation = velocity * 0.05;
// Ground & ceiling
if (bird.y > 2600) {
bird.y = 2600;
endGame();
return;
}
if (bird.y < 0) {
bird.y = 0;
velocity = 0;
}
// Spawn pipes
frame++;
if (frame % 90 === 0) {
spawnPipes();
}
// Difficulty scaling
if (score > 5) {
pipeSpeed = 12;
}
if (score > 10) {
pipeSpeed = 14;
}
if (score > 20) {
pipeSpeed = 16;
}
// Move pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var p = pipes[i];
p.top.x -= pipeSpeed;
p.bottom.x -= pipeSpeed;
// Score
if (!p.top.passed && p.top.x < bird.x) {
p.top.passed = true;
score++;
scoreTxt.setText(score);
LK.getSound('score').play();
}
// Collision
if (hitTest(bird, p.top) || hitTest(bird, p.bottom)) {
endGame();
return;
}
// Remove pipes
if (p.top.x < -200) {
p.top.destroy();
p.bottom.destroy();
pipes.splice(i, 1);
}
}
};
/**** End Game ****/
function endGame() {
if (gameOver) {
return;
}
gameOver = true;
velocity = 0;
LK.getSound('hit').play();
restartButton.visible = true;
gameOverTxt.visible = true;
}
/**** Reset Game ****/
function resetGame() {
gameOver = false;
score = 0;
scoreTxt.setText(score);
velocity = 0;
bird.y = 800;
bird.rotation = 0;
pipeSpeed = 10;
// Clear pipes
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].top.destroy();
pipes[i].bottom.destroy();
}
pipes = [];
frame = 0;
restartButton.visible = false;
gameOverTxt.visible = false;
} /****
* Initialize Game
****/
/**** Initialize Game ****/
var game = new LK.Game({
backgroundColor: 0x70c5ce
});
/****
* Game Code
****/
/**** Background ****/
var bg = LK.getAsset('background', {
x: 0,
y: 0
});
game.addChild(bg);
var ground = LK.getAsset('ground', {
x: 0,
y: 2532
});
game.addChild(ground);
var groundDirt = LK.getAsset('groundDirt', {
x: 0,
y: 2672
});
game.addChild(groundDirt);
/**** Bird ****/
var bird = new Container();
bird.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
bird.x = 400;
bird.y = 800;
game.addChild(bird);
var velocity = 0;
var gravity = 0.8;
var jump = -15;
var maxFall = 20;
/**** Pipes ****/
var pipes = [];
var pipeGap = 400;
var pipeSpeed = 10;
/**** Score ****/
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0);
scoreTxt.x = 1950;
scoreTxt.y = 50;
LK.gui.top.addChild(scoreTxt);
/**** Game Over Text ****/
var gameOverTxt = new Text2('GAME OVER', {
size: 120,
fill: 0xff0000
});
gameOverTxt.anchor.set(0.5);
gameOverTxt.x = 1024;
gameOverTxt.y = 900;
gameOverTxt.visible = false;
LK.gui.center.addChild(gameOverTxt);
/**** Restart Button ****/
var restartButton = LK.getAsset('restartButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1300
});
restartButton.visible = false;
restartButton.interactive = true;
restartButton.on('pointerdown', resetGame);
LK.gui.center.addChild(restartButton);
/**** Spawn Pipes ****/
function spawnPipes() {
var gapY = 500 + Math.random() * 1000;
var topPipe = new Container();
topPipe.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
topPipe.x = 2200;
topPipe.y = gapY - pipeGap / 2;
var bottomPipe = new Container();
bottomPipe.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
bottomPipe.x = 2200;
bottomPipe.y = gapY + pipeGap / 2;
topPipe.passed = false;
game.addChild(topPipe);
game.addChild(bottomPipe);
pipes.push({
top: topPipe,
bottom: bottomPipe
});
}
/**** Collision ****/
function hitTest(bird, pipe) {
return bird.x + 50 > pipe.x - pipe.width / 2 && bird.x - 50 < pipe.x + pipe.width / 2 && bird.y + 40 > pipe.y - pipe.height * pipe.anchorY && bird.y - 40 < pipe.y + pipe.height * (1 - pipe.anchorY);
}
/**** Controls ****/
game.down = function () {
if (gameOver) {
return;
}
velocity = jump;
};
/**** Game Loop ****/
var frame = 0;
var gameOver = false;
game.update = function () {
if (gameOver) {
return;
}
// Gravity
velocity += gravity;
if (velocity > maxFall) {
velocity = maxFall;
}
bird.y += velocity;
// Rotation
bird.rotation = velocity * 0.05;
// Ground & ceiling
if (bird.y > 2600) {
bird.y = 2600;
endGame();
return;
}
if (bird.y < 0) {
bird.y = 0;
velocity = 0;
}
// Spawn pipes
frame++;
if (frame % 90 === 0) {
spawnPipes();
}
// Difficulty scaling
if (score > 5) {
pipeSpeed = 12;
}
if (score > 10) {
pipeSpeed = 14;
}
if (score > 20) {
pipeSpeed = 16;
}
// Move pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var p = pipes[i];
p.top.x -= pipeSpeed;
p.bottom.x -= pipeSpeed;
// Score
if (!p.top.passed && p.top.x < bird.x) {
p.top.passed = true;
score++;
scoreTxt.setText(score);
LK.getSound('score').play();
}
// Collision
if (hitTest(bird, p.top) || hitTest(bird, p.bottom)) {
endGame();
return;
}
// Remove pipes
if (p.top.x < -200) {
p.top.destroy();
p.bottom.destroy();
pipes.splice(i, 1);
}
}
};
/**** End Game ****/
function endGame() {
if (gameOver) {
return;
}
gameOver = true;
velocity = 0;
LK.getSound('hit').play();
restartButton.visible = true;
gameOverTxt.visible = true;
}
/**** Reset Game ****/
function resetGame() {
gameOver = false;
score = 0;
scoreTxt.setText(score);
velocity = 0;
bird.y = 800;
bird.rotation = 0;
pipeSpeed = 10;
// Clear pipes
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].top.destroy();
pipes[i].bottom.destroy();
}
pipes = [];
frame = 0;
restartButton.visible = false;
gameOverTxt.visible = false;
}