/**** * Classes ****/ //<Write imports for supported plugins here> // Bird class representing 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.velocityY = 0; self.gravity = 0.5; self.lift = -10; self.update = function () { self.velocityY += self.gravity; self.y += self.velocityY; // Prevent bird from going off-screen if (self.y < 0) { self.y = 0; self.velocityY = 0; } else if (self.y > 2732) { self.y = 2732; self.velocityY = 0; } }; self.flap = function () { self.velocityY = self.lift; }; }); // Obstacle class representing the obstacles var Obstacle = Container.expand(function () { var self = Container.call(this); var obstacleGraphics = self.attachAsset('obstacle', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; // Remove obstacle if it goes off-screen if (self.x < -100) { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb // Sky blue background }); /**** * Game Code ****/ //<Assets used in the game will automatically appear here> var bird = game.addChild(new Bird()); bird.x = 2048 / 2; bird.y = 2732 / 2; var obstacles = []; var obstacleInterval = 100; // Interval for obstacle generation var ticks = 0; game.down = function (x, y, obj) { bird.flap(); }; game.update = function () { bird.update(); // Generate obstacles at intervals if (ticks % obstacleInterval === 0) { var gap = 300; // Gap between top and bottom obstacles var topObstacleHeight = Math.random() * (2732 - gap); var bottomObstacleHeight = 2732 - topObstacleHeight - gap; var topObstacle = new Obstacle(); topObstacle.x = 2048; topObstacle.y = topObstacleHeight / 2; obstacles.push(topObstacle); game.addChild(topObstacle); var bottomObstacle = new Obstacle(); bottomObstacle.x = 2048; bottomObstacle.y = 2732 - bottomObstacleHeight / 2; obstacles.push(bottomObstacle); game.addChild(bottomObstacle); } // Update obstacles and check for collisions for (var i = obstacles.length - 1; i >= 0; i--) { var obstacle = obstacles[i]; obstacle.update(); if (bird.intersects(obstacle)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } if (obstacle.x < -100) { obstacles.splice(i, 1); } } ticks++; };
/****
* Classes
****/
//<Write imports for supported plugins here>
// Bird class representing 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.velocityY = 0;
self.gravity = 0.5;
self.lift = -10;
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY;
// Prevent bird from going off-screen
if (self.y < 0) {
self.y = 0;
self.velocityY = 0;
} else if (self.y > 2732) {
self.y = 2732;
self.velocityY = 0;
}
};
self.flap = function () {
self.velocityY = self.lift;
};
});
// Obstacle class representing the obstacles
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('obstacle', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
// Remove obstacle if it goes off-screen
if (self.x < -100) {
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb // Sky blue background
});
/****
* Game Code
****/
//<Assets used in the game will automatically appear here>
var bird = game.addChild(new Bird());
bird.x = 2048 / 2;
bird.y = 2732 / 2;
var obstacles = [];
var obstacleInterval = 100; // Interval for obstacle generation
var ticks = 0;
game.down = function (x, y, obj) {
bird.flap();
};
game.update = function () {
bird.update();
// Generate obstacles at intervals
if (ticks % obstacleInterval === 0) {
var gap = 300; // Gap between top and bottom obstacles
var topObstacleHeight = Math.random() * (2732 - gap);
var bottomObstacleHeight = 2732 - topObstacleHeight - gap;
var topObstacle = new Obstacle();
topObstacle.x = 2048;
topObstacle.y = topObstacleHeight / 2;
obstacles.push(topObstacle);
game.addChild(topObstacle);
var bottomObstacle = new Obstacle();
bottomObstacle.x = 2048;
bottomObstacle.y = 2732 - bottomObstacleHeight / 2;
obstacles.push(bottomObstacle);
game.addChild(bottomObstacle);
}
// Update obstacles and check for collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
obstacle.update();
if (bird.intersects(obstacle)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
if (obstacle.x < -100) {
obstacles.splice(i, 1);
}
}
ticks++;
};