/****
* 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.4;
self.flapPower = -12;
self.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add flap animation
tween(birdGraphics, {
rotation: -0.3
}, {
duration: 100
});
tween(birdGraphics, {
rotation: 0.3
}, {
duration: 200,
onFinish: function onFinish() {
tween(birdGraphics, {
rotation: 0
}, {
duration: 100
});
}
});
};
self.update = function () {
// Only apply physics when game has started
if (gameStarted) {
self.velocity += self.gravity;
self.y += self.velocity;
// Rotate bird based on velocity
if (self.velocity > 0) {
birdGraphics.rotation = Math.min(self.velocity * 0.05, 0.5);
} else {
birdGraphics.rotation = Math.max(self.velocity * 0.05, -0.5);
}
}
};
return self;
});
var Cloud = Container.expand(function () {
var self = Container.call(this);
// Create cloud using a white ellipse
var cloudGraphics = self.attachAsset('cloud', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -1.5; // Slower than pipes for parallax effect
self.update = function () {
self.x += self.speed;
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.speed = -4;
self.gapHeight = 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.setGapPosition = function (gapY) {
self.topPipe.y = gapY - self.gapHeight / 2;
self.bottomPipe.y = gapY + self.gapHeight / 2;
};
self.update = function () {
self.x += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game variables
var bird;
var pipes = [];
var clouds = [];
var ground;
var gameStarted = false;
var gameActive = false;
var pipeTimer = 0;
var pipeInterval = 90; // frames between pipes
var cloudTimer = 0;
var cloudInterval = 120; // frames between clouds
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 1,
x: 0,
y: 2732
}));
// Create bird
bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366; // Center of screen vertically
// Create score display
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 150;
// Create start instruction
var startTxt = new Text2('TAP TO START', {
size: 80,
fill: 0xFFFFFF
});
startTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(startTxt);
// Create initial clouds for immediate visual effect
for (var i = 0; i < 3; i++) {
var cloud = new Cloud();
cloud.x = Math.random() * 2048;
cloud.y = 200 + Math.random() * 800;
var scale = 0.8 + Math.random() * 0.6;
cloud.scaleX = scale;
cloud.scaleY = scale;
cloud.alpha = 0.6 + Math.random() * 0.3;
clouds.push(cloud);
game.addChild(cloud);
}
function createPipe() {
var pipe = new Pipe();
pipe.x = 2048 + 60; // Start off right edge
// Random gap position (avoid too high or too low)
var minGapY = 200;
var maxGapY = 2532; // Account for ground height
var gapY = minGapY + Math.random() * (maxGapY - minGapY);
pipe.setGapPosition(gapY);
pipes.push(pipe);
game.addChild(pipe);
}
function createCloud() {
var cloud = new Cloud();
cloud.x = 2048 + 100; // Start off right edge
// Random Y position in upper part of screen
cloud.y = 200 + Math.random() * 800;
// Random scale for variety
var scale = 0.8 + Math.random() * 0.6;
cloud.scaleX = scale;
cloud.scaleY = scale;
// Random opacity for depth effect
cloud.alpha = 0.6 + Math.random() * 0.3;
clouds.push(cloud);
game.addChild(cloud);
}
function checkCollisions() {
// Check ground collision
if (bird.y + 30 >= 2632) {
// Ground level minus bird radius
return true;
}
// Check ceiling collision
if (bird.y - 30 <= 0) {
return true;
}
// Check pipe collisions
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
// Check if bird is in horizontal range of pipe
if (bird.x + 30 > pipe.x - 60 && bird.x - 30 < pipe.x + 60) {
// Check top pipe collision
if (bird.y - 30 < pipe.topPipe.y) {
return true;
}
// Check bottom pipe collision
if (bird.y + 30 > pipe.bottomPipe.y) {
return true;
}
}
}
return false;
}
function updateScore() {
for (var i = 0; i < pipes.length; i++) {
var pipe = pipes[i];
// Check if bird passed this pipe
if (!pipe.scored && bird.x > pipe.x + 60) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.getSound('score').play();
}
}
}
// Game input handling
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
gameActive = true;
startTxt.visible = false;
// Reset bird velocity for clean start
bird.velocity = 0;
}
if (gameActive) {
bird.flap();
}
};
// Main game loop
game.update = function () {
if (!gameActive) {
return;
}
// Update bird
bird.update();
// Check collisions
if (checkCollisions()) {
gameActive = false;
LK.showGameOver();
return;
}
// Update score
updateScore();
// Create new pipes
pipeTimer++;
if (pipeTimer >= pipeInterval) {
createPipe();
pipeTimer = 0;
}
// Create new clouds
cloudTimer++;
if (cloudTimer >= cloudInterval) {
createCloud();
cloudTimer = 0;
}
// Update and clean up pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
pipe.update();
// Remove pipes that are off screen
if (pipe.x < -120) {
pipe.destroy();
pipes.splice(i, 1);
}
}
// Update and clean up clouds
for (var i = clouds.length - 1; i >= 0; i--) {
var cloud = clouds[i];
cloud.update();
// Remove clouds that are off screen
if (cloud.x < -150) {
cloud.destroy();
clouds.splice(i, 1);
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -37,15 +37,18 @@
}
});
};
self.update = function () {
- self.velocity += self.gravity;
- self.y += self.velocity;
- // Rotate bird based on velocity
- if (self.velocity > 0) {
- birdGraphics.rotation = Math.min(self.velocity * 0.05, 0.5);
- } else {
- birdGraphics.rotation = Math.max(self.velocity * 0.05, -0.5);
+ // Only apply physics when game has started
+ if (gameStarted) {
+ self.velocity += self.gravity;
+ self.y += self.velocity;
+ // Rotate bird based on velocity
+ if (self.velocity > 0) {
+ birdGraphics.rotation = Math.min(self.velocity * 0.05, 0.5);
+ } else {
+ birdGraphics.rotation = Math.max(self.velocity * 0.05, -0.5);
+ }
}
};
return self;
});
@@ -215,8 +218,10 @@
if (!gameStarted) {
gameStarted = true;
gameActive = true;
startTxt.visible = false;
+ // Reset bird velocity for clean start
+ bird.velocity = 0;
}
if (gameActive) {
bird.flap();
}