/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Bird class to represent the player's character var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('multicolorBird', { anchorX: 0.5, anchorY: 0.5, scaleX: 3.0, scaleY: 3.0 }); self.speedY = 0; self.gravity = 0.5; self.flapStrength = -10; // Update method to apply gravity and movement self.update = function () { self.speedY += self.gravity; self.y += self.speedY; }; // Method to make the bird flap self.flap = function () { self.speedY = self.flapStrength; }; }); // Obstacle class to represent obstacles in the game var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5, scaleX: 3.0, scaleY: 3.0 }); self.speedX = -12; // Update method to move the obstacle self.update = function () { self.x += self.speedX; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xFFFFFF // White background }); /**** * Game Code ****/ // Play background music LK.playMusic('bgmusic', { volume: 0.5 }); // Initialize game variables var bird = game.addChild(new Bird()); // Set a timeout to end the game after 60 seconds LK.setTimeout(function () { LK.showGameOver(); }, 60000); bird.x = 2048 / 4; bird.y = 2732 / 2; var obstacles = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#0000ff" // Blue color }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to create a new obstacle function createObstacle() { var obstacle = new Obstacle(); obstacle.x = 2048; obstacle.y = Math.random() * 2732; obstacles.push(obstacle); game.addChild(obstacle); } // Handle touch events to make the bird flap game.down = function (x, y, obj) { bird.flap(); }; // Update game logic game.update = function () { bird.update(); // Update obstacles and check for collisions for (var i = obstacles.length - 1; i >= 0; i--) { obstacles[i].update(); // Check if the bird hits an obstacle if (bird.intersects(obstacles[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // Remove obstacles that are off-screen if (obstacles[i].x < -obstacles[i].width) { obstacles[i].destroy(); obstacles.splice(i, 1); score++; scoreTxt.setText(score); } } // Create new obstacles periodically if (LK.ticks % 120 == 0) { createObstacle(); } // Check if the bird hits the ground or flies too high if (bird.y > 2732 || bird.y < 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Bird class to represent the player's character
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('multicolorBird', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 3.0,
scaleY: 3.0
});
self.speedY = 0;
self.gravity = 0.5;
self.flapStrength = -10;
// Update method to apply gravity and movement
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
};
// Method to make the bird flap
self.flap = function () {
self.speedY = self.flapStrength;
};
});
// Obstacle class to represent obstacles in the game
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 3.0,
scaleY: 3.0
});
self.speedX = -12;
// Update method to move the obstacle
self.update = function () {
self.x += self.speedX;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFFFFF // White background
});
/****
* Game Code
****/
// Play background music
LK.playMusic('bgmusic', {
volume: 0.5
});
// Initialize game variables
var bird = game.addChild(new Bird());
// Set a timeout to end the game after 60 seconds
LK.setTimeout(function () {
LK.showGameOver();
}, 60000);
bird.x = 2048 / 4;
bird.y = 2732 / 2;
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#0000ff" // Blue color
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to create a new obstacle
function createObstacle() {
var obstacle = new Obstacle();
obstacle.x = 2048;
obstacle.y = Math.random() * 2732;
obstacles.push(obstacle);
game.addChild(obstacle);
}
// Handle touch events to make the bird flap
game.down = function (x, y, obj) {
bird.flap();
};
// Update game logic
game.update = function () {
bird.update();
// Update obstacles and check for collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
// Check if the bird hits an obstacle
if (bird.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Remove obstacles that are off-screen
if (obstacles[i].x < -obstacles[i].width) {
obstacles[i].destroy();
obstacles.splice(i, 1);
score++;
scoreTxt.setText(score);
}
}
// Create new obstacles periodically
if (LK.ticks % 120 == 0) {
createObstacle();
}
// Check if the bird hits the ground or flies too high
if (bird.y > 2732 || bird.y < 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};