/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphic = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3 + Math.random() * 3;
self.rotationSpeed = (Math.random() - 0.5) * 0.05;
self.size = 0.8 + Math.random() * 0.8;
asteroidGraphic.scale.set(self.size);
self.update = function () {
self.x -= self.speed;
asteroidGraphic.rotation += self.rotationSpeed;
// Remove if off screen
if (self.x < -200) {
self.toRemove = true;
}
};
return self;
});
var Astronaut = Container.expand(function () {
var self = Container.call(this);
var astronautBody = self.attachAsset('astronaut', {
anchorX: 0.5,
anchorY: 0.5
});
var jetpackFlame = self.attachAsset('jetpackFlame', {
anchorX: 0.5,
anchorY: 0,
x: 0,
y: 50,
alpha: 0
});
self.vx = 0;
self.vy = 0;
self.thrust = 0;
self.fuel = 100;
self.isDead = false;
self.activateJetpack = function (strength) {
if (self.fuel <= 0 || self.isDead) {
jetpackFlame.alpha = 0;
return;
}
self.thrust = strength;
jetpackFlame.alpha = strength;
// Consume fuel when jetpack is active
self.fuel -= 0.3 * strength;
if (self.fuel < 0) {
self.fuel = 0;
}
};
self.deactivateJetpack = function () {
self.thrust = 0;
jetpackFlame.alpha = 0;
};
self.update = function () {
if (self.isDead) {
return;
}
// Apply gravity
self.vy += 0.2;
// Apply thrust
self.vy -= self.thrust * 0.4;
// Apply velocity
self.y += self.vy;
self.x += self.vx;
// Tilt astronaut based on vertical movement
var targetRotation = self.vy * 0.05;
astronautBody.rotation = astronautBody.rotation * 0.8 + targetRotation * 0.2;
// Screen boundaries
if (self.y < 100) {
self.y = 100;
self.vy = 0;
}
if (self.y > 2732 - 100) {
self.y = 2732 - 100;
self.vy = 0;
}
};
return self;
});
var FuelCell = Container.expand(function () {
var self = Container.call(this);
var fuelGraphic = self.attachAsset('fuelCell', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2 + Math.random() * 2;
self.collected = false;
// Add subtle floating animation
self.baseY = 0;
self.floatOffset = 0;
self.floatSpeed = 0.02 + Math.random() * 0.01;
self.update = function () {
self.x -= self.speed;
// Floating animation
self.floatOffset += self.floatSpeed;
fuelGraphic.y = self.baseY + Math.sin(self.floatOffset) * 10;
// Remove if off screen
if (self.x < -200) {
self.toRemove = true;
}
};
self.collect = function () {
if (self.collected) {
return;
}
self.collected = true;
LK.getSound('collect').play();
// Flash and scale animation
tween(fuelGraphic, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
easing: tween.easeOut
});
};
return self;
});
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphic = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0.2 + Math.random() * 1;
self.size = 0.3 + Math.random() * 0.7;
starGraphic.scale.set(self.size);
starGraphic.alpha = 0.3 + Math.random() * 0.7;
self.update = function () {
self.x -= self.speed;
// Reset position if off screen
if (self.x < -50) {
self.x = 2048 + 50;
self.y = Math.random() * 2732;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000011
});
/****
* Game Code
****/
// Game state variables
var astronaut;
var asteroids = [];
var fuelCells = [];
var stars = [];
var isJetpackActive = false;
var score = 0;
var gameOver = false;
var asteroidSpawnTimer = 0;
var asteroidSpawnRate = 120;
var fuelCellSpawnTimer = 0;
var fuelCellSpawnRate = 180;
var highScore = storage.highScore || 0;
// UI elements
var scoreText = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreText.anchor.set(1.0, 0);
LK.gui.topRight.addChild(scoreText);
var highScoreText = new Text2('BEST: 0', {
size: 50,
fill: 0xAAAAAA
});
highScoreText.anchor.set(1.0, 0);
highScoreText.y = 90;
LK.gui.topRight.addChild(highScoreText);
highScoreText.setText('BEST: ' + highScore);
var fuelBarBg = LK.getAsset('fuelBarBg', {
anchorX: 0.5,
anchorY: 0.5
});
var fuelBar = LK.getAsset('fuelBar', {
anchorX: 0,
anchorY: 0.5,
x: -400
});
var fuelBarContainer = new Container();
fuelBarContainer.addChild(fuelBarBg);
fuelBarContainer.addChild(fuelBar);
fuelBarContainer.y = 50;
LK.gui.top.addChild(fuelBarContainer);
// Create background stars
function createStars() {
for (var i = 0; i < 100; i++) {
var star = new Star();
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
stars.push(star);
game.addChild(star);
}
}
// Initialize game elements
function initGame() {
// Reset game state
score = 0;
gameOver = false;
// Clear existing game objects
if (astronaut) {
astronaut.destroy();
}
for (var i = asteroids.length - 1; i >= 0; i--) {
asteroids[i].destroy();
}
asteroids = [];
for (var i = fuelCells.length - 1; i >= 0; i--) {
fuelCells[i].destroy();
}
fuelCells = [];
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].destroy();
}
stars = [];
// Create background stars
createStars();
// Create astronaut
astronaut = new Astronaut();
astronaut.x = 300;
astronaut.y = 2732 / 2;
game.addChild(astronaut);
// Reset UI
scoreText.setText('0');
LK.setScore(0);
// Play background music
LK.playMusic('spaceBgm', {
fade: {
start: 0,
end: 0.4,
duration: 1000
}
});
}
// Spawn a new asteroid
function spawnAsteroid() {
var asteroid = new Asteroid();
asteroid.x = 2048 + 200;
asteroid.y = 200 + Math.random() * (2732 - 400);
asteroids.push(asteroid);
game.addChild(asteroid);
}
// Spawn a new fuel cell
function spawnFuelCell() {
var fuelCell = new FuelCell();
fuelCell.x = 2048 + 200;
fuelCell.y = 200 + Math.random() * (2732 - 400);
fuelCells.push(fuelCell);
game.addChild(fuelCell);
}
// Handle input for astronaut jetpack
game.down = function (x, y) {
if (gameOver) {
return;
}
isJetpackActive = true;
astronaut.activateJetpack(1.0);
LK.getSound('jetpack').play();
};
game.up = function () {
isJetpackActive = false;
astronaut.deactivateJetpack();
};
game.move = function (x, y) {
if (isJetpackActive && !gameOver) {
astronaut.activateJetpack(1.0);
}
};
// Main game update function
game.update = function () {
if (!astronaut) {
initGame();
return;
}
// Increment score
if (!gameOver) {
score += 0.1;
LK.setScore(Math.floor(score));
scoreText.setText(Math.floor(score));
}
// Update fuel bar
var fuelPercent = astronaut.fuel / 100;
fuelBar.width = 800 * fuelPercent;
// Change fuel bar color based on amount
if (fuelPercent < 0.3) {
fuelBar.tint = 0xff0000;
} else if (fuelPercent < 0.6) {
fuelBar.tint = 0xffff00;
} else {
fuelBar.tint = 0x00ff00;
}
// Spawn asteroids
asteroidSpawnTimer++;
if (asteroidSpawnTimer >= asteroidSpawnRate && !gameOver) {
spawnAsteroid();
asteroidSpawnTimer = 0;
// Gradually increase difficulty
if (asteroidSpawnRate > 40) {
asteroidSpawnRate -= 0.2;
}
}
// Spawn fuel cells
fuelCellSpawnTimer++;
if (fuelCellSpawnTimer >= fuelCellSpawnRate && !gameOver) {
spawnFuelCell();
fuelCellSpawnTimer = 0;
}
// Update game objects
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
asteroid.update();
// Check collision with astronaut
if (!gameOver && !astronaut.isDead && asteroid.intersects(astronaut)) {
// Collision with asteroid - game over
LK.getSound('explosion').play();
astronaut.isDead = true;
gameOver = true;
// Flash screen red
LK.effects.flashScreen(0xff0000, 800);
// Update high score
if (score > highScore) {
highScore = Math.floor(score);
storage.highScore = highScore;
highScoreText.setText('BEST: ' + highScore);
}
// Show game over after a short delay
LK.setTimeout(function () {
LK.showGameOver();
}, 1500);
// Stop music
LK.stopMusic();
// Visual effect for asteroid collision
tween(asteroid, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 500,
easing: tween.easeOut
});
}
// Remove asteroids that are marked for removal
if (asteroid.toRemove) {
asteroid.destroy();
asteroids.splice(i, 1);
}
}
// Update fuel cells
for (var i = fuelCells.length - 1; i >= 0; i--) {
var fuelCell = fuelCells[i];
fuelCell.update();
// Check collision with astronaut
if (!gameOver && !fuelCell.collected && fuelCell.intersects(astronaut)) {
// Collect fuel cell
fuelCell.collect();
// Add fuel
astronaut.fuel += 30;
if (astronaut.fuel > 100) {
astronaut.fuel = 100;
}
// Remove fuel cell after animation
LK.setTimeout(function () {
var index = fuelCells.indexOf(fuelCell);
if (index !== -1) {
fuelCell.destroy();
fuelCells.splice(index, 1);
}
}, 300);
}
// Remove fuel cells that are marked for removal
if (fuelCell.toRemove) {
fuelCell.destroy();
fuelCells.splice(i, 1);
}
}
// Update stars
for (var i = 0; i < stars.length; i++) {
stars[i].update();
}
// Check for out of fuel condition
if (!gameOver && astronaut.fuel <= 0 && astronaut.y > 2732 - 200) {
// Out of fuel and hitting the bottom - game over
astronaut.isDead = true;
gameOver = true;
// Flash screen
LK.effects.flashScreen(0xff0000, 800);
// Update high score
if (score > highScore) {
highScore = Math.floor(score);
storage.highScore = highScore;
highScoreText.setText('BEST: ' + highScore);
}
// Show game over after a short delay
LK.setTimeout(function () {
LK.showGameOver();
}, 1500);
// Stop music
LK.stopMusic();
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphic = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3 + Math.random() * 3;
self.rotationSpeed = (Math.random() - 0.5) * 0.05;
self.size = 0.8 + Math.random() * 0.8;
asteroidGraphic.scale.set(self.size);
self.update = function () {
self.x -= self.speed;
asteroidGraphic.rotation += self.rotationSpeed;
// Remove if off screen
if (self.x < -200) {
self.toRemove = true;
}
};
return self;
});
var Astronaut = Container.expand(function () {
var self = Container.call(this);
var astronautBody = self.attachAsset('astronaut', {
anchorX: 0.5,
anchorY: 0.5
});
var jetpackFlame = self.attachAsset('jetpackFlame', {
anchorX: 0.5,
anchorY: 0,
x: 0,
y: 50,
alpha: 0
});
self.vx = 0;
self.vy = 0;
self.thrust = 0;
self.fuel = 100;
self.isDead = false;
self.activateJetpack = function (strength) {
if (self.fuel <= 0 || self.isDead) {
jetpackFlame.alpha = 0;
return;
}
self.thrust = strength;
jetpackFlame.alpha = strength;
// Consume fuel when jetpack is active
self.fuel -= 0.3 * strength;
if (self.fuel < 0) {
self.fuel = 0;
}
};
self.deactivateJetpack = function () {
self.thrust = 0;
jetpackFlame.alpha = 0;
};
self.update = function () {
if (self.isDead) {
return;
}
// Apply gravity
self.vy += 0.2;
// Apply thrust
self.vy -= self.thrust * 0.4;
// Apply velocity
self.y += self.vy;
self.x += self.vx;
// Tilt astronaut based on vertical movement
var targetRotation = self.vy * 0.05;
astronautBody.rotation = astronautBody.rotation * 0.8 + targetRotation * 0.2;
// Screen boundaries
if (self.y < 100) {
self.y = 100;
self.vy = 0;
}
if (self.y > 2732 - 100) {
self.y = 2732 - 100;
self.vy = 0;
}
};
return self;
});
var FuelCell = Container.expand(function () {
var self = Container.call(this);
var fuelGraphic = self.attachAsset('fuelCell', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2 + Math.random() * 2;
self.collected = false;
// Add subtle floating animation
self.baseY = 0;
self.floatOffset = 0;
self.floatSpeed = 0.02 + Math.random() * 0.01;
self.update = function () {
self.x -= self.speed;
// Floating animation
self.floatOffset += self.floatSpeed;
fuelGraphic.y = self.baseY + Math.sin(self.floatOffset) * 10;
// Remove if off screen
if (self.x < -200) {
self.toRemove = true;
}
};
self.collect = function () {
if (self.collected) {
return;
}
self.collected = true;
LK.getSound('collect').play();
// Flash and scale animation
tween(fuelGraphic, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
easing: tween.easeOut
});
};
return self;
});
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphic = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0.2 + Math.random() * 1;
self.size = 0.3 + Math.random() * 0.7;
starGraphic.scale.set(self.size);
starGraphic.alpha = 0.3 + Math.random() * 0.7;
self.update = function () {
self.x -= self.speed;
// Reset position if off screen
if (self.x < -50) {
self.x = 2048 + 50;
self.y = Math.random() * 2732;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000011
});
/****
* Game Code
****/
// Game state variables
var astronaut;
var asteroids = [];
var fuelCells = [];
var stars = [];
var isJetpackActive = false;
var score = 0;
var gameOver = false;
var asteroidSpawnTimer = 0;
var asteroidSpawnRate = 120;
var fuelCellSpawnTimer = 0;
var fuelCellSpawnRate = 180;
var highScore = storage.highScore || 0;
// UI elements
var scoreText = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreText.anchor.set(1.0, 0);
LK.gui.topRight.addChild(scoreText);
var highScoreText = new Text2('BEST: 0', {
size: 50,
fill: 0xAAAAAA
});
highScoreText.anchor.set(1.0, 0);
highScoreText.y = 90;
LK.gui.topRight.addChild(highScoreText);
highScoreText.setText('BEST: ' + highScore);
var fuelBarBg = LK.getAsset('fuelBarBg', {
anchorX: 0.5,
anchorY: 0.5
});
var fuelBar = LK.getAsset('fuelBar', {
anchorX: 0,
anchorY: 0.5,
x: -400
});
var fuelBarContainer = new Container();
fuelBarContainer.addChild(fuelBarBg);
fuelBarContainer.addChild(fuelBar);
fuelBarContainer.y = 50;
LK.gui.top.addChild(fuelBarContainer);
// Create background stars
function createStars() {
for (var i = 0; i < 100; i++) {
var star = new Star();
star.x = Math.random() * 2048;
star.y = Math.random() * 2732;
stars.push(star);
game.addChild(star);
}
}
// Initialize game elements
function initGame() {
// Reset game state
score = 0;
gameOver = false;
// Clear existing game objects
if (astronaut) {
astronaut.destroy();
}
for (var i = asteroids.length - 1; i >= 0; i--) {
asteroids[i].destroy();
}
asteroids = [];
for (var i = fuelCells.length - 1; i >= 0; i--) {
fuelCells[i].destroy();
}
fuelCells = [];
for (var i = stars.length - 1; i >= 0; i--) {
stars[i].destroy();
}
stars = [];
// Create background stars
createStars();
// Create astronaut
astronaut = new Astronaut();
astronaut.x = 300;
astronaut.y = 2732 / 2;
game.addChild(astronaut);
// Reset UI
scoreText.setText('0');
LK.setScore(0);
// Play background music
LK.playMusic('spaceBgm', {
fade: {
start: 0,
end: 0.4,
duration: 1000
}
});
}
// Spawn a new asteroid
function spawnAsteroid() {
var asteroid = new Asteroid();
asteroid.x = 2048 + 200;
asteroid.y = 200 + Math.random() * (2732 - 400);
asteroids.push(asteroid);
game.addChild(asteroid);
}
// Spawn a new fuel cell
function spawnFuelCell() {
var fuelCell = new FuelCell();
fuelCell.x = 2048 + 200;
fuelCell.y = 200 + Math.random() * (2732 - 400);
fuelCells.push(fuelCell);
game.addChild(fuelCell);
}
// Handle input for astronaut jetpack
game.down = function (x, y) {
if (gameOver) {
return;
}
isJetpackActive = true;
astronaut.activateJetpack(1.0);
LK.getSound('jetpack').play();
};
game.up = function () {
isJetpackActive = false;
astronaut.deactivateJetpack();
};
game.move = function (x, y) {
if (isJetpackActive && !gameOver) {
astronaut.activateJetpack(1.0);
}
};
// Main game update function
game.update = function () {
if (!astronaut) {
initGame();
return;
}
// Increment score
if (!gameOver) {
score += 0.1;
LK.setScore(Math.floor(score));
scoreText.setText(Math.floor(score));
}
// Update fuel bar
var fuelPercent = astronaut.fuel / 100;
fuelBar.width = 800 * fuelPercent;
// Change fuel bar color based on amount
if (fuelPercent < 0.3) {
fuelBar.tint = 0xff0000;
} else if (fuelPercent < 0.6) {
fuelBar.tint = 0xffff00;
} else {
fuelBar.tint = 0x00ff00;
}
// Spawn asteroids
asteroidSpawnTimer++;
if (asteroidSpawnTimer >= asteroidSpawnRate && !gameOver) {
spawnAsteroid();
asteroidSpawnTimer = 0;
// Gradually increase difficulty
if (asteroidSpawnRate > 40) {
asteroidSpawnRate -= 0.2;
}
}
// Spawn fuel cells
fuelCellSpawnTimer++;
if (fuelCellSpawnTimer >= fuelCellSpawnRate && !gameOver) {
spawnFuelCell();
fuelCellSpawnTimer = 0;
}
// Update game objects
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
asteroid.update();
// Check collision with astronaut
if (!gameOver && !astronaut.isDead && asteroid.intersects(astronaut)) {
// Collision with asteroid - game over
LK.getSound('explosion').play();
astronaut.isDead = true;
gameOver = true;
// Flash screen red
LK.effects.flashScreen(0xff0000, 800);
// Update high score
if (score > highScore) {
highScore = Math.floor(score);
storage.highScore = highScore;
highScoreText.setText('BEST: ' + highScore);
}
// Show game over after a short delay
LK.setTimeout(function () {
LK.showGameOver();
}, 1500);
// Stop music
LK.stopMusic();
// Visual effect for asteroid collision
tween(asteroid, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 500,
easing: tween.easeOut
});
}
// Remove asteroids that are marked for removal
if (asteroid.toRemove) {
asteroid.destroy();
asteroids.splice(i, 1);
}
}
// Update fuel cells
for (var i = fuelCells.length - 1; i >= 0; i--) {
var fuelCell = fuelCells[i];
fuelCell.update();
// Check collision with astronaut
if (!gameOver && !fuelCell.collected && fuelCell.intersects(astronaut)) {
// Collect fuel cell
fuelCell.collect();
// Add fuel
astronaut.fuel += 30;
if (astronaut.fuel > 100) {
astronaut.fuel = 100;
}
// Remove fuel cell after animation
LK.setTimeout(function () {
var index = fuelCells.indexOf(fuelCell);
if (index !== -1) {
fuelCell.destroy();
fuelCells.splice(index, 1);
}
}, 300);
}
// Remove fuel cells that are marked for removal
if (fuelCell.toRemove) {
fuelCell.destroy();
fuelCells.splice(i, 1);
}
}
// Update stars
for (var i = 0; i < stars.length; i++) {
stars[i].update();
}
// Check for out of fuel condition
if (!gameOver && astronaut.fuel <= 0 && astronaut.y > 2732 - 200) {
// Out of fuel and hitting the bottom - game over
astronaut.isDead = true;
gameOver = true;
// Flash screen
LK.effects.flashScreen(0xff0000, 800);
// Update high score
if (score > highScore) {
highScore = Math.floor(score);
storage.highScore = highScore;
highScoreText.setText('BEST: ' + highScore);
}
// Show game over after a short delay
LK.setTimeout(function () {
LK.showGameOver();
}, 1500);
// Stop music
LK.stopMusic();
}
};