/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
// Define the Button class
var Button = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
return self;
});
// Define the Obstacle class
var Obstacle = Container.expand(function () {
var self = Container.call(this);
var obstacleGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5; // Initial speed
// Update method for obstacle movement
self.update = function () {
self.x -= self.speed;
};
return self;
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define the Player class
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
self.layer = 1; // Start on the middle layer
self.speed = 10; // Initial speed
// Method to move the player up a layer
self.moveUp = function () {
if (self.layer > 0) {
LK.getSound('jump').play();
self.layer--;
tween(self, {
y: self.y - 450
}, {
duration: 300,
easing: tween.easeInOut
});
}
};
// Method to move the player down a layer
self.moveDown = function () {
if (self.layer < 2) {
LK.getSound('jump').play();
self.layer++;
tween(self, {
y: self.y + 450
}, {
duration: 300,
easing: tween.easeInOut
});
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0000FF //Init game with blue background
});
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0.0,
anchorY: 0.0
});
background.x = 0;
background.y = 0;
// Play 'macera' music track in a loop
LK.playMusic('macera');
var player = new Player();
player.x = 200;
player.y = 1366 - 450; // Start on the middle layer
game.addChild(player);
// Add squares to each layer
var layers = [-450, 0, 450];
layers.forEach(function (layer) {
var square = LK.getAsset('box1', {
anchorX: 0.5,
anchorY: 0.5
});
square.x = 1024; // Center horizontally
square.y = 1366 - 300 + layer; // Position based on layer, moved further down
game.addChild(square);
});
// Create a variable to keep track of the current character asset
var characterAssets = ['character', 'c2', 'c3', 'c4'];
var currentCharacterAsset = 0;
// Create a timer to change the character asset every 0.2 seconds
LK.setInterval(function () {
// Remove the current character asset
player.removeChild(player.children[0]);
// Update the current character asset index
currentCharacterAsset = (currentCharacterAsset + 1) % characterAssets.length;
// Attach the new character asset
player.attachAsset(characterAssets[currentCharacterAsset], {
anchorX: 0.5,
anchorY: 0.5
});
}, 200);
// Initialize buttons
var buttonUp = new Button();
buttonUp.x = 1024;
buttonUp.y = 2732 - 600; // Position at the bottom of the screen
buttonUp.scale.set(2); // Increase the size of the button
game.addChild(buttonUp);
var buttonDown = new Button();
buttonDown.x = 1024;
buttonDown.y = 2732 - 300; // Position at the bottom of the screen
buttonDown.scale.set(2); // Increase the size of the button
buttonDown.rotation = Math.PI; // Rotate the button by 180 degrees
game.addChild(buttonDown);
// Initialize obstacles array
var obstacles = [];
// Initialize score
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Display leaderboard
var playerHighScore = storage.playerHighScore || 0;
var leaderboardTxt = new Text2('Your High Score: ' + playerHighScore, {
size: 120,
// Reduced font size from 160 to 120
fill: 0xFFFFFF
});
leaderboardTxt.anchor.set(0.5, 0);
leaderboardTxt.x = (buttonUp.x + buttonDown.x) / 2; // Center horizontally between the buttons
leaderboardTxt.y = (buttonUp.y + buttonDown.y) / 2 - 550; // Move slightly more upwards
game.addChild(leaderboardTxt);
// Function to create obstacles
function createObstacle() {
var lanes = [-450, 0, 450];
var selectedLanes = [lanes[Math.floor(Math.random() * lanes.length)]];
// 30% chance to spawn a second obstacle in a different lane
if (Math.random() < 0.3) {
var secondLane;
do {
secondLane = lanes[Math.floor(Math.random() * lanes.length)];
} while (selectedLanes.includes(secondLane));
selectedLanes.push(secondLane);
}
selectedLanes.forEach(function (lane) {
var obstacle = new Obstacle();
obstacle.x = 2048;
obstacle.y = 1366 - 350 + lane;
obstacles.push(obstacle);
game.addChild(obstacle);
});
}
// Function to update game speed
function updateSpeed() {
var speedIncrease = score * 0.01; // Increase speed based on score
player.speed += speedIncrease;
obstacles.forEach(function (obstacle) {
obstacle.speed += speedIncrease;
});
}
// Handle swipe up and down
game.move = function (x, y, obj) {
if (obj.event && obj.event.deltaY < 0) {
player.moveUp();
} else if (obj.event && obj.event.deltaY > 0) {
player.moveDown();
}
};
// Handle button presses
buttonUp.down = function (x, y, obj) {
if (!buttonUp.cooldown && !buttonDown.cooldown) {
player.moveUp();
buttonUp.cooldown = true;
buttonDown.cooldown = true;
LK.setTimeout(function () {
buttonUp.cooldown = false;
buttonDown.cooldown = false;
}, 310);
}
};
buttonDown.down = function (x, y, obj) {
if (!buttonUp.cooldown && !buttonDown.cooldown) {
player.moveDown();
buttonUp.cooldown = true;
buttonDown.cooldown = true;
LK.setTimeout(function () {
buttonUp.cooldown = false;
buttonDown.cooldown = false;
}, 310);
}
};
// Game update loop
game.update = function () {
// Update obstacles
for (var i = obstacles.length - 1; i >= 0; i--) {
obstacles[i].update();
if (obstacles[i].x < -100) {
obstacles[i].destroy();
obstacles.splice(i, 1);
score++;
scoreTxt.setText(score);
// Save the score to the leaderboard
storage.leaderboard = storage.leaderboard || [];
storage.leaderboard.push(score);
storage.leaderboard.sort(function (a, b) {
return b - a;
});
storage.leaderboard = storage.leaderboard.slice(0, 10);
if (score > playerHighScore) {
storage.playerHighScore = score;
}
}
if (obstacles[i] && obstacles[i].intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Create new obstacles
if (LK.ticks % 120 == 0) {
createObstacle();
}
// Update speed
updateSpeed();
};