/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0,
anchorY: 0
});
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.isJumping = false;
self.isDead = false;
self.velocity = 0;
self.gravity = 0.7;
self.jumpForce = -18;
self.groundY = 0;
self.jump = function () {
if (!self.isJumping && !self.isDead) {
self.velocity = self.jumpForce;
self.isJumping = true;
LK.getSound('jump').play();
}
};
self.update = function () {
if (self.isDead) {
return;
}
self.velocity += self.gravity;
self.y += self.velocity;
// Check if player has landed
if (self.y >= self.groundY) {
self.y = self.groundY;
self.velocity = 0;
self.isJumping = false;
}
};
self.die = function () {
if (!self.isDead) {
self.isDead = true;
LK.getSound('death').play();
LK.effects.flashObject(self, 0xff0000, 500);
tween(self, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
LK.effects.flashScreen(0xff0000, 500);
LK.showGameOver();
}
});
}
};
return self;
});
var Spike = Container.expand(function () {
var self = Container.call(this);
var spikeGraphics = self.attachAsset('spike', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = 8;
self.active = true;
self.update = function () {
if (!self.active) {
return;
}
self.x -= self.speed;
// Remove if off screen
if (self.x < -50) {
self.active = false;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var player;
var ground;
var spikes = [];
var nextSpawnTime = 0;
var minSpawnInterval = 60;
var maxSpawnInterval = 120;
var score = 0;
var increaseDifficultyAt = 10;
var lastIncreasedAt = 0;
var gameStarted = false;
var spikePool = [];
// Create and position the score text
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 50;
// Create instruction text
var instructionTxt = new Text2('Tap anywhere to jump', {
size: 60,
fill: 0xFFFFFF
});
instructionTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(instructionTxt);
// Initialize the game
function initGame() {
// Reset game variables
score = 0;
LK.setScore(0);
scoreTxt.setText('0');
nextSpawnTime = 60;
minSpawnInterval = 60;
maxSpawnInterval = 120;
increaseDifficultyAt = 10;
lastIncreasedAt = 0;
// Clear existing spikes
for (var i = 0; i < spikes.length; i++) {
if (spikes[i].parent) {
spikes[i].parent.removeChild(spikes[i]);
}
}
spikes = [];
// Create ground
if (ground && ground.parent) {
ground.parent.removeChild(ground);
}
ground = new Ground();
ground.y = 2732 - 50;
game.addChild(ground);
// Create player
if (player && player.parent) {
player.parent.removeChild(player);
}
player = new Player();
player.x = 300;
player.y = ground.y;
player.groundY = ground.y;
player.isDead = false;
player.alpha = 1;
game.addChild(player);
// Play background music
LK.playMusic('bgMusic');
}
// Get a spike from the pool or create a new one
function getSpike() {
for (var i = 0; i < spikePool.length; i++) {
if (!spikePool[i].active && !spikePool[i].parent) {
spikePool[i].active = true;
return spikePool[i];
}
}
var newSpike = new Spike();
spikePool.push(newSpike);
return newSpike;
}
// Spawn a new spike
function spawnSpike() {
var spike = getSpike();
spike.x = 2048 + 50;
spike.y = ground.y;
spike.active = true;
spike.speed = 8 + Math.floor(score / increaseDifficultyAt);
game.addChild(spike);
spikes.push(spike);
// Set next spawn time
nextSpawnTime = LK.ticks + Math.floor(Math.random() * (maxSpawnInterval - minSpawnInterval)) + minSpawnInterval;
}
// Check collisions between player and spikes
function checkCollisions() {
if (player.isDead) {
return;
}
for (var i = 0; i < spikes.length; i++) {
if (spikes[i].active && player.intersects(spikes[i])) {
player.die();
return;
}
}
}
// Update spikes
function updateSpikes() {
for (var i = spikes.length - 1; i >= 0; i--) {
spikes[i].update();
// Remove inactive spikes from the array
if (!spikes[i].active) {
if (spikes[i].parent) {
spikes[i].parent.removeChild(spikes[i]);
}
spikes.splice(i, 1);
// Increase score when player passes a spike
score++;
LK.setScore(score);
scoreTxt.setText(score.toString());
}
}
}
// Increase game difficulty
function increaseDifficulty() {
if (score > 0 && score % increaseDifficultyAt === 0 && lastIncreasedAt !== score) {
lastIncreasedAt = score;
// Decrease spawn interval to make the game harder
if (minSpawnInterval > 30) {
minSpawnInterval -= 5;
}
if (maxSpawnInterval > 60) {
maxSpawnInterval -= 5;
}
}
}
// Handle game input
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
instructionTxt.parent.removeChild(instructionTxt);
initGame();
} else {
player.jump();
}
};
// Game update loop
game.update = function () {
if (!gameStarted) {
return;
}
// Update player
player.update();
// Spawn new spikes
if (LK.ticks >= nextSpawnTime) {
spawnSpike();
}
// Update spikes
updateSpikes();
// Check for collisions
checkCollisions();
// Increase difficulty
increaseDifficulty();
};
// Initialize game on first load
LK.setTimeout(function () {
// Keep instruction text until player starts
gameStarted = false;
}, 100); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Ground = Container.expand(function () {
var self = Container.call(this);
var groundGraphics = self.attachAsset('ground', {
anchorX: 0,
anchorY: 0
});
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 1.0
});
self.isJumping = false;
self.isDead = false;
self.velocity = 0;
self.gravity = 0.7;
self.jumpForce = -18;
self.groundY = 0;
self.jump = function () {
if (!self.isJumping && !self.isDead) {
self.velocity = self.jumpForce;
self.isJumping = true;
LK.getSound('jump').play();
}
};
self.update = function () {
if (self.isDead) {
return;
}
self.velocity += self.gravity;
self.y += self.velocity;
// Check if player has landed
if (self.y >= self.groundY) {
self.y = self.groundY;
self.velocity = 0;
self.isJumping = false;
}
};
self.die = function () {
if (!self.isDead) {
self.isDead = true;
LK.getSound('death').play();
LK.effects.flashObject(self, 0xff0000, 500);
tween(self, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
LK.effects.flashScreen(0xff0000, 500);
LK.showGameOver();
}
});
}
};
return self;
});
var Spike = Container.expand(function () {
var self = Container.call(this);
var spikeGraphics = self.attachAsset('spike', {
anchorX: 0.5,
anchorY: 1.0
});
self.speed = 8;
self.active = true;
self.update = function () {
if (!self.active) {
return;
}
self.x -= self.speed;
// Remove if off screen
if (self.x < -50) {
self.active = false;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
var player;
var ground;
var spikes = [];
var nextSpawnTime = 0;
var minSpawnInterval = 60;
var maxSpawnInterval = 120;
var score = 0;
var increaseDifficultyAt = 10;
var lastIncreasedAt = 0;
var gameStarted = false;
var spikePool = [];
// Create and position the score text
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 50;
// Create instruction text
var instructionTxt = new Text2('Tap anywhere to jump', {
size: 60,
fill: 0xFFFFFF
});
instructionTxt.anchor.set(0.5, 0.5);
LK.gui.center.addChild(instructionTxt);
// Initialize the game
function initGame() {
// Reset game variables
score = 0;
LK.setScore(0);
scoreTxt.setText('0');
nextSpawnTime = 60;
minSpawnInterval = 60;
maxSpawnInterval = 120;
increaseDifficultyAt = 10;
lastIncreasedAt = 0;
// Clear existing spikes
for (var i = 0; i < spikes.length; i++) {
if (spikes[i].parent) {
spikes[i].parent.removeChild(spikes[i]);
}
}
spikes = [];
// Create ground
if (ground && ground.parent) {
ground.parent.removeChild(ground);
}
ground = new Ground();
ground.y = 2732 - 50;
game.addChild(ground);
// Create player
if (player && player.parent) {
player.parent.removeChild(player);
}
player = new Player();
player.x = 300;
player.y = ground.y;
player.groundY = ground.y;
player.isDead = false;
player.alpha = 1;
game.addChild(player);
// Play background music
LK.playMusic('bgMusic');
}
// Get a spike from the pool or create a new one
function getSpike() {
for (var i = 0; i < spikePool.length; i++) {
if (!spikePool[i].active && !spikePool[i].parent) {
spikePool[i].active = true;
return spikePool[i];
}
}
var newSpike = new Spike();
spikePool.push(newSpike);
return newSpike;
}
// Spawn a new spike
function spawnSpike() {
var spike = getSpike();
spike.x = 2048 + 50;
spike.y = ground.y;
spike.active = true;
spike.speed = 8 + Math.floor(score / increaseDifficultyAt);
game.addChild(spike);
spikes.push(spike);
// Set next spawn time
nextSpawnTime = LK.ticks + Math.floor(Math.random() * (maxSpawnInterval - minSpawnInterval)) + minSpawnInterval;
}
// Check collisions between player and spikes
function checkCollisions() {
if (player.isDead) {
return;
}
for (var i = 0; i < spikes.length; i++) {
if (spikes[i].active && player.intersects(spikes[i])) {
player.die();
return;
}
}
}
// Update spikes
function updateSpikes() {
for (var i = spikes.length - 1; i >= 0; i--) {
spikes[i].update();
// Remove inactive spikes from the array
if (!spikes[i].active) {
if (spikes[i].parent) {
spikes[i].parent.removeChild(spikes[i]);
}
spikes.splice(i, 1);
// Increase score when player passes a spike
score++;
LK.setScore(score);
scoreTxt.setText(score.toString());
}
}
}
// Increase game difficulty
function increaseDifficulty() {
if (score > 0 && score % increaseDifficultyAt === 0 && lastIncreasedAt !== score) {
lastIncreasedAt = score;
// Decrease spawn interval to make the game harder
if (minSpawnInterval > 30) {
minSpawnInterval -= 5;
}
if (maxSpawnInterval > 60) {
maxSpawnInterval -= 5;
}
}
}
// Handle game input
game.down = function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
instructionTxt.parent.removeChild(instructionTxt);
initGame();
} else {
player.jump();
}
};
// Game update loop
game.update = function () {
if (!gameStarted) {
return;
}
// Update player
player.update();
// Spawn new spikes
if (LK.ticks >= nextSpawnTime) {
spawnSpike();
}
// Update spikes
updateSpikes();
// Check for collisions
checkCollisions();
// Increase difficulty
increaseDifficulty();
};
// Initialize game on first load
LK.setTimeout(function () {
// Keep instruction text until player starts
gameStarted = false;
}, 100);