/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Bird class to represent the player's character
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;
};
self.flap = function () {
self.velocity = self.flapStrength;
};
});
// Column class to represent obstacles
var Column = Container.expand(function () {
var self = Container.call(this);
var columnGraphics = self.attachAsset('column', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var bird;
var columns = [];
var columnSpacing = 600;
var columnWidth = 200;
var columnGap = 400;
var score = 0;
var scoreTxt;
// Initialize game elements
function initGame() {
bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366; // Center vertically
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create initial columns
for (var i = 0; i < 3; i++) {
createColumn(2048 + i * columnSpacing);
}
}
// Create a new column pair
function createColumn(x) {
var gapPosition = Math.random() * (2732 - columnGap - 200) + 100;
var topColumn = game.addChild(new Column());
topColumn.x = x;
topColumn.y = gapPosition - columnGap / 2 - columnWidth / 2;
columns.push(topColumn);
var bottomColumn = game.addChild(new Column());
bottomColumn.x = x;
bottomColumn.y = gapPosition + columnGap / 2 + columnWidth / 2;
columns.push(bottomColumn);
}
// Handle game updates
game.update = function () {
bird.update();
// Check for collisions and update columns
for (var i = columns.length - 1; i >= 0; i--) {
columns[i].update();
// Check for collision with bird
if (bird.intersects(columns[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
// Remove columns that are off-screen
if (columns[i].x < -columnWidth) {
columns[i].destroy();
columns.splice(i, 1);
// Increase score and create new column
score++;
scoreTxt.setText(score);
createColumn(2048 + columnSpacing);
}
}
// Check if bird is out of bounds
if (bird.y < 0 || bird.y > 2732) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};
// Handle tap to flap
game.down = function (x, y, obj) {
bird.flap();
};
// Initialize the game
initGame(); /****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// Bird class to represent the player's character
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;
};
self.flap = function () {
self.velocity = self.flapStrength;
};
});
// Column class to represent obstacles
var Column = Container.expand(function () {
var self = Container.call(this);
var columnGraphics = self.attachAsset('column', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var bird;
var columns = [];
var columnSpacing = 600;
var columnWidth = 200;
var columnGap = 400;
var score = 0;
var scoreTxt;
// Initialize game elements
function initGame() {
bird = game.addChild(new Bird());
bird.x = 300;
bird.y = 1366; // Center vertically
scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create initial columns
for (var i = 0; i < 3; i++) {
createColumn(2048 + i * columnSpacing);
}
}
// Create a new column pair
function createColumn(x) {
var gapPosition = Math.random() * (2732 - columnGap - 200) + 100;
var topColumn = game.addChild(new Column());
topColumn.x = x;
topColumn.y = gapPosition - columnGap / 2 - columnWidth / 2;
columns.push(topColumn);
var bottomColumn = game.addChild(new Column());
bottomColumn.x = x;
bottomColumn.y = gapPosition + columnGap / 2 + columnWidth / 2;
columns.push(bottomColumn);
}
// Handle game updates
game.update = function () {
bird.update();
// Check for collisions and update columns
for (var i = columns.length - 1; i >= 0; i--) {
columns[i].update();
// Check for collision with bird
if (bird.intersects(columns[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
// Remove columns that are off-screen
if (columns[i].x < -columnWidth) {
columns[i].destroy();
columns.splice(i, 1);
// Increase score and create new column
score++;
scoreTxt.setText(score);
createColumn(2048 + columnSpacing);
}
}
// Check if bird is out of bounds
if (bird.y < 0 || bird.y > 2732) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};
// Handle tap to flap
game.down = function (x, y, obj) {
bird.flap();
};
// Initialize the game
initGame();