/**** * Classes ****/ // The assets will be automatically created and loaded by the LK engine based on their usage in the code. // For this game, we will use a bird and pole shapes, and a background image for the forest. // 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.velocity = 0; self.gravity = 0.5; self.flapStrength = -8; // Update function to apply gravity and move the bird self.update = function () { self.velocity += self.gravity; self.y += self.velocity; }; // Function to make the bird flap self.flap = function () { self.velocity = self.flapStrength; }; }); // Pole class to represent the obstacles var Pole = Container.expand(function () { var self = Container.call(this); var poleGraphics = self.attachAsset('pole', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; // Update function to move the pole self.update = function () { self.x += self.speed; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Set a sky blue background color }); /**** * Game Code ****/ // Initialize game variables var bird = game.addChild(new Bird()); bird.x = 2048 / 4; bird.y = 2732 / 2; var poles = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to create a new pair of poles function createPoles() { var gap = 400; var poleHeight = Math.random() * 800 + 400; var topPole = new Pole(); topPole.x = 2048; topPole.y = poleHeight - 2732; poles.push(topPole); game.addChild(topPole); var bottomPole = new Pole(); bottomPole.x = 2048; bottomPole.y = poleHeight + gap; poles.push(bottomPole); game.addChild(bottomPole); } // Handle screen tap to make the bird flap game.down = function (x, y, obj) { bird.flap(); }; // Update game logic game.update = function () { bird.update(); // Check for collision with ground or ceiling if (bird.y > 2732 || bird.y < 0) { LK.showGameOver(); } // Update poles and check for collisions for (var i = poles.length - 1; i >= 0; i--) { poles[i].update(); // Check for collision with bird if (bird.intersects(poles[i])) { LK.showGameOver(); } // Remove poles that have moved off screen if (poles[i].x < -100) { poles[i].destroy(); poles.splice(i, 1); score++; scoreTxt.setText(score); } } // Create new poles periodically if (LK.ticks % 120 == 0) { createPoles(); } };
/****
* Classes
****/
// The assets will be automatically created and loaded by the LK engine based on their usage in the code.
// For this game, we will use a bird and pole shapes, and a background image for the forest.
// 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.velocity = 0;
self.gravity = 0.5;
self.flapStrength = -8;
// Update function to apply gravity and move the bird
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
};
// Function to make the bird flap
self.flap = function () {
self.velocity = self.flapStrength;
};
});
// Pole class to represent the obstacles
var Pole = Container.expand(function () {
var self = Container.call(this);
var poleGraphics = self.attachAsset('pole', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
// Update function to move the pole
self.update = function () {
self.x += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Set a sky blue background color
});
/****
* Game Code
****/
// Initialize game variables
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
var poles = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to create a new pair of poles
function createPoles() {
var gap = 400;
var poleHeight = Math.random() * 800 + 400;
var topPole = new Pole();
topPole.x = 2048;
topPole.y = poleHeight - 2732;
poles.push(topPole);
game.addChild(topPole);
var bottomPole = new Pole();
bottomPole.x = 2048;
bottomPole.y = poleHeight + gap;
poles.push(bottomPole);
game.addChild(bottomPole);
}
// Handle screen tap to make the bird flap
game.down = function (x, y, obj) {
bird.flap();
};
// Update game logic
game.update = function () {
bird.update();
// Check for collision with ground or ceiling
if (bird.y > 2732 || bird.y < 0) {
LK.showGameOver();
}
// Update poles and check for collisions
for (var i = poles.length - 1; i >= 0; i--) {
poles[i].update();
// Check for collision with bird
if (bird.intersects(poles[i])) {
LK.showGameOver();
}
// Remove poles that have moved off screen
if (poles[i].x < -100) {
poles[i].destroy();
poles.splice(i, 1);
score++;
scoreTxt.setText(score);
}
}
// Create new poles periodically
if (LK.ticks % 120 == 0) {
createPoles();
}
};