/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityY = 0;
self.gravity = 0.6;
self.flapPower = -15;
self.isAlive = true;
self.flap = function () {
self.velocityY = self.flapPower;
LK.getSound('flap').play();
};
self.update = function () {
if (!self.isAlive) return;
self.velocityY += self.gravity;
self.y += self.velocityY;
// Rotate bird based on velocity
if (self.velocityY > 0) {
birdGraphics.rotation = Math.min(self.velocityY * 0.05, 0.5);
} else {
birdGraphics.rotation = Math.max(self.velocityY * 0.05, -0.5);
}
// Ceiling collision
if (self.y < 50) {
self.y = 50;
self.velocityY = 0;
self.isAlive = false;
}
// Ground collision
if (self.y > 2582) {
self.y = 2582;
self.velocityY = 0;
self.isAlive = false;
}
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -8;
self.gapHeight = 300;
self.minGapY = 200;
self.maxGapY = 1800;
self.scored = false;
// Generate random gap position
var gapY = Math.random() * (self.maxGapY - self.minGapY) + self.minGapY;
// Top pipe
var topPipe = self.attachAsset('pipeTop', {
anchorX: 0.5,
anchorY: 1
});
topPipe.x = 75;
topPipe.y = gapY;
// Bottom pipe
var bottomPipe = self.attachAsset('pipeBottom', {
anchorX: 0.5,
anchorY: 0
});
bottomPipe.x = 75;
bottomPipe.y = gapY + self.gapHeight;
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366;
var pipes = [];
var score = 0;
var gameOver = false;
var pipeSpawnCounter = 0;
var pipeSpawnInterval = 120;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var ground = game.addChild(LK.getAsset('ground', {
anchorX: 0.5,
anchorY: 0.5
}));
ground.x = 1024;
ground.y = 2650;
// Game input handler
game.down = function (x, y, obj) {
if (!gameOver && bird.isAlive) {
bird.flap();
}
};
game.update = function () {
if (!gameOver) {
bird.update();
// Spawn pipes
pipeSpawnCounter++;
if (pipeSpawnCounter >= pipeSpawnInterval) {
var newPipe = new Pipe();
newPipe.x = 2200;
newPipe.y = 0;
pipes.push(newPipe);
game.addChild(newPipe);
pipeSpawnCounter = 0;
}
// Update and check pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Remove off-screen pipes
if (pipe.x < -200) {
pipe.destroy();
pipes.splice(i, 1);
continue;
}
// Check collision with bird
var topPipe = pipe.children[0];
var bottomPipe = pipe.children[1];
if (bird.intersects(topPipe) || bird.intersects(bottomPipe)) {
if (bird.isAlive) {
bird.isAlive = false;
LK.getSound('hit').play();
LK.effects.flashScreen(0xFF0000, 500);
gameOver = true;
LK.setTimeout(function () {
LK.showGameOver();
}, 600);
}
}
// Award score when pipe is passed
if (!pipe.scored && pipe.x < bird.x && pipe.x + 150 > bird.x - 40) {
pipe.scored = true;
score++;
LK.setScore(score);
scoreTxt.setText(score.toString());
LK.getSound('score').play();
// Win condition at score 50
if (score >= 50) {
gameOver = true;
LK.setTimeout(function () {
LK.showYouWin();
}, 300);
}
}
}
// Ground collision
if (bird.intersects(ground)) {
if (bird.isAlive) {
bird.isAlive = false;
LK.getSound('hit').play();
LK.effects.flashScreen(0xFF0000, 500);
gameOver = true;
LK.setTimeout(function () {
LK.showGameOver();
}, 600);
}
}
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityY = 0;
self.gravity = 0.6;
self.flapPower = -15;
self.isAlive = true;
self.flap = function () {
self.velocityY = self.flapPower;
LK.getSound('flap').play();
};
self.update = function () {
if (!self.isAlive) return;
self.velocityY += self.gravity;
self.y += self.velocityY;
// Rotate bird based on velocity
if (self.velocityY > 0) {
birdGraphics.rotation = Math.min(self.velocityY * 0.05, 0.5);
} else {
birdGraphics.rotation = Math.max(self.velocityY * 0.05, -0.5);
}
// Ceiling collision
if (self.y < 50) {
self.y = 50;
self.velocityY = 0;
self.isAlive = false;
}
// Ground collision
if (self.y > 2582) {
self.y = 2582;
self.velocityY = 0;
self.isAlive = false;
}
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -8;
self.gapHeight = 300;
self.minGapY = 200;
self.maxGapY = 1800;
self.scored = false;
// Generate random gap position
var gapY = Math.random() * (self.maxGapY - self.minGapY) + self.minGapY;
// Top pipe
var topPipe = self.attachAsset('pipeTop', {
anchorX: 0.5,
anchorY: 1
});
topPipe.x = 75;
topPipe.y = gapY;
// Bottom pipe
var bottomPipe = self.attachAsset('pipeBottom', {
anchorX: 0.5,
anchorY: 0
});
bottomPipe.x = 75;
bottomPipe.y = gapY + self.gapHeight;
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366;
var pipes = [];
var score = 0;
var gameOver = false;
var pipeSpawnCounter = 0;
var pipeSpawnInterval = 120;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var ground = game.addChild(LK.getAsset('ground', {
anchorX: 0.5,
anchorY: 0.5
}));
ground.x = 1024;
ground.y = 2650;
// Game input handler
game.down = function (x, y, obj) {
if (!gameOver && bird.isAlive) {
bird.flap();
}
};
game.update = function () {
if (!gameOver) {
bird.update();
// Spawn pipes
pipeSpawnCounter++;
if (pipeSpawnCounter >= pipeSpawnInterval) {
var newPipe = new Pipe();
newPipe.x = 2200;
newPipe.y = 0;
pipes.push(newPipe);
game.addChild(newPipe);
pipeSpawnCounter = 0;
}
// Update and check pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Remove off-screen pipes
if (pipe.x < -200) {
pipe.destroy();
pipes.splice(i, 1);
continue;
}
// Check collision with bird
var topPipe = pipe.children[0];
var bottomPipe = pipe.children[1];
if (bird.intersects(topPipe) || bird.intersects(bottomPipe)) {
if (bird.isAlive) {
bird.isAlive = false;
LK.getSound('hit').play();
LK.effects.flashScreen(0xFF0000, 500);
gameOver = true;
LK.setTimeout(function () {
LK.showGameOver();
}, 600);
}
}
// Award score when pipe is passed
if (!pipe.scored && pipe.x < bird.x && pipe.x + 150 > bird.x - 40) {
pipe.scored = true;
score++;
LK.setScore(score);
scoreTxt.setText(score.toString());
LK.getSound('score').play();
// Win condition at score 50
if (score >= 50) {
gameOver = true;
LK.setTimeout(function () {
LK.showYouWin();
}, 300);
}
}
}
// Ground collision
if (bird.intersects(ground)) {
if (bird.isAlive) {
bird.isAlive = false;
LK.getSound('hit').play();
LK.effects.flashScreen(0xFF0000, 500);
gameOver = true;
LK.setTimeout(function () {
LK.showGameOver();
}, 600);
}
}
}
};