/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdSprite = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityY = 0;
self.gravity = 0.9; // Gravity per frame (slower fall)
self.flapStrength = -18; // Negative for upward movement (slower flap)
// Flap method
self.flap = function () {
self.velocityY = self.flapStrength;
// Animate a quick upward tilt
tween.stop(birdSprite, {
rotation: true
});
birdSprite.rotation = -0.35;
tween(birdSprite, {
rotation: 0.25
}, {
duration: 400,
easing: tween.easeOut
});
};
// Update method
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY;
// Clamp rotation based on velocity
if (self.velocityY > 0) {
birdSprite.rotation = Math.min(0.25, birdSprite.rotation + 0.02);
}
};
return self;
});
// Ground class
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundSprite = self.attachAsset('ground', {
anchorX: 0,
anchorY: 0
});
return self;
});
// PipePair class (upper and lower pipes)
var PipePair = Container.expand(function () {
var self = Container.call(this);
// Pipe gap size
self.gap = 420;
// Pipe speed
self.speed = 7;
// Create upper pipe
self.upperPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1.0
});
// Create lower pipe
self.lowerPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.0
});
// Set pipes' positions based on gapY
self.setGapY = function (gapY) {
self.upperPipe.x = 0;
self.upperPipe.y = gapY - self.gap / 2;
self.lowerPipe.x = 0;
self.lowerPipe.y = gapY + self.gap / 2;
};
// Update method
self.update = function () {
self.x -= self.speed;
};
// Helper for collision
self.getUpperPipe = function () {
return self.upperPipe;
};
self.getLowerPipe = function () {
return self.lowerPipe;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Add cloudy background
var bgClouds = LK.getAsset('clouds', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 0,
width: 2048,
height: 2732
});
game.addChild(bgClouds);
// Ground
// Pipe (upper and lower)
// Bird (player)
// Game constants
var BIRD_START_X = 600;
var BIRD_START_Y = 1100;
var PIPE_INTERVAL = 120; // Frames between pipes (slower pipe spawn)
var PIPE_MIN_Y = 500;
var PIPE_MAX_Y = 2000;
var GROUND_Y = 2732 - 120;
// Game state
var bird;
var pipes = [];
var ground;
var score = 0;
var scoreTxt;
var gameStarted = false;
var gameOver = false;
var lastPipeFrame = 0;
// Score display
scoreTxt = new Text2('0', {
size: 180,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Add ground
ground = new Ground();
ground.x = 0;
ground.y = GROUND_Y;
game.addChild(ground);
// Add bird
bird = new Bird();
bird.x = BIRD_START_X;
bird.y = BIRD_START_Y;
game.addChild(bird);
// Reset function
function resetGame() {
// Remove pipes
for (var i = 0; i < pipes.length; i++) {
pipes[i].destroy();
}
pipes = [];
// Reset bird
bird.x = BIRD_START_X;
bird.y = BIRD_START_Y;
bird.velocityY = 0;
// Reset score
score = 0;
scoreTxt.setText(score);
// Reset state
gameStarted = false;
gameOver = false;
lastPipeFrame = LK.ticks;
}
// Start game on first tap
game.down = function (x, y, obj) {
if (gameOver) return;
if (!gameStarted) {
gameStarted = true;
bird.flap();
return;
}
bird.flap();
};
// Main update loop
game.update = function () {
if (gameOver) return;
// Bird physics
if (gameStarted) {
bird.update();
}
// Add pipes
if (gameStarted && LK.ticks - lastPipeFrame >= PIPE_INTERVAL) {
var gapY = PIPE_MIN_Y + Math.floor(Math.random() * (PIPE_MAX_Y - PIPE_MIN_Y));
var pipePair = new PipePair();
pipePair.x = 2048 + 200; // Start just off right edge
pipePair.setGapY(gapY);
pipes.push(pipePair);
game.addChild(pipePair);
lastPipeFrame = LK.ticks;
}
// Update pipes and check for collisions
for (var i = pipes.length - 1; i >= 0; i--) {
var pipePair = pipes[i];
pipePair.update();
// Remove pipes that have gone off screen
if (pipePair.x < -300) {
pipePair.destroy();
pipes.splice(i, 1);
continue;
}
// Only check collision if game is started
if (gameStarted) {
// Check collision with upper pipe
var upperPipe = pipePair.getUpperPipe();
var lowerPipe = pipePair.getLowerPipe();
// Convert pipe positions to world
var pipeGlobalX = pipePair.x;
var upperPipeGlobalY = pipePair.y + upperPipe.y;
var lowerPipeGlobalY = pipePair.y + lowerPipe.y;
// Bird bounding box
var birdLeft = bird.x - 60;
var birdRight = bird.x + 60;
var birdTop = bird.y - 45;
var birdBottom = bird.y + 45;
// Upper pipe bounding box
var upperPipeLeft = pipeGlobalX - 100;
var upperPipeRight = pipeGlobalX + 100;
var upperPipeTop = upperPipeGlobalY - 900;
var upperPipeBottom = upperPipeGlobalY;
// Lower pipe bounding box
var lowerPipeLeft = pipeGlobalX - 100;
var lowerPipeRight = pipeGlobalX + 100;
var lowerPipeTop = lowerPipeGlobalY;
var lowerPipeBottom = lowerPipeGlobalY + 900;
// Collision with upper pipe
if (birdRight > upperPipeLeft && birdLeft < upperPipeRight && birdTop < upperPipeBottom && birdBottom > upperPipeTop) {
endGame();
return;
}
// Collision with lower pipe
if (birdRight > lowerPipeLeft && birdLeft < lowerPipeRight && birdBottom > lowerPipeTop && birdTop < lowerPipeBottom) {
endGame();
return;
}
// Score: when bird passes the center of the pipe
if (!pipePair.passed && pipePair.x + 100 < bird.x) {
pipePair.passed = true;
score += 1;
scoreTxt.setText(score);
}
}
}
// Collision with ground
if (bird.y + 45 >= GROUND_Y) {
endGame();
return;
}
// Collision with ceiling
if (bird.y - 45 <= 0) {
endGame();
return;
}
};
// End game
function endGame() {
if (gameOver) return;
gameOver = true;
// Flash screen red
LK.effects.flashScreen(0xff0000, 800);
// Show game over (auto resets game)
LK.showGameOver();
}
// Reset game on game over (handled by LK, but for safety)
game.onGameOver = function () {
resetGame();
};
// Initial reset
resetGame(); ===================================================================
--- original.js
+++ change.js
@@ -92,14 +92,24 @@
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x87ceeb // Sky blue
+ backgroundColor: 0x000000
});
/****
* Game Code
****/
+// Add cloudy background
+var bgClouds = LK.getAsset('clouds', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: 0,
+ width: 2048,
+ height: 2732
+});
+game.addChild(bgClouds);
// Ground
// Pipe (upper and lower)
// Bird (player)
// Game constants