/**** * 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.velocity = 0; self.gravity = 0.8; self.flapPower = -12; self.flipped = false; self.flap = function () { self.velocity = self.flapPower; self.flipped = !self.flipped; if (self.flipped) { birdGraphics.rotation = Math.PI; } else { birdGraphics.rotation = 0; } LK.getSound('flap').play(); }; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; // Rotate bird based on velocity var targetRotation = self.flipped ? Math.PI : 0; if (self.velocity > 0) { targetRotation += self.flipped ? -0.5 : 0.5; } else { targetRotation += self.flipped ? 0.3 : -0.3; } birdGraphics.rotation = targetRotation; // Check boundaries if (self.y < -30 || self.y > 2732 + 30) { gameOver = true; } }; return self; }); var Pipe = Container.expand(function (isTop) { var self = Container.call(this); var pipeGraphics = self.attachAsset('pipe', { anchorX: 0.5, anchorY: isTop ? 1 : 0 }); self.speed = -4; self.scored = false; self.isTop = isTop; self.update = function () { self.x += self.speed; if (self.x < -100) { self.destroy(); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ var bird; var pipes = []; var gameOver = false; var gameStarted = false; var pipeTimer = 0; var gapSize = 300; var pipeSpacing = 400; // UI Elements var scoreTxt = new Text2('0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var instructionTxt = new Text2('TAP TO START', { size: 60, fill: 0xFFFFFF }); instructionTxt.anchor.set(0.5, 0.5); game.addChild(instructionTxt); instructionTxt.x = 1024; instructionTxt.y = 1366; // Initialize bird bird = new Bird(); bird.x = 400; bird.y = 1366; game.addChild(bird); function createPipePair() { var gapCenter = 400 + Math.random() * (2732 - 800); var topHeight = gapCenter - gapSize / 2; var bottomY = gapCenter + gapSize / 2; // Top pipe var topPipe = new Pipe(true); topPipe.x = 2048 + 100; topPipe.y = topHeight; pipes.push(topPipe); game.addChild(topPipe); // Bottom pipe var bottomPipe = new Pipe(false); bottomPipe.x = 2048 + 100; bottomPipe.y = bottomY; pipes.push(bottomPipe); game.addChild(bottomPipe); } function resetGame() { // Remove all pipes for (var i = pipes.length - 1; i >= 0; i--) { pipes[i].destroy(); } pipes = []; // Reset bird bird.x = 400; bird.y = 1366; bird.velocity = 0; bird.flipped = false; bird.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }).rotation = 0; // Reset game state gameOver = false; gameStarted = false; pipeTimer = 0; LK.setScore(0); scoreTxt.setText('0'); // Show instruction instructionTxt.alpha = 1; } game.down = function (x, y, obj) { if (gameOver) { LK.showGameOver(); return; } if (!gameStarted) { gameStarted = true; instructionTxt.alpha = 0; } bird.flap(); }; game.update = function () { if (gameOver) { return; } if (!gameStarted) { return; } // Update bird bird.update(); // Create pipes pipeTimer++; if (pipeTimer >= pipeSpacing / 4) { createPipePair(); pipeTimer = 0; // Increase difficulty slightly if (gapSize > 200) { gapSize -= 0.5; } } // Update pipes and check collisions for (var i = pipes.length - 1; i >= 0; i--) { var pipe = pipes[i]; pipe.update(); // Check if pipe is destroyed if (pipe.parent === null) { pipes.splice(i, 1); continue; } // Check collision if (bird.intersects(pipe)) { LK.getSound('hit').play(); gameOver = true; LK.showGameOver(); return; } // Check scoring (only for top pipes to avoid double scoring) if (pipe.isTop && !pipe.scored && pipe.x < bird.x - 30) { pipe.scored = true; LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore().toString()); LK.getSound('score').play(); } } };
/****
* 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.velocity = 0;
self.gravity = 0.8;
self.flapPower = -12;
self.flipped = false;
self.flap = function () {
self.velocity = self.flapPower;
self.flipped = !self.flipped;
if (self.flipped) {
birdGraphics.rotation = Math.PI;
} else {
birdGraphics.rotation = 0;
}
LK.getSound('flap').play();
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
// Rotate bird based on velocity
var targetRotation = self.flipped ? Math.PI : 0;
if (self.velocity > 0) {
targetRotation += self.flipped ? -0.5 : 0.5;
} else {
targetRotation += self.flipped ? 0.3 : -0.3;
}
birdGraphics.rotation = targetRotation;
// Check boundaries
if (self.y < -30 || self.y > 2732 + 30) {
gameOver = true;
}
};
return self;
});
var Pipe = Container.expand(function (isTop) {
var self = Container.call(this);
var pipeGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: isTop ? 1 : 0
});
self.speed = -4;
self.scored = false;
self.isTop = isTop;
self.update = function () {
self.x += self.speed;
if (self.x < -100) {
self.destroy();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var bird;
var pipes = [];
var gameOver = false;
var gameStarted = false;
var pipeTimer = 0;
var gapSize = 300;
var pipeSpacing = 400;
// UI Elements
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var instructionTxt = new Text2('TAP TO START', {
size: 60,
fill: 0xFFFFFF
});
instructionTxt.anchor.set(0.5, 0.5);
game.addChild(instructionTxt);
instructionTxt.x = 1024;
instructionTxt.y = 1366;
// Initialize bird
bird = new Bird();
bird.x = 400;
bird.y = 1366;
game.addChild(bird);
function createPipePair() {
var gapCenter = 400 + Math.random() * (2732 - 800);
var topHeight = gapCenter - gapSize / 2;
var bottomY = gapCenter + gapSize / 2;
// Top pipe
var topPipe = new Pipe(true);
topPipe.x = 2048 + 100;
topPipe.y = topHeight;
pipes.push(topPipe);
game.addChild(topPipe);
// Bottom pipe
var bottomPipe = new Pipe(false);
bottomPipe.x = 2048 + 100;
bottomPipe.y = bottomY;
pipes.push(bottomPipe);
game.addChild(bottomPipe);
}
function resetGame() {
// Remove all pipes
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].destroy();
}
pipes = [];
// Reset bird
bird.x = 400;
bird.y = 1366;
bird.velocity = 0;
bird.flipped = false;
bird.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
}).rotation = 0;
// Reset game state
gameOver = false;
gameStarted = false;
pipeTimer = 0;
LK.setScore(0);
scoreTxt.setText('0');
// Show instruction
instructionTxt.alpha = 1;
}
game.down = function (x, y, obj) {
if (gameOver) {
LK.showGameOver();
return;
}
if (!gameStarted) {
gameStarted = true;
instructionTxt.alpha = 0;
}
bird.flap();
};
game.update = function () {
if (gameOver) {
return;
}
if (!gameStarted) {
return;
}
// Update bird
bird.update();
// Create pipes
pipeTimer++;
if (pipeTimer >= pipeSpacing / 4) {
createPipePair();
pipeTimer = 0;
// Increase difficulty slightly
if (gapSize > 200) {
gapSize -= 0.5;
}
}
// Update pipes and check collisions
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Check if pipe is destroyed
if (pipe.parent === null) {
pipes.splice(i, 1);
continue;
}
// Check collision
if (bird.intersects(pipe)) {
LK.getSound('hit').play();
gameOver = true;
LK.showGameOver();
return;
}
// Check scoring (only for top pipes to avoid double scoring)
if (pipe.isTop && !pipe.scored && pipe.x < bird.x - 30) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore().toString());
LK.getSound('score').play();
}
}
};