/**** * Classes ****/ // Pipe class to represent 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; }; }); // Assets will be automatically created and loaded by the LK engine based on their usage in the code. // SpongeBob class to represent the player character var SpongeBob = Container.expand(function () { var self = Container.call(this); var spongebobGraphics = self.attachAsset('spongebob', { anchorX: 0.5, anchorY: 0.5 }); self.velocity = 0; self.gravity = 0.5; self.flapStrength = -10; self.flap = function () { self.velocity = self.flapStrength; }; self.update = function () { self.velocity += self.gravity; self.y += self.velocity; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ var spongebob = game.addChild(new SpongeBob()); spongebob.x = 2048 / 4; spongebob.y = 2732 / 2; var pipes = []; var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); function spawnPipe() { var pipe = new Pipe(); pipe.x = 2048; pipe.y = Math.random() * (2732 - 400) + 200; // Random y position pipes.push(pipe); game.addChild(pipe); } game.down = function (x, y, obj) { spongebob.flap(); }; game.update = function () { spongebob.update(); for (var i = pipes.length - 1; i >= 0; i--) { pipes[i].update(); if (pipes[i].x < -100) { pipes[i].destroy(); pipes.splice(i, 1); score++; scoreTxt.setText(score); } if (spongebob.intersects(pipes[i])) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } } if (LK.ticks % 90 == 0) { spawnPipe(); } if (spongebob.y > 2732 || spongebob.y < 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); } };
/****
* Classes
****/
// Pipe class to represent 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;
};
});
// Assets will be automatically created and loaded by the LK engine based on their usage in the code.
// SpongeBob class to represent the player character
var SpongeBob = Container.expand(function () {
var self = Container.call(this);
var spongebobGraphics = self.attachAsset('spongebob', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocity = 0;
self.gravity = 0.5;
self.flapStrength = -10;
self.flap = function () {
self.velocity = self.flapStrength;
};
self.update = function () {
self.velocity += self.gravity;
self.y += self.velocity;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
var spongebob = game.addChild(new SpongeBob());
spongebob.x = 2048 / 4;
spongebob.y = 2732 / 2;
var pipes = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function spawnPipe() {
var pipe = new Pipe();
pipe.x = 2048;
pipe.y = Math.random() * (2732 - 400) + 200; // Random y position
pipes.push(pipe);
game.addChild(pipe);
}
game.down = function (x, y, obj) {
spongebob.flap();
};
game.update = function () {
spongebob.update();
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].update();
if (pipes[i].x < -100) {
pipes[i].destroy();
pipes.splice(i, 1);
score++;
scoreTxt.setText(score);
}
if (spongebob.intersects(pipes[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
if (LK.ticks % 90 == 0) {
spawnPipe();
}
if (spongebob.y > 2732 || spongebob.y < 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
};