/****
* Classes
****/
// The assets will be automatically created and loaded by the LK engine.
// Create a Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.8,
scaleY: 2.8
});
self.speed = 0;
self.gravity = 1.5;
self.lift = -35;
self.update = function () {
self.speed += self.gravity;
self.speed *= 0.9; // Add air resistance to smooth movement
self.y += self.speed;
if (self.y > 2632) {
// Adjusted to simulate ground level
self.y = 2632;
self.speed = 0;
}
if (self.y < 0) {
self.y = 0;
self.speed = 0;
}
};
self.up = function () {
self.speed += self.lift;
};
});
// Create a Ground class
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.0,
scaleY: 0.5 // Scale to make it a thin ground
});
self.y = 2632; // Position at the bottom of the screen
});
// Create a LongPole class
var LongPole = Container.expand(function () {
var self = Container.call(this);
var poleGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1.0,
// Anchor at the bottom
scaleY: Math.random() * 5 + 5 // Randomize the height to make the pipe uneven
});
self.speed = 4; // Increase speed to make the game faster
self.update = function () {
self.x -= self.speed;
};
});
// Create an UpperPipe class
var UpperPipe = Container.expand(function () {
var self = Container.call(this);
var pipeGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.0,
// Anchor at the top
scaleY: Math.random() * 5 + 5 // Randomize the height to make the pipe uneven
});
self.speed = 4; // Increase speed to make the game faster
self.update = function () {
self.x -= self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb // Sky blue background
});
/****
* Game Code
****/
var ground = game.addChild(new Ground());
var bird = game.addChild(new Bird());
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
bird.x = 512;
bird.y = 1366;
var obstacles = [];
game.update = function () {
if (LK.ticks % 120 == 0) {
var longPole = new LongPole();
longPole.x = 2048;
longPole.y = 2732 + 100; // Decrease gap by adjusting y-position
obstacles.push(longPole);
game.addChild(longPole);
var upperPipe = new UpperPipe();
upperPipe.x = 2048;
upperPipe.y = 0; // Position at the top of the screen
obstacles.push(upperPipe);
game.addChild(upperPipe);
}
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].x + obstacles[i].width < bird.x && !obstacles[i].scored) {
score += 1;
scoreTxt.setText(score);
obstacles[i].scored = true;
}
if (obstacles[i].x < -obstacles[i].width) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
if (bird.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};
game.down = function (x, y, obj) {
bird.up();
}; /****
* Classes
****/
// The assets will be automatically created and loaded by the LK engine.
// Create a Bird class
var Bird = Container.expand(function () {
var self = Container.call(this);
var birdGraphics = self.attachAsset('bird', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.8,
scaleY: 2.8
});
self.speed = 0;
self.gravity = 1.5;
self.lift = -35;
self.update = function () {
self.speed += self.gravity;
self.speed *= 0.9; // Add air resistance to smooth movement
self.y += self.speed;
if (self.y > 2632) {
// Adjusted to simulate ground level
self.y = 2632;
self.speed = 0;
}
if (self.y < 0) {
self.y = 0;
self.speed = 0;
}
};
self.up = function () {
self.speed += self.lift;
};
});
// Create a Ground class
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.0,
scaleY: 0.5 // Scale to make it a thin ground
});
self.y = 2632; // Position at the bottom of the screen
});
// Create a LongPole class
var LongPole = Container.expand(function () {
var self = Container.call(this);
var poleGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 1.0,
// Anchor at the bottom
scaleY: Math.random() * 5 + 5 // Randomize the height to make the pipe uneven
});
self.speed = 4; // Increase speed to make the game faster
self.update = function () {
self.x -= self.speed;
};
});
// Create an UpperPipe class
var UpperPipe = Container.expand(function () {
var self = Container.call(this);
var pipeGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.0,
// Anchor at the top
scaleY: Math.random() * 5 + 5 // Randomize the height to make the pipe uneven
});
self.speed = 4; // Increase speed to make the game faster
self.update = function () {
self.x -= self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb // Sky blue background
});
/****
* Game Code
****/
var ground = game.addChild(new Ground());
var bird = game.addChild(new Bird());
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
bird.x = 512;
bird.y = 1366;
var obstacles = [];
game.update = function () {
if (LK.ticks % 120 == 0) {
var longPole = new LongPole();
longPole.x = 2048;
longPole.y = 2732 + 100; // Decrease gap by adjusting y-position
obstacles.push(longPole);
game.addChild(longPole);
var upperPipe = new UpperPipe();
upperPipe.x = 2048;
upperPipe.y = 0; // Position at the top of the screen
obstacles.push(upperPipe);
game.addChild(upperPipe);
}
for (var i = obstacles.length - 1; i >= 0; i--) {
if (obstacles[i].x + obstacles[i].width < bird.x && !obstacles[i].scored) {
score += 1;
scoreTxt.setText(score);
obstacles[i].scored = true;
}
if (obstacles[i].x < -obstacles[i].width) {
obstacles[i].destroy();
obstacles.splice(i, 1);
}
if (bird.intersects(obstacles[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
};
game.down = function (x, y, obj) {
bird.up();
};