/**** * Classes ****/ // Assets will be automatically created and loaded by the LK engine based on their usage in the code. // For example, a bird asset will be created when we use LK.getAsset('bird'). // Bird class to represent the player-controlled bird var Bird = Container.expand(function () { var self = Container.call(this); var birdGraphics = self.attachAsset('bird', { anchorX: 0.5, anchorY: 0.5 }); self.speedY = 0; self.gravity = 0.5; self.flapStrength = -10; // Update method to apply gravity and move the bird self.update = function () { self.speedY += self.gravity; self.y += self.speedY; // Check for collision with the top and bottom of the screen if (self.y < 0) { self.y = 0; self.speedY = 0; } else if (self.y > 2732 - birdGraphics.height) { self.y = 2732 - birdGraphics.height; self.speedY = 0; } }; // Method to make the bird flap self.flap = function () { self.speedY = self.flapStrength; }; }); // Wall class to represent the walls the bird must avoid var Wall = Container.expand(function () { var self = Container.call(this); var wallGraphics = self.attachAsset('wall', { anchorX: 0.5, anchorY: 0.5 }); self.speedX = -5; // Update method to move the wall self.update = function () { self.x += self.speedX; // Remove the wall if it goes off screen if (self.x < -wallGraphics.width) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Light blue background to represent the sky }); /**** * Game Code ****/ // Initialize game variables var bird; var walls = []; var wallInterval; var score = 0; var scoreTxt; // Function to start the game function startGame() { // Create the bird and add it to the game bird = game.addChild(new Bird()); bird.x = 2048 / 4; bird.y = 2732 / 2; // Create score text scoreTxt = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Set interval to create walls wallInterval = LK.setInterval(createWall, 2000); } // Function to create a new wall function createWall() { var wall = new Wall(); wall.x = 2048; wall.y = Math.random() * (2732 - 400) + 200; // Random y position walls.push(wall); game.addChild(wall); } // Function to update the game state game.update = function () { bird.update(); // Update each wall for (var i = walls.length - 1; i >= 0; i--) { walls[i].update(); // Check for collision with the bird if (bird.intersects(walls[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } // Remove walls that are off screen if (walls[i].x < -walls[i].width) { walls[i].destroy(); walls.splice(i, 1); score++; scoreTxt.setText('Score: ' + score); } } }; // Event listener for touch or mouse down to make the bird flap game.down = function (x, y, obj) { bird.flap(); }; // Start the game startGame();
/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// For example, a bird asset will be created when we use LK.getAsset('bird').
// Bird class to represent the player-controlled bird
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedY = 0;
self.gravity = 0.5;
self.flapStrength = -10;
// Update method to apply gravity and move the bird
self.update = function () {
self.speedY += self.gravity;
self.y += self.speedY;
// Check for collision with the top and bottom of the screen
if (self.y < 0) {
self.y = 0;
self.speedY = 0;
} else if (self.y > 2732 - birdGraphics.height) {
self.y = 2732 - birdGraphics.height;
self.speedY = 0;
}
};
// Method to make the bird flap
self.flap = function () {
self.speedY = self.flapStrength;
};
});
// Wall class to represent the walls the bird must avoid
var Wall = Container.expand(function () {
var self = Container.call(this);
var wallGraphics = self.attachAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = -5;
// Update method to move the wall
self.update = function () {
self.x += self.speedX;
// Remove the wall if it goes off screen
if (self.x < -wallGraphics.width) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Light blue background to represent the sky
});
/****
* Game Code
****/
// Initialize game variables
var bird;
var walls = [];
var wallInterval;
var score = 0;
var scoreTxt;
// Function to start the game
function startGame() {
// Create the bird and add it to the game
bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
// Create score text
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Set interval to create walls
wallInterval = LK.setInterval(createWall, 2000);
}
// Function to create a new wall
function createWall() {
var wall = new Wall();
wall.x = 2048;
wall.y = Math.random() * (2732 - 400) + 200; // Random y position
walls.push(wall);
game.addChild(wall);
}
// Function to update the game state
game.update = function () {
bird.update();
// Update each wall
for (var i = walls.length - 1; i >= 0; i--) {
walls[i].update();
// Check for collision with the bird
if (bird.intersects(walls[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
// Remove walls that are off screen
if (walls[i].x < -walls[i].width) {
walls[i].destroy();
walls.splice(i, 1);
score++;
scoreTxt.setText('Score: ' + score);
}
}
};
// Event listener for touch or mouse down to make the bird flap
game.down = function (x, y, obj) {
bird.flap();
};
// Start the game
startGame();