/****
* 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
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 () {
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 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 ground;
var gameStarted = false;
var gameActive = false;
var pipeTimer = 0;
var pipeInterval = 90; // frames between pipes
// 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);
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 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;
}
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;
}
// 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);
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,213 @@
-/****
+/****
+* 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
+ 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 () {
+ 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 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: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+// Game variables
+var bird;
+var pipes = [];
+var ground;
+var gameStarted = false;
+var gameActive = false;
+var pipeTimer = 0;
+var pipeInterval = 90; // frames between pipes
+// 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);
+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 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;
+ }
+ 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;
+ }
+ // 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);
+ }
+ }
+};
\ No newline at end of file