/**** * 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.8; self.flapPower = -12; self.maxFallSpeed = 15; self.flap = function () { self.velocityY = self.flapPower; LK.getSound('flap').play(); // Bird animation - slight rotation and scale tween(birdGraphics, { rotation: -0.3, scaleY: 1.1 }, { duration: 150 }); tween(birdGraphics, { rotation: 0.3, scaleY: 1 }, { duration: 300 }); }; self.update = function () { // Apply gravity self.velocityY += self.gravity; // Limit fall speed if (self.velocityY > self.maxFallSpeed) { self.velocityY = self.maxFallSpeed; } // Update position self.y += self.velocityY; // Rotate bird based on velocity if (self.velocityY < 0) { birdGraphics.rotation = Math.max(-0.5, self.velocityY * 0.05); } else { birdGraphics.rotation = Math.min(0.8, self.velocityY * 0.05); } }; return self; }); var Pipe = Container.expand(function (gapY) { var self = Container.call(this); self.gapSize = 300; self.speed = 4; self.scored = false; // Top pipe var topPipe = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 1 }); topPipe.y = gapY - self.gapSize / 2; // Bottom pipe var bottomPipe = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 0 }); 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); // Create ground ground = game.addChild(LK.getAsset('ground', { anchorX: 0, anchorY: 0 })); ground.x = 0; ground.y = 2732 - 100; // Create bird bird = game.addChild(new Bird()); bird.x = 400; bird.y = 1366; // Create first pipe function createPipe() { var gapY = Math.random() * (2200 - 400) + 400; // Random gap position var pipe = new Pipe(gapY); pipe.x = 2048 + 60; pipes.push(pipe); game.addChild(pipe); } // Initial pipe createPipe(); game.down = function (x, y, obj) { if (!gameStarted) { gameStarted = true; } bird.flap(); }; game.update = function () { if (!gameStarted) return; // Spawn new pipes pipeSpawnTimer++; if (pipeSpawnTimer >= pipeSpawnInterval) { createPipe(); pipeSpawnTimer = 0; } // Update and check pipes for (var i = pipes.length - 1; i >= 0; i--) { var pipe = pipes[i]; // Check if pipe has moved off screen if (pipe.x < -120) { pipe.destroy(); pipes.splice(i, 1); continue; } // Check collision with bird if (bird.intersects(pipe)) { LK.getSound('hit').play(); LK.showGameOver(); return; } // Check scoring if (!pipe.scored && pipe.x + 60 < bird.x) { pipe.scored = true; LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); LK.getSound('score').play(); } } // Check ground collision if (bird.y + 22 > ground.y) { LK.getSound('hit').play(); LK.showGameOver(); return; } // Check ceiling collision if (bird.y - 22 < 0) { LK.getSound('hit').play(); LK.showGameOver(); return; } };
/****
* 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.8;
self.flapPower = -12;
self.maxFallSpeed = 15;
self.flap = function () {
self.velocityY = self.flapPower;
LK.getSound('flap').play();
// Bird animation - slight rotation and scale
tween(birdGraphics, {
rotation: -0.3,
scaleY: 1.1
}, {
duration: 150
});
tween(birdGraphics, {
rotation: 0.3,
scaleY: 1
}, {
duration: 300
});
};
self.update = function () {
// Apply gravity
self.velocityY += self.gravity;
// Limit fall speed
if (self.velocityY > self.maxFallSpeed) {
self.velocityY = self.maxFallSpeed;
}
// Update position
self.y += self.velocityY;
// Rotate bird based on velocity
if (self.velocityY < 0) {
birdGraphics.rotation = Math.max(-0.5, self.velocityY * 0.05);
} else {
birdGraphics.rotation = Math.min(0.8, self.velocityY * 0.05);
}
};
return self;
});
var Pipe = Container.expand(function (gapY) {
var self = Container.call(this);
self.gapSize = 300;
self.speed = 4;
self.scored = false;
// Top pipe
var topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
topPipe.y = gapY - self.gapSize / 2;
// Bottom pipe
var bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
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);
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
}));
ground.x = 0;
ground.y = 2732 - 100;
// Create bird
bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366;
// Create first pipe
function createPipe() {
var gapY = Math.random() * (2200 - 400) + 400; // Random gap position
var pipe = new Pipe(gapY);
pipe.x = 2048 + 60;
pipes.push(pipe);
game.addChild(pipe);
}
// Initial pipe
createPipe();
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
}
bird.flap();
};
game.update = function () {
if (!gameStarted) return;
// Spawn new pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
createPipe();
pipeSpawnTimer = 0;
}
// Update and check pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
// Check if pipe has moved off screen
if (pipe.x < -120) {
pipe.destroy();
pipes.splice(i, 1);
continue;
}
// Check collision with bird
if (bird.intersects(pipe)) {
LK.getSound('hit').play();
LK.showGameOver();
return;
}
// Check scoring
if (!pipe.scored && pipe.x + 60 < bird.x) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
}
}
// Check ground collision
if (bird.y + 22 > ground.y) {
LK.getSound('hit').play();
LK.showGameOver();
return;
}
// Check ceiling collision
if (bird.y - 22 < 0) {
LK.getSound('hit').play();
LK.showGameOver();
return;
}
};