/**** * Plugins ****/ var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ //<Assets used in the game will automatically appear here> // Highscore class to display the highscore var Highscore = Container.expand(function () { var self = Container.call(this); self.highscore = storage.highscore || 0; var scoreText = new Text2('Highscore: 0', { size: 150, fill: 0xFFFFFF }); scoreText.anchor.set(1, 0); // Anchor to the top right self.addChild(scoreText); self.update = function () { scoreText.setText('Highscore: ' + self.highscore); }; }); // Rock class representing falling rocks var Rock = Container.expand(function () { var self = Container.call(this); var rockGraphics = self.attachAsset('rock', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 5 + Math.floor(LK.ticks / 600); // Speed at which the rock falls increases over time self.update = function () { self.y += self.speed; }; }); // Score class to display the current score var Score = Container.expand(function () { var self = Container.call(this); var scoreText = new Text2('Score: 0', { size: 150, fill: 0xFFFFFF }); scoreText.anchor.set(0.5, 0.5); // Anchor to the center self.addChild(scoreText); self.update = function () { scoreText.setText('Score: ' + LK.getScore()); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ title: 'Space escape', backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var galaxyBackground = game.attachAsset('Galaxy', { anchorX: 0.5, anchorY: 0.5, scaleX: 2048 / 100, // Scale to fit the screen width scaleY: 2732 / 100 // Scale to fit the screen height }); galaxyBackground.x = 2048 / 2; // Center the background galaxyBackground.y = 2732 / 2; // Center the background // Initialize variables var rocks = []; var player; var score = 0; // Create player character function createPlayer() { player = new Container(); var playerGraphics = player.attachAsset('player', { anchorX: 0.5, anchorY: 0.5 }); player.x = 2048 / 2; player.y = 2732 - 150; // Position player near the bottom game.addChild(player); } // Handle player movement game.move = function (x, y, obj) { // Restrict the player's x position to within the game frame player.x = Math.max(0, Math.min(2048, x)); }; // Create rocks at random positions function createRock() { var rock = new Rock(); rock.x = Math.random() * 2048; rock.y = -50; // Start above the screen rocks.push(rock); game.addChild(rock); } // Update game state game.update = function () { // Create a new rock every 60 frames if (LK.ticks % 60 === 0) { createRock(); } // Update rocks for (var i = rocks.length - 1; i >= 0; i--) { var rock = rocks[i]; rock.speed = 5 + Math.floor(LK.ticks / 600); // Update the rock's falling speed rock.update(); // Check if rock is off-screen if (rock.y > 2732) { rock.destroy(); rocks.splice(i, 1); LK.setScore(LK.getScore() + 1); // Increment score for each asteroid passed if (LK.getScore() > highscore.highscore) { highscore.highscore = LK.getScore(); storage.highscore = LK.getScore(); } continue; } // Check for collision with player if (rock.intersects(player)) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } } }; // Initialize game elements createPlayer(); var highscore = new Highscore(); highscore.x = 2048; // Position to the right edge of the screen highscore.y = 0; game.addChild(highscore); var score = new Score(); score.x = 500; // Position to the right score.y = 70; // Position up by 100 pixels game.addChild(score); ;
/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Highscore class to display the highscore
var Highscore = Container.expand(function () {
var self = Container.call(this);
self.highscore = storage.highscore || 0;
var scoreText = new Text2('Highscore: 0', {
size: 150,
fill: 0xFFFFFF
});
scoreText.anchor.set(1, 0); // Anchor to the top right
self.addChild(scoreText);
self.update = function () {
scoreText.setText('Highscore: ' + self.highscore);
};
});
// Rock class representing falling rocks
var Rock = Container.expand(function () {
var self = Container.call(this);
var rockGraphics = self.attachAsset('rock', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5 + Math.floor(LK.ticks / 600); // Speed at which the rock falls increases over time
self.update = function () {
self.y += self.speed;
};
});
// Score class to display the current score
var Score = Container.expand(function () {
var self = Container.call(this);
var scoreText = new Text2('Score: 0', {
size: 150,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0.5); // Anchor to the center
self.addChild(scoreText);
self.update = function () {
scoreText.setText('Score: ' + LK.getScore());
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
title: 'Space escape',
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var galaxyBackground = game.attachAsset('Galaxy', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2048 / 100,
// Scale to fit the screen width
scaleY: 2732 / 100 // Scale to fit the screen height
});
galaxyBackground.x = 2048 / 2; // Center the background
galaxyBackground.y = 2732 / 2; // Center the background
// Initialize variables
var rocks = [];
var player;
var score = 0;
// Create player character
function createPlayer() {
player = new Container();
var playerGraphics = player.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
player.x = 2048 / 2;
player.y = 2732 - 150; // Position player near the bottom
game.addChild(player);
}
// Handle player movement
game.move = function (x, y, obj) {
// Restrict the player's x position to within the game frame
player.x = Math.max(0, Math.min(2048, x));
};
// Create rocks at random positions
function createRock() {
var rock = new Rock();
rock.x = Math.random() * 2048;
rock.y = -50; // Start above the screen
rocks.push(rock);
game.addChild(rock);
}
// Update game state
game.update = function () {
// Create a new rock every 60 frames
if (LK.ticks % 60 === 0) {
createRock();
}
// Update rocks
for (var i = rocks.length - 1; i >= 0; i--) {
var rock = rocks[i];
rock.speed = 5 + Math.floor(LK.ticks / 600); // Update the rock's falling speed
rock.update();
// Check if rock is off-screen
if (rock.y > 2732) {
rock.destroy();
rocks.splice(i, 1);
LK.setScore(LK.getScore() + 1); // Increment score for each asteroid passed
if (LK.getScore() > highscore.highscore) {
highscore.highscore = LK.getScore();
storage.highscore = LK.getScore();
}
continue;
}
// Check for collision with player
if (rock.intersects(player)) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
}
};
// Initialize game elements
createPlayer();
var highscore = new Highscore();
highscore.x = 2048; // Position to the right edge of the screen
highscore.y = 0;
game.addChild(highscore);
var score = new Score();
score.x = 500; // Position to the right
score.y = 70; // Position up by 100 pixels
game.addChild(score);
;