/**** * Classes ****/ //<Assets used in the game will automatically appear here> // Flappy class representing the player character var Flappy = Container.expand(function () { var self = Container.call(this); var flappyGraphics = self.attachAsset('flappy', { anchorX: 0.5, anchorY: 0.5, scaleX: 2.0, scaleY: 2.0 }); self.gravity = 0.5; self.lift = -10; self.velocity = 0; self.jump = function () { self.velocity = self.lift; }; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; if (self.y > 2732 - flappyGraphics.height / 2) { self.y = 2732 - flappyGraphics.height / 2; self.velocity = 0; LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } if (self.y < flappyGraphics.height / 2) { self.y = flappyGraphics.height / 2; self.velocity = 0; } }; }); // Pipe class representing the obstacles var Pipe = Container.expand(function () { var self = Container.call(this); var pipeGraphics = self.attachAsset('pipe', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -5; self.update = function () { self.x += self.speed; if (self.x < -pipeGraphics.width / 2) { self.destroy(); } }; }); // Sky class representing the sky var Sky = Container.expand(function () { var self = Container.call(this); var skyGraphics = self.attachAsset('sky', { anchorX: 0.5, anchorY: 0.5, scaleX: 2.0, scaleY: 2.0 }); self.update = function () { // Sky does not need to update }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Init game with sky blue background }); /**** * Game Code ****/ var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); var sky = game.addChild(new Sky()); sky.x = 2048 / 2; sky.y = 2732 / 4; var flappy = game.addChild(new Flappy()); flappy.x = 2048 / 4; flappy.y = 2732 / 2; var pipes = []; var pipeInterval = 90; var frameCount = 0; game.down = function (x, y, obj) { flappy.jump(); }; game.update = function () { frameCount++; if (frameCount % pipeInterval === 0) { var pipeTop = new Pipe(); var pipeBottom = new Pipe(); var pipeGap = 400; var pipeHeight = Math.random() * (2732 - pipeGap - 200) + 100; pipeTop.x = 2048; pipeTop.y = pipeHeight / 2; pipeBottom.x = 2048; pipeBottom.y = pipeHeight + pipeGap + pipeHeight / 2; pipes.push(pipeTop); pipes.push(pipeBottom); game.addChild(pipeTop); game.addChild(pipeBottom); } // Removed duplicate tree update loop for (var i = pipes.length - 1; i >= 0; i--) { pipes[i].update(); if (pipes[i].intersects(flappy)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } scoreTxt.setText(Math.floor(LK.ticks / 60)); flappy.update(); if (flappy.y >= 2732 - flappy.height / 2) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } };
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Flappy class representing the player character
var Flappy = Container.expand(function () {
var self = Container.call(this);
var flappyGraphics = self.attachAsset('flappy', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.0,
scaleY: 2.0
});
self.gravity = 0.5;
self.lift = -10;
self.velocity = 0;
self.jump = function () {
self.velocity = self.lift;
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
if (self.y > 2732 - flappyGraphics.height / 2) {
self.y = 2732 - flappyGraphics.height / 2;
self.velocity = 0;
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (self.y < flappyGraphics.height / 2) {
self.y = flappyGraphics.height / 2;
self.velocity = 0;
}
};
});
// Pipe class representing the obstacles
var Pipe = Container.expand(function () {
var self = Container.call(this);
var pipeGraphics = self.attachAsset('pipe', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -pipeGraphics.width / 2) {
self.destroy();
}
};
});
// Sky class representing the sky
var Sky = Container.expand(function () {
var self = Container.call(this);
var skyGraphics = self.attachAsset('sky', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2.0,
scaleY: 2.0
});
self.update = function () {
// Sky does not need to update
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Init game with sky blue background
});
/****
* Game Code
****/
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var sky = game.addChild(new Sky());
sky.x = 2048 / 2;
sky.y = 2732 / 4;
var flappy = game.addChild(new Flappy());
flappy.x = 2048 / 4;
flappy.y = 2732 / 2;
var pipes = [];
var pipeInterval = 90;
var frameCount = 0;
game.down = function (x, y, obj) {
flappy.jump();
};
game.update = function () {
frameCount++;
if (frameCount % pipeInterval === 0) {
var pipeTop = new Pipe();
var pipeBottom = new Pipe();
var pipeGap = 400;
var pipeHeight = Math.random() * (2732 - pipeGap - 200) + 100;
pipeTop.x = 2048;
pipeTop.y = pipeHeight / 2;
pipeBottom.x = 2048;
pipeBottom.y = pipeHeight + pipeGap + pipeHeight / 2;
pipes.push(pipeTop);
pipes.push(pipeBottom);
game.addChild(pipeTop);
game.addChild(pipeBottom);
}
// Removed duplicate tree update loop
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (pipes[i].intersects(flappy)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
scoreTxt.setText(Math.floor(LK.ticks / 60));
flappy.update();
if (flappy.y >= 2732 - flappy.height / 2) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};