/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Define the Cube class
var Cube = Container.expand(function () {
var self = Container.call(this);
var cubeGraphics = self.attachAsset('cube', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.jumpSpeed = -20;
self.gravity = 1;
self.isJumping = false;
self.update = function () {
if (self.x < 2048 / 2) {
self.x += self.speed;
}
if (self.isJumping) {
self.y += self.jumpSpeed;
if (self.jumpSpeed < 0) {
// The cube is still going up, delay the gravity
self.jumpSpeed += self.gravity / 2;
} else {
// The cube is falling down, apply full gravity
self.jumpSpeed += self.gravity;
}
if (self.y >= 2732 / 2) {
// Ground level
self.y = 2732 / 2;
self.isJumping = false;
self.jumpSpeed = -20;
}
}
};
self.jump = function () {
if (!self.isJumping) {
self.isJumping = true;
}
};
});
// Define the Ground class
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0.5,
anchorY: 1,
height: 2732 - (2732 / 2 + 100)
});
});
// Define the PauseButton class
var PauseButton = Container.expand(function () {
var self = Container.call(this);
var pauseButtonGraphics = self.attachAsset('pauseButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Pause the game when the button is pressed
self.down = function (x, y, obj) {
game.paused = !game.paused;
};
};
});
// Define the Spike class
var Spike = Container.expand(function () {
var self = Container.call(this);
var spikeGraphics = self.attachAsset('spike', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -5;
self.update = function () {
self.x += self.speed;
if (self.x < -50) {
// Off screen
self.destroy();
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
var ground = game.addChild(new Ground());
ground.x = 2048 / 2;
ground.y = 2732;
var cube = game.addChild(new Cube());
cube.x = 100;
cube.y = 2732 / 2;
var spikes = [];
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Add the pause button to the game
var pauseButton = game.addChild(new PauseButton());
pauseButton.x = 2048 - 100; // Position the button at the top right corner
pauseButton.y = 100;
// Function to handle jump
game.down = function (x, y, obj) {
cube.jump();
};
// Function to spawn spikes
function spawnSpike() {
var spike = new Spike();
spike.x = 2048;
spike.y = 2732 / 2;
spikes.push(spike);
game.addChild(spike);
}
// Update function
game.update = function () {
if (game.paused) {
return;
}
cube.update();
for (var i = spikes.length - 1; i >= 0; i--) {
spikes[i].update();
if (cube.intersects(spikes[i])) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
if (LK.ticks % 60 == 0) {
// Spawn spike every second
spawnSpike();
}
score += 1;
scoreTxt.setText(score);
};
yellow cube with blue eyes. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
black spike. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
blue and black checkerboard patter led lights. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
pause button for endless runner game. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.