/**** * 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.3; self.lift = -15; self.update = function () { self.velocityY += self.gravity; self.y += self.velocityY; 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: 0xD2B48C // Light brown 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(); LK.getSound('Tik').play(); }; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: 0xFFFFFF }); LK.gui.top.addChild(scoreTxt); game.update = function () { bird.update(); // Generate obstacles at intervals if (ticks % obstacleInterval === 0) { var gap = -1200; // 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 = 0; // Connect the top obstacle to the top of the screen topObstacle.height = topObstacleHeight; // Set the height of the top obstacle obstacles.push(topObstacle); game.addChild(topObstacle); var bottomObstacle = new Obstacle(); bottomObstacle.x = 2048; bottomObstacle.y = 2732; // Connect the bottom obstacle to the bottom of the screen bottomObstacle.height = bottomObstacleHeight; // Set the height of the bottom obstacle 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) || bird.y <= 0 || bird.y >= 2732) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(score); return; } if (obstacle.x < bird.x && !obstacle.passed && obstacle.y == 0) { obstacle.passed = true; score += 1; scoreTxt.setText(score.toString()); } 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.3;
self.lift = -15;
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY;
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: 0xD2B48C // Light brown 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();
LK.getSound('Tik').play();
};
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
LK.gui.top.addChild(scoreTxt);
game.update = function () {
bird.update();
// Generate obstacles at intervals
if (ticks % obstacleInterval === 0) {
var gap = -1200; // 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 = 0; // Connect the top obstacle to the top of the screen
topObstacle.height = topObstacleHeight; // Set the height of the top obstacle
obstacles.push(topObstacle);
game.addChild(topObstacle);
var bottomObstacle = new Obstacle();
bottomObstacle.x = 2048;
bottomObstacle.y = 2732; // Connect the bottom obstacle to the bottom of the screen
bottomObstacle.height = bottomObstacleHeight; // Set the height of the bottom obstacle
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) || bird.y <= 0 || bird.y >= 2732) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver(score);
return;
}
if (obstacle.x < bird.x && !obstacle.passed && obstacle.y == 0) {
obstacle.passed = true;
score += 1;
scoreTxt.setText(score.toString());
}
if (obstacle.x < -100) {
obstacles.splice(i, 1);
}
}
ticks++;
};