/**** * 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.maxFallSpeed = 15; self.flap = function () { self.velocity = self.flapPower; LK.getSound('flap').play(); // Add a subtle flap animation tween(birdGraphics, { rotation: -0.3 }, { duration: 100 }); tween(birdGraphics, { rotation: 0 }, { duration: 200 }); }; self.update = function () { // Apply gravity self.velocity += self.gravity; // Limit fall speed if (self.velocity > self.maxFallSpeed) { self.velocity = self.maxFallSpeed; } // Update position self.y += self.velocity; // Rotate bird based on velocity if (self.velocity < 0) { birdGraphics.rotation = Math.max(-0.5, self.velocity * 0.05); } else { birdGraphics.rotation = Math.min(1.2, self.velocity * 0.08); } // Check ground collision if (self.y > 2732 - 100 - 30) { // Ground height minus bird radius gameOver = true; } // Check ceiling collision if (self.y < 30) { gameOver = true; } }; return self; }); var Pipe = Container.expand(function () { var self = Container.call(this); self.speed = -4; self.gapSize = 300; 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.setupPipes = function (gapCenterY) { self.topPipe.y = gapCenterY - self.gapSize / 2; self.bottomPipe.y = gapCenterY + self.gapSize / 2; }; self.update = function () { self.x += self.speed; // Check if bird passed through this pipe if (!self.scored && self.x < bird.x - 60) { self.scored = true; score++; LK.setScore(score); scoreTxt.setText(score); LK.getSound('score').play(); } }; self.checkCollision = function (birdObj) { var birdBounds = { x: birdObj.x - 25, y: birdObj.y - 25, width: 50, height: 50 }; var topPipeBounds = { x: self.x - 60, y: 0, width: 120, height: self.topPipe.y }; var bottomPipeBounds = { x: self.x - 60, y: self.bottomPipe.y, width: 120, height: 2732 - self.bottomPipe.y }; return self.intersectsBounds(birdBounds, topPipeBounds) || self.intersectsBounds(birdBounds, bottomPipeBounds); }; self.intersectsBounds = function (rect1, rect2) { return rect1.x < rect2.x + rect2.width && rect1.x + rect1.width > rect2.x && rect1.y < rect2.y + rect2.height && rect1.y + rect1.height > rect2.y; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB }); /**** * Game Code ****/ var bird; var pipes = []; var ground; var scoreTxt; var score = 0; var gameOver = false; var gameStarted = false; var pipeSpawnTimer = 0; var pipeSpawnInterval = 120; // 2 seconds at 60fps // Create bird bird = game.addChild(new Bird()); bird.x = 400; bird.y = 1366; // Center of screen // Create ground ground = game.addChild(LK.getAsset('ground', { anchorX: 0, anchorY: 0 })); ground.x = 0; ground.y = 2732 - 100; // Create score text scoreTxt = new Text2('0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Create instruction text var instructionTxt = new Text2('TAP TO FLAP', { size: 60, fill: 0xFFFFFF }); instructionTxt.anchor.set(0.5, 0.5); instructionTxt.x = 2048 / 2; instructionTxt.y = 1366 - 200; game.addChild(instructionTxt); function spawnPipe() { var pipe = new Pipe(); pipe.x = 2048 + 60; // Random gap position (avoid too high or too low) var minGapCenter = 200 + pipe.gapSize / 2; var maxGapCenter = 2732 - 200 - pipe.gapSize / 2; var gapCenterY = minGapCenter + Math.random() * (maxGapCenter - minGapCenter); pipe.setupPipes(gapCenterY); pipes.push(pipe); game.addChild(pipe); } function resetGame() { // Clear 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; // Reset score score = 0; LK.setScore(0); scoreTxt.setText('0'); // Reset game state gameOver = false; gameStarted = false; pipeSpawnTimer = 0; // Show instruction text instructionTxt.visible = true; } game.down = function (x, y, obj) { if (gameOver) { LK.showGameOver(); return; } if (!gameStarted) { gameStarted = true; instructionTxt.visible = false; } bird.flap(); }; game.update = function () { if (gameOver) { return; } if (!gameStarted) { 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]; // Check collision if (pipe.checkCollision(bird)) { gameOver = true; LK.showGameOver(); return; } // Remove pipes that are off screen if (pipe.x < -120) { pipe.destroy(); pipes.splice(i, 1); } } };
/****
* 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.maxFallSpeed = 15;
self.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add a subtle flap animation
tween(birdGraphics, {
rotation: -0.3
}, {
duration: 100
});
tween(birdGraphics, {
rotation: 0
}, {
duration: 200
});
};
self.update = function () {
// Apply gravity
self.velocity += self.gravity;
// Limit fall speed
if (self.velocity > self.maxFallSpeed) {
self.velocity = self.maxFallSpeed;
}
// Update position
self.y += self.velocity;
// Rotate bird based on velocity
if (self.velocity < 0) {
birdGraphics.rotation = Math.max(-0.5, self.velocity * 0.05);
} else {
birdGraphics.rotation = Math.min(1.2, self.velocity * 0.08);
}
// Check ground collision
if (self.y > 2732 - 100 - 30) {
// Ground height minus bird radius
gameOver = true;
}
// Check ceiling collision
if (self.y < 30) {
gameOver = true;
}
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -4;
self.gapSize = 300;
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.setupPipes = function (gapCenterY) {
self.topPipe.y = gapCenterY - self.gapSize / 2;
self.bottomPipe.y = gapCenterY + self.gapSize / 2;
};
self.update = function () {
self.x += self.speed;
// Check if bird passed through this pipe
if (!self.scored && self.x < bird.x - 60) {
self.scored = true;
score++;
LK.setScore(score);
scoreTxt.setText(score);
LK.getSound('score').play();
}
};
self.checkCollision = function (birdObj) {
var birdBounds = {
x: birdObj.x - 25,
y: birdObj.y - 25,
width: 50,
height: 50
};
var topPipeBounds = {
x: self.x - 60,
y: 0,
width: 120,
height: self.topPipe.y
};
var bottomPipeBounds = {
x: self.x - 60,
y: self.bottomPipe.y,
width: 120,
height: 2732 - self.bottomPipe.y
};
return self.intersectsBounds(birdBounds, topPipeBounds) || self.intersectsBounds(birdBounds, bottomPipeBounds);
};
self.intersectsBounds = function (rect1, rect2) {
return rect1.x < rect2.x + rect2.width && rect1.x + rect1.width > rect2.x && rect1.y < rect2.y + rect2.height && rect1.y + rect1.height > rect2.y;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var bird;
var pipes = [];
var ground;
var scoreTxt;
var score = 0;
var gameOver = false;
var gameStarted = false;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120; // 2 seconds at 60fps
// Create bird
bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366; // Center of screen
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
}));
ground.x = 0;
ground.y = 2732 - 100;
// Create score text
scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create instruction text
var instructionTxt = new Text2('TAP TO FLAP', {
size: 60,
fill: 0xFFFFFF
});
instructionTxt.anchor.set(0.5, 0.5);
instructionTxt.x = 2048 / 2;
instructionTxt.y = 1366 - 200;
game.addChild(instructionTxt);
function spawnPipe() {
var pipe = new Pipe();
pipe.x = 2048 + 60;
// Random gap position (avoid too high or too low)
var minGapCenter = 200 + pipe.gapSize / 2;
var maxGapCenter = 2732 - 200 - pipe.gapSize / 2;
var gapCenterY = minGapCenter + Math.random() * (maxGapCenter - minGapCenter);
pipe.setupPipes(gapCenterY);
pipes.push(pipe);
game.addChild(pipe);
}
function resetGame() {
// Clear 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;
// Reset score
score = 0;
LK.setScore(0);
scoreTxt.setText('0');
// Reset game state
gameOver = false;
gameStarted = false;
pipeSpawnTimer = 0;
// Show instruction text
instructionTxt.visible = true;
}
game.down = function (x, y, obj) {
if (gameOver) {
LK.showGameOver();
return;
}
if (!gameStarted) {
gameStarted = true;
instructionTxt.visible = false;
}
bird.flap();
};
game.update = function () {
if (gameOver) {
return;
}
if (!gameStarted) {
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];
// Check collision
if (pipe.checkCollision(bird)) {
gameOver = true;
LK.showGameOver();
return;
}
// Remove pipes that are off screen
if (pipe.x < -120) {
pipe.destroy();
pipes.splice(i, 1);
}
}
};