/**** * Classes ****/ // Assets will be automatically created and loaded by the LK engine based on their usage in the code. // Define the Bird class 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.5; self.flapStrength = -10; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; // Check for collision with the top and bottom of the screen if (self.y < 0) { self.y = 0; self.velocity = 0; } else if (self.y > 2732) { self.y = 2732; self.velocity = 0; } }; self.flap = function () { self.velocity = self.flapStrength; }; }); // Define the Wall class var Wall = Container.expand(function () { var self = Container.call(this); var wallGraphics = self.attachAsset('wall', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; // Reset wall position if it goes off screen if (self.x < -100) { self.x = 2048 + 100; self.y = Math.random() * 2000 + 366; // Randomize y position } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize game variables var bird = game.addChild(new Bird()); bird.x = 2048 / 4; bird.y = 2732 / 2; var walls = []; for (var i = 0; i < 3; i++) { var wall = new Wall(); wall.x = 2048 + i * 800; wall.y = Math.random() * 2000 + 366; walls.push(wall); game.addChild(wall); } var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Handle game touch events game.down = function (x, y, obj) { bird.flap(); }; // Update game state game.update = function () { bird.update(); for (var i = 0; i < walls.length; i++) { walls[i].update(); // Check for collision between bird and walls if (bird.intersects(walls[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } // Update score if (LK.ticks % 60 == 0) { score++; scoreTxt.setText(score); } };
/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Define the Bird class
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.5;
self.flapStrength = -10;
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
// Check for collision with the top and bottom of the screen
if (self.y < 0) {
self.y = 0;
self.velocity = 0;
} else if (self.y > 2732) {
self.y = 2732;
self.velocity = 0;
}
};
self.flap = function () {
self.velocity = self.flapStrength;
};
});
// Define the Wall class
var Wall = Container.expand(function () {
var self = Container.call(this);
var wallGraphics = self.attachAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
// Reset wall position if it goes off screen
if (self.x < -100) {
self.x = 2048 + 100;
self.y = Math.random() * 2000 + 366; // Randomize y position
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize game variables
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
var walls = [];
for (var i = 0; i < 3; i++) {
var wall = new Wall();
wall.x = 2048 + i * 800;
wall.y = Math.random() * 2000 + 366;
walls.push(wall);
game.addChild(wall);
}
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Handle game touch events
game.down = function (x, y, obj) {
bird.flap();
};
// Update game state
game.update = function () {
bird.update();
for (var i = 0; i < walls.length; i++) {
walls[i].update();
// Check for collision between bird and walls
if (bird.intersects(walls[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Update score
if (LK.ticks % 60 == 0) {
score++;
scoreTxt.setText(score);
}
};