/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Bird class to represent the player character
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.flapStrength = -10;
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY;
// Prevent bird from going off-screen
if (self.y > 2732 - birdGraphics.height / 2) {
self.y = 2732 - birdGraphics.height / 2;
self.velocityY = 0;
}
if (self.y < birdGraphics.height / 2) {
self.y = birdGraphics.height / 2;
self.velocityY = 0;
}
};
self.flap = function () {
self.velocityY = self.flapStrength;
};
});
// Obstacle class to represent the pipes
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
var bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.speed = -5;
self.gap = 400;
self.update = function () {
self.x += self.speed;
// Reset position if off-screen
if (self.x < -topPipe.width) {
self.x = 2048 + topPipe.width;
self.setRandomGap();
}
};
self.setRandomGap = function () {
var centerY = Math.random() * (2732 - self.gap) + self.gap / 2;
topPipe.y = centerY - self.gap / 2;
bottomPipe.y = centerY + self.gap / 2;
};
self.setRandomGap();
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Initialize bird
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
// Initialize obstacles
var obstacles = [];
for (var i = 0; i < 3; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = 2048 + i * 800;
obstacles.push(obstacle);
}
// Handle game tap to make the bird flap
game.down = function (x, y, obj) {
bird.flap();
};
// Update game logic
game.update = function () {
bird.update();
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
// Check for collision
if (bird.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
}; /****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Bird class to represent the player character
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.flapStrength = -10;
self.update = function () {
self.velocityY += self.gravity;
self.y += self.velocityY;
// Prevent bird from going off-screen
if (self.y > 2732 - birdGraphics.height / 2) {
self.y = 2732 - birdGraphics.height / 2;
self.velocityY = 0;
}
if (self.y < birdGraphics.height / 2) {
self.y = birdGraphics.height / 2;
self.velocityY = 0;
}
};
self.flap = function () {
self.velocityY = self.flapStrength;
};
});
// Obstacle class to represent the pipes
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var topPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1
});
var bottomPipe = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0
});
self.speed = -5;
self.gap = 400;
self.update = function () {
self.x += self.speed;
// Reset position if off-screen
if (self.x < -topPipe.width) {
self.x = 2048 + topPipe.width;
self.setRandomGap();
}
};
self.setRandomGap = function () {
var centerY = Math.random() * (2732 - self.gap) + self.gap / 2;
topPipe.y = centerY - self.gap / 2;
bottomPipe.y = centerY + self.gap / 2;
};
self.setRandomGap();
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Initialize bird
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
// Initialize obstacles
var obstacles = [];
for (var i = 0; i < 3; i++) {
var obstacle = game.addChild(new Obstacle());
obstacle.x = 2048 + i * 800;
obstacles.push(obstacle);
}
// Handle game tap to make the bird flap
game.down = function (x, y, obj) {
bird.flap();
};
// Update game logic
game.update = function () {
bird.update();
for (var i = 0; i < obstacles.length; i++) {
obstacles[i].update();
// Check for collision
if (bird.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};