/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Bird class var Bird = Container.expand(function () { var self = Container.call(this); // Attach bird asset (ellipse, yellow) var birdAsset = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); // Set up physics self.vy = 0; // vertical speed self.gravity = 1.0; // gravity per frame (reduced for slower fall) self.flapStrength = -20; // negative for upward movement (reduced for slower jump) // Bird update: apply gravity, move, clamp position self.update = function () { self.vy += self.gravity; self.y += self.vy; // Clamp to top of screen if (self.y < self.height / 2) { self.y = self.height / 2; self.vy = 0; } }; // Flap: set upward velocity self.flap = function () { self.vy = self.flapStrength; }; return self; }); // PipePair class (top and bottom pipes) var PipePair = Container.expand(function () { var self = Container.call(this); // Pipe config self.pipeWidth = 220; self.gapHeight = 700; self.speed = 7; // Randomize gap position var minGapY = 350; var maxGapY = 2732 - 350 - self.gapHeight; var gapY = minGapY + Math.floor(Math.random() * (maxGapY - minGapY + 1)); // Top pipe self.topPipe = self.attachAsset('pipe', { anchorX: 0, anchorY: 1, width: self.pipeWidth, height: gapY, y: gapY, x: 0, tint: 0x3bb44a }); // Bottom pipe self.bottomPipe = self.attachAsset('pipe', { anchorX: 0, anchorY: 0, width: self.pipeWidth, height: 2732 - (gapY + self.gapHeight), y: gapY + self.gapHeight, x: 0, tint: 0x3bb44a }); // Used for scoring self.passed = false; // Move pipes left self.update = function () { self.x -= self.speed; }; // Helper: get bounding rectangles for collision self.getTopRect = function () { return new Rectangle(self.x, 0, self.pipeWidth, gapY); }; self.getBottomRect = function () { return new Rectangle(self.x, gapY + self.gapHeight, self.pipeWidth, 2732 - (gapY + self.gapHeight)); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb // Sky blue }); /**** * Game Code ****/ // --- Game Variables --- // --- Asset Initialization --- var bird; var pipes = []; var score = 0; var scoreTxt; var groundY = 2732 - 120; // ground is at bottom, bird is 120px tall var pipeInterval = 120; // frames between pipes (slower pipe spawn) var ticksSincePipe = 0; var gameStarted = false; var gameOver = false; // --- Score Display --- scoreTxt = new Text2('0', { size: 180, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // --- Bird --- bird = new Bird(); game.addChild(bird); bird.x = 2048 / 3; bird.y = 2732 / 2; // --- Game Start Helper --- function startGame() { // Reset for (var i = 0; i < pipes.length; i++) { pipes[i].destroy(); } pipes = []; score = 0; scoreTxt.setText(score); bird.x = 2048 / 3; bird.y = 2732 / 2; bird.vy = 0; ticksSincePipe = 0; gameStarted = true; gameOver = false; } // --- Input: Tap to Flap --- game.down = function (x, y, obj) { if (!gameStarted) { startGame(); } if (!gameOver) { bird.flap(); } }; // --- Main Update Loop --- game.update = function () { if (!gameStarted || gameOver) { return; } // Bird physics bird.update(); // Add new pipes ticksSincePipe++; if (ticksSincePipe >= pipeInterval) { ticksSincePipe = 0; var pipePair = new PipePair(); pipePair.x = 2048; game.addChild(pipePair); pipes.push(pipePair); } // Update pipes, check for offscreen, collisions, scoring for (var i = pipes.length - 1; i >= 0; i--) { var pipe = pipes[i]; pipe.update(); // Remove pipes offscreen if (pipe.x + pipe.pipeWidth < 0) { pipe.destroy(); pipes.splice(i, 1); continue; } // Collision detection (bird vs pipes) var birdRect = new Rectangle(bird.x - bird.width / 2 + 10, bird.y - bird.height / 2 + 10, bird.width - 20, bird.height - 20); var topRect = new Rectangle(pipe.x, 0, pipe.pipeWidth, pipe.topPipe.height); var bottomRect = new Rectangle(pipe.x, pipe.bottomPipe.y, pipe.pipeWidth, pipe.bottomPipe.height); if (rectsIntersect(birdRect, topRect) || rectsIntersect(birdRect, bottomRect)) { triggerGameOver(); return; } // Scoring: passed pipe if (!pipe.passed && pipe.x + pipe.pipeWidth < bird.x) { pipe.passed = true; score++; scoreTxt.setText(score); } } // Collision with ground or ceiling if (bird.y + bird.height / 2 >= 2732) { bird.y = 2732 - bird.height / 2; triggerGameOver(); return; } if (bird.y - bird.height / 2 <= 0) { bird.y = bird.height / 2; bird.vy = 0; } }; // --- Rectangle Intersection Helper --- function rectsIntersect(r1, r2) { return !(r2.x > r1.x + r1.width || r2.x + r2.width < r1.x || r2.y > r1.y + r1.height || r2.y + r2.height < r1.y); } // --- Game Over Handler --- function triggerGameOver() { if (gameOver) return; gameOver = true; LK.effects.flashScreen(0xff0000, 600); LK.showGameOver(); } // --- Initial State: Show bird, wait for tap --- gameStarted = false; gameOver = false; score = 0; scoreTxt.setText(score);
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
// Attach bird asset (ellipse, yellow)
var birdAsset = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
// Set up physics
self.vy = 0; // vertical speed
self.gravity = 1.0; // gravity per frame (reduced for slower fall)
self.flapStrength = -20; // negative for upward movement (reduced for slower jump)
// Bird update: apply gravity, move, clamp position
self.update = function () {
self.vy += self.gravity;
self.y += self.vy;
// Clamp to top of screen
if (self.y < self.height / 2) {
self.y = self.height / 2;
self.vy = 0;
}
};
// Flap: set upward velocity
self.flap = function () {
self.vy = self.flapStrength;
};
return self;
});
// PipePair class (top and bottom pipes)
var PipePair = Container.expand(function () {
var self = Container.call(this);
// Pipe config
self.pipeWidth = 220;
self.gapHeight = 700;
self.speed = 7;
// Randomize gap position
var minGapY = 350;
var maxGapY = 2732 - 350 - self.gapHeight;
var gapY = minGapY + Math.floor(Math.random() * (maxGapY - minGapY + 1));
// Top pipe
self.topPipe = self.attachAsset('pipe', {
anchorX: 0,
anchorY: 1,
width: self.pipeWidth,
height: gapY,
y: gapY,
x: 0,
tint: 0x3bb44a
});
// Bottom pipe
self.bottomPipe = self.attachAsset('pipe', {
anchorX: 0,
anchorY: 0,
width: self.pipeWidth,
height: 2732 - (gapY + self.gapHeight),
y: gapY + self.gapHeight,
x: 0,
tint: 0x3bb44a
});
// Used for scoring
self.passed = false;
// Move pipes left
self.update = function () {
self.x -= self.speed;
};
// Helper: get bounding rectangles for collision
self.getTopRect = function () {
return new Rectangle(self.x, 0, self.pipeWidth, gapY);
};
self.getBottomRect = function () {
return new Rectangle(self.x, gapY + self.gapHeight, self.pipeWidth, 2732 - (gapY + self.gapHeight));
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb // Sky blue
});
/****
* Game Code
****/
// --- Game Variables ---
// --- Asset Initialization ---
var bird;
var pipes = [];
var score = 0;
var scoreTxt;
var groundY = 2732 - 120; // ground is at bottom, bird is 120px tall
var pipeInterval = 120; // frames between pipes (slower pipe spawn)
var ticksSincePipe = 0;
var gameStarted = false;
var gameOver = false;
// --- Score Display ---
scoreTxt = new Text2('0', {
size: 180,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// --- Bird ---
bird = new Bird();
game.addChild(bird);
bird.x = 2048 / 3;
bird.y = 2732 / 2;
// --- Game Start Helper ---
function startGame() {
// Reset
for (var i = 0; i < pipes.length; i++) {
pipes[i].destroy();
}
pipes = [];
score = 0;
scoreTxt.setText(score);
bird.x = 2048 / 3;
bird.y = 2732 / 2;
bird.vy = 0;
ticksSincePipe = 0;
gameStarted = true;
gameOver = false;
}
// --- Input: Tap to Flap ---
game.down = function (x, y, obj) {
if (!gameStarted) {
startGame();
}
if (!gameOver) {
bird.flap();
}
};
// --- Main Update Loop ---
game.update = function () {
if (!gameStarted || gameOver) {
return;
}
// Bird physics
bird.update();
// Add new pipes
ticksSincePipe++;
if (ticksSincePipe >= pipeInterval) {
ticksSincePipe = 0;
var pipePair = new PipePair();
pipePair.x = 2048;
game.addChild(pipePair);
pipes.push(pipePair);
}
// Update pipes, check for offscreen, collisions, scoring
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Remove pipes offscreen
if (pipe.x + pipe.pipeWidth < 0) {
pipe.destroy();
pipes.splice(i, 1);
continue;
}
// Collision detection (bird vs pipes)
var birdRect = new Rectangle(bird.x - bird.width / 2 + 10, bird.y - bird.height / 2 + 10, bird.width - 20, bird.height - 20);
var topRect = new Rectangle(pipe.x, 0, pipe.pipeWidth, pipe.topPipe.height);
var bottomRect = new Rectangle(pipe.x, pipe.bottomPipe.y, pipe.pipeWidth, pipe.bottomPipe.height);
if (rectsIntersect(birdRect, topRect) || rectsIntersect(birdRect, bottomRect)) {
triggerGameOver();
return;
}
// Scoring: passed pipe
if (!pipe.passed && pipe.x + pipe.pipeWidth < bird.x) {
pipe.passed = true;
score++;
scoreTxt.setText(score);
}
}
// Collision with ground or ceiling
if (bird.y + bird.height / 2 >= 2732) {
bird.y = 2732 - bird.height / 2;
triggerGameOver();
return;
}
if (bird.y - bird.height / 2 <= 0) {
bird.y = bird.height / 2;
bird.vy = 0;
}
};
// --- Rectangle Intersection Helper ---
function rectsIntersect(r1, r2) {
return !(r2.x > r1.x + r1.width || r2.x + r2.width < r1.x || r2.y > r1.y + r1.height || r2.y + r2.height < r1.y);
}
// --- Game Over Handler ---
function triggerGameOver() {
if (gameOver) return;
gameOver = true;
LK.effects.flashScreen(0xff0000, 600);
LK.showGameOver();
}
// --- Initial State: Show bird, wait for tap ---
gameStarted = false;
gameOver = false;
score = 0;
scoreTxt.setText(score);