===================================================================
--- original.js
+++ change.js
@@ -1,114 +1,120 @@
-/****
+/****
* Classes
-****/
+****/
// Assets will be automatically created based on usage in the code.
// Bird class
var Bird = Container.expand(function () {
- var self = Container.call(this);
- var birdGraphics = self.attachAsset('bird', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.gravity = 0.25;
- self.lift = -6;
- self.velocity = 0;
- self.flap = function () {
- self.velocity += self.lift;
- };
- self.update = function () {
- self.velocity += self.gravity;
- self.y += self.velocity;
- if (self.y > 2732 - birdGraphics.height / 2) {
- self.y = 2732 - birdGraphics.height / 2;
- self.velocity = 0;
- }
- if (self.y < birdGraphics.height / 2) {
- self.y = birdGraphics.height / 2;
- self.velocity = 0;
- }
- };
+ var self = Container.call(this);
+ var birdGraphics = self.attachAsset('bird', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.gravity = 0.25;
+ self.lift = -6;
+ self.velocity = 0;
+ self.flap = function () {
+ self.velocity += self.lift;
+ };
+ self.update = function () {
+ self.velocity += self.gravity;
+ self.y += self.velocity;
+ if (self.y > 2732 - birdGraphics.height / 2) {
+ self.y = 2732 - birdGraphics.height / 2;
+ self.velocity = 0;
+ }
+ if (self.y < birdGraphics.height / 2) {
+ self.y = birdGraphics.height / 2;
+ self.velocity = 0;
+ }
+ };
});
// Pipe class
var Pipe = Container.expand(function (isTop, height) {
- var self = Container.call(this);
- var pipeGraphics = self.attachAsset(isTop ? 'pipeTop' : 'pipeBottom', {
- anchorX: 0.5,
- anchorY: isTop ? 1 : 0
- });
- self.height = height;
+ var self = Container.call(this);
+ var pipeGraphics = self.attachAsset(isTop ? 'pipeTop' : 'pipeBottom', {
+ anchorX: 0.5,
+ anchorY: isTop ? 1 : 0
+ });
+ self.height = height;
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x87CEEB // Light blue background to simulate sky
+ backgroundColor: 0x87CEEB // Light blue background to simulate sky
});
-/****
+/****
* Game Code
-****/
+****/
var bird;
var pipes = [];
var gap = 400;
var pipeWidth = 148; // Assuming a pipe width, since it's not specified
var pipeInterval = 1800; // Time in milliseconds between pipe spawns
var lastPipeTime = 0;
// Initialize bird
function initBird() {
- bird = new Bird();
- bird.x = 2048 / 4;
- bird.y = 2732 / 2;
- game.addChild(bird);
+ bird = new Bird();
+ bird.x = 2048 / 4;
+ bird.y = 2732 / 2;
+ game.addChild(bird);
}
// Function to add pipes
function addPipe() {
- var topPipeHeight = Math.floor(Math.random() * (2732 - gap)) + 100; // Random height for top pipe
- var bottomPipeHeight = 2732 - topPipeHeight - gap;
- var topPipe = new Pipe(true, topPipeHeight);
- topPipe.x = 2048;
- topPipe.y = 0;
- pipes.push(topPipe);
- game.addChild(topPipe);
- var bottomPipe = new Pipe(false, bottomPipeHeight);
- bottomPipe.x = 2048;
- bottomPipe.y = 2732 - bottomPipeHeight;
- pipes.push(bottomPipe);
- game.addChild(bottomPipe);
+ var topPipeHeight = Math.floor(Math.random() * (2732 - gap)) + 100; // Random height for top pipe
+ var bottomPipeHeight = 2732 - topPipeHeight - gap;
+ var topPipe = new Pipe(true, topPipeHeight);
+ topPipe.x = 2048;
+ topPipe.y = 0;
+ pipes.push(topPipe);
+ game.addChild(topPipe);
+ var bottomPipe = new Pipe(false, bottomPipeHeight);
+ bottomPipe.x = 2048;
+ bottomPipe.y = 2732 - bottomPipeHeight;
+ pipes.push(bottomPipe);
+ game.addChild(bottomPipe);
}
// Function to update pipes
function updatePipes() {
- for (var i = pipes.length - 1; i >= 0; i--) {
- pipes[i].x -= 5;
- if (pipes[i].x + pipeWidth < 0) {
- pipes[i].destroy();
- pipes.splice(i, 1);
- }
- }
+ for (var i = pipes.length - 1; i >= 0; i--) {
+ pipes[i].x -= 5;
+ if (pipes[i].x + pipeWidth < 0) {
+ pipes[i].destroy();
+ pipes.splice(i, 1);
+ // Increase score when bird passes a pipe
+ score++;
+ }
+ }
}
// Function to check collision
function checkCollision() {
- for (var i = 0; i < pipes.length; i++) {
- if (bird.intersects(pipes[i])) {
- LK.effects.flashScreen(0xff0000, 1000);
- LK.showGameOver();
- }
- }
+ for (var i = 0; i < pipes.length; i++) {
+ if (bird.intersects(pipes[i])) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ // Display the score when the game is over
+ console.log("Score: " + score);
+ }
+ }
}
// Initialize game elements
initBird();
+// Initialize score
+var score = 0;
// Touch event to make the bird flap
game.on('down', function () {
- bird.flap();
+ bird.flap();
});
// Game tick event
LK.on('tick', function () {
- var currentTime = Date.now();
- if (currentTime - lastPipeTime > pipeInterval) {
- addPipe();
- lastPipeTime = currentTime;
- }
- bird.update();
- updatePipes();
- checkCollision();
+ var currentTime = Date.now();
+ if (currentTime - lastPipeTime > pipeInterval) {
+ addPipe();
+ lastPipeTime = currentTime;
+ }
+ bird.update();
+ updatePipes();
+ checkCollision();
});
\ No newline at end of file
A yellow bird. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A green pipe facing upwards. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
Clouds. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.