/****
* 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.flap = function () {
self.velocity = self.flapPower;
LK.getSound('flap').play();
// Add flap animation
birdGraphics.rotation = -0.3;
tween(birdGraphics, {
rotation: 0.3
}, {
duration: 300
});
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
// Rotate bird based on velocity
birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
};
return self;
});
var Pipe = Container.expand(function () {
var self = Container.call(this);
self.gapSize = 300;
self.speed = 4;
self.scored = false;
var topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
var bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.setGapPosition = function (gapY) {
topPipe.y = gapY - self.gapSize / 2;
bottomPipe.y = gapY + self.gapSize / 2;
};
self.update = function () {
self.x -= self.speed;
};
self.checkCollision = function (bird) {
var birdBounds = {
x: bird.x - 30,
y: bird.y - 30,
width: 60,
height: 60
};
var topPipeBounds = {
x: self.x - 60,
y: topPipe.y - 400,
width: 120,
height: 400
};
var bottomPipeBounds = {
x: self.x - 60,
y: bottomPipe.y,
width: 120,
height: 400
};
return birdBounds.x < topPipeBounds.x + topPipeBounds.width && birdBounds.x + birdBounds.width > topPipeBounds.x && birdBounds.y < topPipeBounds.y + topPipeBounds.height && birdBounds.y + birdBounds.height > topPipeBounds.y || birdBounds.x < bottomPipeBounds.x + bottomPipeBounds.width && birdBounds.x + birdBounds.width > bottomPipeBounds.x && birdBounds.y < bottomPipeBounds.y + bottomPipeBounds.height && birdBounds.y + birdBounds.height > bottomPipeBounds.y;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366;
var ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0
}));
ground.x = 0;
ground.y = 2632;
var pipes = [];
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120; // Spawn pipe every 2 seconds at 60fps
var gameStarted = false;
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var goldTxt = new Text2('Gold: 0', {
size: 60,
fill: 0xFFD700
});
goldTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(goldTxt);
var instructionTxt = new Text2('TAP TO START', {
size: 60,
fill: 0xFFFFFF
});
instructionTxt.anchor.set(0.5, 0.5);
instructionTxt.x = 1024;
instructionTxt.y = 800;
game.addChild(instructionTxt);
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
game.removeChild(instructionTxt);
}
bird.flap();
};
game.update = function () {
if (!gameStarted) return;
// Check if bird hits ground or ceiling
if (bird.y > 2632 || bird.y < 0) {
LK.showGameOver();
return;
}
// Spawn pipes
pipeSpawnTimer++;
if (pipeSpawnTimer >= pipeSpawnInterval) {
pipeSpawnTimer = 0;
var newPipe = new Pipe();
newPipe.x = 2048 + 60;
var gapY = Math.random() * (2000 - 600) + 600; // Random gap between y=600 and y=2000
newPipe.setGapPosition(gapY);
pipes.push(newPipe);
game.addChild(newPipe);
}
// Update and check pipes
for (var i = pipes.length - 1; i >= 0; i--) {
var pipe = pipes[i];
// Check collision
if (pipe.checkCollision(bird)) {
LK.showGameOver();
return;
}
// Check scoring
if (!pipe.scored && bird.x > pipe.x) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore().toString());
goldTxt.setText('Gold: ' + LK.getScore().toString());
LK.getSound('score').play();
}
// Remove pipes that are off-screen
if (pipe.x < -120) {
pipe.destroy();
pipes.splice(i, 1);
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -106,8 +106,14 @@
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
+var goldTxt = new Text2('Gold: 0', {
+ size: 60,
+ fill: 0xFFD700
+});
+goldTxt.anchor.set(1, 0);
+LK.gui.topRight.addChild(goldTxt);
var instructionTxt = new Text2('TAP TO START', {
size: 60,
fill: 0xFFFFFF
});
@@ -152,8 +158,9 @@
if (!pipe.scored && bird.x > pipe.x) {
pipe.scored = true;
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore().toString());
+ goldTxt.setText('Gold: ' + LK.getScore().toString());
LK.getSound('score').play();
}
// Remove pipes that are off-screen
if (pipe.x < -120) {