/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = 0;
self.gravity = 0.8;
self.flapPower = -18;
self.maxFallSpeed = 15;
self.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add flap animation
tween.stop(birdGraphics, {
rotation: true
});
birdGraphics.rotation = -0.3;
tween(birdGraphics, {
rotation: 0.3
}, {
duration: 300,
easing: tween.easeOut
});
};
self.update = function () {
// Apply gravity
self.velocity += self.gravity;
// Cap fall speed
if (self.velocity > self.maxFallSpeed) {
self.velocity = self.maxFallSpeed;
}
// Update position
self.y += self.velocity;
// Check boundaries
if (self.y < 0) {
self.y = 0;
self.velocity = 0;
}
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -4;
self.gapSize = 400;
self.scored = false;
// Create top pipe
self.topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
// Create bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.setGapPosition = function (gapY) {
self.topPipe.y = gapY - self.gapSize / 2;
self.bottomPipe.y = gapY + self.gapSize / 2;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var bird;
var pipes = [];
var ground;
var gameStarted = false;
var gameSpeed = 4;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 90; // frames between pipe spawns
// Create score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Create high score display
var highScoreTxt = new Text2('BEST: ' + storage.highScore, {
size: 50,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 200;
// Create tap to start text
var startTxt = new Text2('TAP TO START', {
size: 60,
fill: 0xFFFFFF
});
startTxt.anchor.set(0.5, 0.5);
game.addChild(startTxt);
startTxt.x = 2048 / 2;
startTxt.y = 2732 / 2 - 200;
// Create bird
bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 2732 / 2;
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 1
}));
ground.x = 0;
ground.y = 2732;
function spawnPipe() {
var pipe = new Pipe();
pipe.x = 2048 + 60; // Start off-screen right
// Random gap position (avoid too high or too low)
var minGapY = 200;
var maxGapY = 2732 - 200 - 150; // Account for ground height
var gapY = minGapY + Math.random() * (maxGapY - minGapY);
pipe.setGapPosition(gapY);
pipes.push(pipe);
game.addChild(pipe);
}
function checkCollisions() {
// Check ground collision
if (bird.y >= ground.y - ground.height / 2 - bird.height / 2) {
return true;
}
// Check ceiling collision
if (bird.y <= bird.height / 2) {
return true;
}
// Check pipe collisions
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
// Check if bird is horizontally aligned with pipe
if (bird.x + bird.width / 2 > pipe.x - pipe.topPipe.width / 2 && bird.x - bird.width / 2 < pipe.x + pipe.topPipe.width / 2) {
// Check top pipe collision
if (bird.y - bird.height / 2 < pipe.topPipe.y) {
return true;
}
// Check bottom pipe collision
if (bird.y + bird.height / 2 > pipe.bottomPipe.y) {
return true;
}
}
}
return false;
}
function updateScore() {
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
if (!pipe.scored && pipe.x < bird.x) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
// Check and update high score
if (LK.getScore() > storage.highScore) {
storage.highScore = LK.getScore();
highScoreTxt.setText('BEST: ' + storage.highScore);
}
// Increase difficulty slightly
if (LK.getScore() % 5 === 0 && pipeSpawnInterval > 60) {
pipeSpawnInterval -= 2;
}
}
}
}
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
startTxt.visible = false;
}
if (gameStarted) {
bird.flap();
}
};
game.update = function () {
if (!gameStarted) {
return;
}
// Update bird
bird.update();
// Check collisions
if (checkCollisions()) {
LK.showGameOver();
return;
}
// Spawn pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
spawnPipe();
pipeSpawnTimer = 0;
}
// Update pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Remove pipes that are off-screen
if (pipe.x < -120) {
pipe.destroy();
pipes.splice(i, 1);
}
}
// Update score
updateScore();
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = 0;
self.gravity = 0.8;
self.flapPower = -18;
self.maxFallSpeed = 15;
self.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add flap animation
tween.stop(birdGraphics, {
rotation: true
});
birdGraphics.rotation = -0.3;
tween(birdGraphics, {
rotation: 0.3
}, {
duration: 300,
easing: tween.easeOut
});
};
self.update = function () {
// Apply gravity
self.velocity += self.gravity;
// Cap fall speed
if (self.velocity > self.maxFallSpeed) {
self.velocity = self.maxFallSpeed;
}
// Update position
self.y += self.velocity;
// Check boundaries
if (self.y < 0) {
self.y = 0;
self.velocity = 0;
}
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -4;
self.gapSize = 400;
self.scored = false;
// Create top pipe
self.topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
// Create bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.setGapPosition = function (gapY) {
self.topPipe.y = gapY - self.gapSize / 2;
self.bottomPipe.y = gapY + self.gapSize / 2;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var bird;
var pipes = [];
var ground;
var gameStarted = false;
var gameSpeed = 4;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 90; // frames between pipe spawns
// Create score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Create high score display
var highScoreTxt = new Text2('BEST: ' + storage.highScore, {
size: 50,
fill: 0xFFFFFF
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 200;
// Create tap to start text
var startTxt = new Text2('TAP TO START', {
size: 60,
fill: 0xFFFFFF
});
startTxt.anchor.set(0.5, 0.5);
game.addChild(startTxt);
startTxt.x = 2048 / 2;
startTxt.y = 2732 / 2 - 200;
// Create bird
bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 2732 / 2;
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 1
}));
ground.x = 0;
ground.y = 2732;
function spawnPipe() {
var pipe = new Pipe();
pipe.x = 2048 + 60; // Start off-screen right
// Random gap position (avoid too high or too low)
var minGapY = 200;
var maxGapY = 2732 - 200 - 150; // Account for ground height
var gapY = minGapY + Math.random() * (maxGapY - minGapY);
pipe.setGapPosition(gapY);
pipes.push(pipe);
game.addChild(pipe);
}
function checkCollisions() {
// Check ground collision
if (bird.y >= ground.y - ground.height / 2 - bird.height / 2) {
return true;
}
// Check ceiling collision
if (bird.y <= bird.height / 2) {
return true;
}
// Check pipe collisions
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
// Check if bird is horizontally aligned with pipe
if (bird.x + bird.width / 2 > pipe.x - pipe.topPipe.width / 2 && bird.x - bird.width / 2 < pipe.x + pipe.topPipe.width / 2) {
// Check top pipe collision
if (bird.y - bird.height / 2 < pipe.topPipe.y) {
return true;
}
// Check bottom pipe collision
if (bird.y + bird.height / 2 > pipe.bottomPipe.y) {
return true;
}
}
}
return false;
}
function updateScore() {
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
if (!pipe.scored && pipe.x < bird.x) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
// Check and update high score
if (LK.getScore() > storage.highScore) {
storage.highScore = LK.getScore();
highScoreTxt.setText('BEST: ' + storage.highScore);
}
// Increase difficulty slightly
if (LK.getScore() % 5 === 0 && pipeSpawnInterval > 60) {
pipeSpawnInterval -= 2;
}
}
}
}
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
startTxt.visible = false;
}
if (gameStarted) {
bird.flap();
}
};
game.update = function () {
if (!gameStarted) {
return;
}
// Update bird
bird.update();
// Check collisions
if (checkCollisions()) {
LK.showGameOver();
return;
}
// Spawn pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
spawnPipe();
pipeSpawnTimer = 0;
}
// Update pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Remove pipes that are off-screen
if (pipe.x < -120) {
pipe.destroy();
pipes.splice(i, 1);
}
}
// Update score
updateScore();
};