/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Asteroid = Container.expand(function (isSmall) {
var self = Container.call(this);
var assetType = isSmall ? 'smallAsteroid' : 'asteroid';
var asteroidGraphics = self.attachAsset(assetType, {
anchorX: 0.5,
anchorY: 0.5
});
self.width = asteroidGraphics.width;
self.height = asteroidGraphics.height;
self.rotation = Math.random() * Math.PI * 2;
self.rotationSpeed = (Math.random() - 0.5) * 0.1;
self.speedY = 3 + Math.random() * 5;
self.speedX = (Math.random() - 0.5) * 3;
self.update = function () {
self.y += self.speedY;
self.x += self.speedX;
self.rotation += self.rotationSpeed;
// Wrap horizontally if asteroid moves off screen
if (self.x < -self.width) {
self.x = 2048 + self.width;
} else if (self.x > 2048 + self.width) {
self.x = -self.width;
}
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerupGraphics = self.attachAsset('powerup', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = powerupGraphics.width;
self.height = powerupGraphics.height;
self.speedY = 2 + Math.random() * 2;
self.type = Math.random() < 0.5 ? 'shield' : 'slowTime';
// Color based on type
if (self.type === 'shield') {
powerupGraphics.tint = 0x00ffff; // Cyan for shield
} else {
powerupGraphics.tint = 0xff9900; // Orange for slow time
}
// Rotation effect
tween(powerupGraphics, {
rotation: Math.PI * 2
}, {
duration: 2000,
onFinish: function onFinish() {
if (self.parent) {
powerupGraphics.rotation = 0;
tween(powerupGraphics, {
rotation: Math.PI * 2
}, {
duration: 2000
});
}
}
});
self.update = function () {
self.y += self.speedY;
};
return self;
});
var Ship = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('ship', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = shipGraphics.width;
self.height = shipGraphics.height;
self.speed = 8;
self.isInvulnerable = false;
self.setInvulnerable = function (duration) {
self.isInvulnerable = true;
// Flash effect for invulnerability
var flashInterval = LK.setInterval(function () {
shipGraphics.alpha = shipGraphics.alpha === 1 ? 0.4 : 1;
}, 100);
LK.setTimeout(function () {
self.isInvulnerable = false;
shipGraphics.alpha = 1;
LK.clearInterval(flashInterval);
}, duration);
};
self.update = function () {
// Ship movement logic will be handled in game's update
};
self.reset = function () {
self.x = 2048 / 2;
self.y = 2732 - 300;
};
return self;
});
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = starGraphics.width;
self.height = starGraphics.height;
self.speedY = 3 + Math.random() * 3;
// Pulsing effect
tween(starGraphics, {
scaleX: 0.8,
scaleY: 0.8
}, {
duration: 800,
easing: tween.sinOut,
onFinish: function onFinish() {
tween(starGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 800,
easing: tween.sinIn,
onFinish: function onFinish() {
// Recursive call to create continuous pulse
if (self.parent) {
tween(starGraphics, {
scaleX: 0.8,
scaleY: 0.8
}, {
duration: 800,
easing: tween.sinOut
});
}
}
});
}
});
self.update = function () {
self.y += self.speedY;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000022
});
/****
* Game Code
****/
// Game variables
var ship;
var asteroids = [];
var stars = [];
var powerups = [];
var score = 0;
var highScore = storage.highScore || 0;
var gameActive = true;
var difficultyLevel = 1;
var lastAsteroidTime = 0;
var lastStarTime = 0;
var lastPowerupTime = 0;
var asteroidSpawnRate = 1500; // ms
var starSpawnRate = 2000; // ms
var powerupSpawnRate = 15000; // ms
var gameTime = 0;
var isSlowTimeActive = false;
var slowTimeFactor = 0.5;
var slowTimeRemaining = 0;
// Create background
var background = LK.getAsset('background', {
anchorX: 0,
anchorY: 0,
width: 2048,
height: 2732
});
game.addChild(background);
// Create ship
ship = new Ship();
ship.reset();
game.addChild(ship);
// Create UI elements
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.x = 120; // Avoid top-left corner
scoreTxt.y = 20;
var highScoreTxt = new Text2('High Score: ' + highScore, {
size: 60,
fill: 0xFFCC00
});
highScoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(highScoreTxt);
highScoreTxt.x = -20;
highScoreTxt.y = 20;
// UI for powerups
var powerupIndicator = new Text2('', {
size: 60,
fill: 0x00FFFF
});
powerupIndicator.anchor.set(0.5, 0);
LK.gui.top.addChild(powerupIndicator);
powerupIndicator.x = 2048 / 2;
powerupIndicator.y = 20;
// Game controls
var dragMode = false;
var targetX = ship.x;
function updateScoreDisplay() {
scoreTxt.setText('Score: ' + score);
if (score > highScore) {
highScore = score;
storage.highScore = highScore;
highScoreTxt.setText('High Score: ' + highScore);
}
}
function spawnAsteroid() {
var isSmall = Math.random() < 0.3;
var asteroid = new Asteroid(isSmall);
asteroid.x = Math.random() * 2048;
asteroid.y = -asteroid.height;
// Increase speed based on difficulty
asteroid.speedY *= 1 + (difficultyLevel - 1) * 0.1;
asteroids.push(asteroid);
game.addChild(asteroid);
}
function spawnStar() {
var star = new Star();
star.x = Math.random() * 2048;
star.y = -star.height;
stars.push(star);
game.addChild(star);
}
function spawnPowerup() {
var powerup = new PowerUp();
powerup.x = Math.random() * 2048;
powerup.y = -powerup.height;
powerups.push(powerup);
game.addChild(powerup);
}
function activatePowerup(type) {
LK.getSound('powerup').play();
if (type === 'shield') {
ship.setInvulnerable(8000);
powerupIndicator.setText('SHIELD ACTIVE');
powerupIndicator.setStyle({
fill: 0x00ffff
});
LK.setTimeout(function () {
powerupIndicator.setText('');
}, 8000);
} else if (type === 'slowTime') {
isSlowTimeActive = true;
slowTimeRemaining = 5000;
powerupIndicator.setText('SLOW TIME ACTIVE');
powerupIndicator.setStyle({
fill: 0xFF9900
});
LK.setTimeout(function () {
isSlowTimeActive = false;
powerupIndicator.setText('');
}, 5000);
}
}
function checkCollisions() {
// Check asteroid collisions
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
if (ship.intersects(asteroid) && !ship.isInvulnerable) {
// Game over on collision
LK.effects.flashScreen(0xff0000, 1000);
LK.getSound('explosion').play();
LK.showGameOver();
gameActive = false;
return;
}
// Remove asteroids that have moved off screen
if (asteroid.y > 2732 + asteroid.height) {
asteroid.destroy();
asteroids.splice(i, 1);
}
}
// Check star collections
for (var j = stars.length - 1; j >= 0; j--) {
var star = stars[j];
if (ship.intersects(star)) {
score += 10;
updateScoreDisplay();
LK.setScore(score);
star.destroy();
stars.splice(j, 1);
LK.getSound('collect').play();
continue;
}
// Remove stars that have moved off screen
if (star.y > 2732 + star.height) {
star.destroy();
stars.splice(j, 1);
}
}
// Check powerup collections
for (var k = powerups.length - 1; k >= 0; k--) {
var powerup = powerups[k];
if (ship.intersects(powerup)) {
activatePowerup(powerup.type);
powerup.destroy();
powerups.splice(k, 1);
continue;
}
// Remove powerups that have moved off screen
if (powerup.y > 2732 + powerup.height) {
powerup.destroy();
powerups.splice(k, 1);
}
}
}
// Event handlers
game.down = function (x, y, obj) {
dragMode = true;
targetX = x;
};
game.up = function (x, y, obj) {
dragMode = false;
};
game.move = function (x, y, obj) {
if (dragMode) {
targetX = x;
// Ensure the ship only moves horizontally
ship.x = targetX;
}
};
// Main game update
game.update = function () {
if (!gameActive) {
return;
}
gameTime += 16.67; // Approximate ms per frame at 60fps
// Increase difficulty over time
difficultyLevel = 1 + Math.floor(gameTime / 30000); // Increase every 30 seconds
// Update ship position (smooth movement)
var speedFactor = isSlowTimeActive ? ship.speed * 0.7 : ship.speed;
if (Math.abs(ship.x - targetX) > speedFactor) {
if (ship.x < targetX) {
ship.x += speedFactor;
} else {
ship.x -= speedFactor;
}
}
// Keep ship on screen
if (ship.x < ship.width / 2) {
ship.x = ship.width / 2;
} else if (ship.x > 2048 - ship.width / 2) {
ship.x = 2048 - ship.width / 2;
}
// Update slow time effect
if (isSlowTimeActive) {
slowTimeRemaining -= 16.67;
if (slowTimeRemaining <= 0) {
isSlowTimeActive = false;
}
}
// Spawn game objects
var spawnSpeedFactor = isSlowTimeActive ? slowTimeFactor : 1;
var currentTime = gameTime;
if (currentTime - lastAsteroidTime > asteroidSpawnRate * spawnSpeedFactor) {
spawnAsteroid();
lastAsteroidTime = currentTime;
// Gradually decrease spawn rate with difficulty
asteroidSpawnRate = Math.max(500, 1500 - (difficultyLevel - 1) * 200);
}
if (currentTime - lastStarTime > starSpawnRate) {
spawnStar();
lastStarTime = currentTime;
}
if (currentTime - lastPowerupTime > powerupSpawnRate) {
spawnPowerup();
lastPowerupTime = currentTime;
}
// Update all game objects
for (var i = 0; i < asteroids.length; i++) {
asteroids[i].update();
}
for (var j = 0; j < stars.length; j++) {
stars[j].update();
}
for (var k = 0; k < powerups.length; k++) {
powerups[k].update();
}
// Check for collisions
checkCollisions();
};
// Play background music
LK.playMusic('gameMusic'); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Asteroid = Container.expand(function (isSmall) {
var self = Container.call(this);
var assetType = isSmall ? 'smallAsteroid' : 'asteroid';
var asteroidGraphics = self.attachAsset(assetType, {
anchorX: 0.5,
anchorY: 0.5
});
self.width = asteroidGraphics.width;
self.height = asteroidGraphics.height;
self.rotation = Math.random() * Math.PI * 2;
self.rotationSpeed = (Math.random() - 0.5) * 0.1;
self.speedY = 3 + Math.random() * 5;
self.speedX = (Math.random() - 0.5) * 3;
self.update = function () {
self.y += self.speedY;
self.x += self.speedX;
self.rotation += self.rotationSpeed;
// Wrap horizontally if asteroid moves off screen
if (self.x < -self.width) {
self.x = 2048 + self.width;
} else if (self.x > 2048 + self.width) {
self.x = -self.width;
}
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerupGraphics = self.attachAsset('powerup', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = powerupGraphics.width;
self.height = powerupGraphics.height;
self.speedY = 2 + Math.random() * 2;
self.type = Math.random() < 0.5 ? 'shield' : 'slowTime';
// Color based on type
if (self.type === 'shield') {
powerupGraphics.tint = 0x00ffff; // Cyan for shield
} else {
powerupGraphics.tint = 0xff9900; // Orange for slow time
}
// Rotation effect
tween(powerupGraphics, {
rotation: Math.PI * 2
}, {
duration: 2000,
onFinish: function onFinish() {
if (self.parent) {
powerupGraphics.rotation = 0;
tween(powerupGraphics, {
rotation: Math.PI * 2
}, {
duration: 2000
});
}
}
});
self.update = function () {
self.y += self.speedY;
};
return self;
});
var Ship = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('ship', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = shipGraphics.width;
self.height = shipGraphics.height;
self.speed = 8;
self.isInvulnerable = false;
self.setInvulnerable = function (duration) {
self.isInvulnerable = true;
// Flash effect for invulnerability
var flashInterval = LK.setInterval(function () {
shipGraphics.alpha = shipGraphics.alpha === 1 ? 0.4 : 1;
}, 100);
LK.setTimeout(function () {
self.isInvulnerable = false;
shipGraphics.alpha = 1;
LK.clearInterval(flashInterval);
}, duration);
};
self.update = function () {
// Ship movement logic will be handled in game's update
};
self.reset = function () {
self.x = 2048 / 2;
self.y = 2732 - 300;
};
return self;
});
var Star = Container.expand(function () {
var self = Container.call(this);
var starGraphics = self.attachAsset('star', {
anchorX: 0.5,
anchorY: 0.5
});
self.width = starGraphics.width;
self.height = starGraphics.height;
self.speedY = 3 + Math.random() * 3;
// Pulsing effect
tween(starGraphics, {
scaleX: 0.8,
scaleY: 0.8
}, {
duration: 800,
easing: tween.sinOut,
onFinish: function onFinish() {
tween(starGraphics, {
scaleX: 1,
scaleY: 1
}, {
duration: 800,
easing: tween.sinIn,
onFinish: function onFinish() {
// Recursive call to create continuous pulse
if (self.parent) {
tween(starGraphics, {
scaleX: 0.8,
scaleY: 0.8
}, {
duration: 800,
easing: tween.sinOut
});
}
}
});
}
});
self.update = function () {
self.y += self.speedY;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000022
});
/****
* Game Code
****/
// Game variables
var ship;
var asteroids = [];
var stars = [];
var powerups = [];
var score = 0;
var highScore = storage.highScore || 0;
var gameActive = true;
var difficultyLevel = 1;
var lastAsteroidTime = 0;
var lastStarTime = 0;
var lastPowerupTime = 0;
var asteroidSpawnRate = 1500; // ms
var starSpawnRate = 2000; // ms
var powerupSpawnRate = 15000; // ms
var gameTime = 0;
var isSlowTimeActive = false;
var slowTimeFactor = 0.5;
var slowTimeRemaining = 0;
// Create background
var background = LK.getAsset('background', {
anchorX: 0,
anchorY: 0,
width: 2048,
height: 2732
});
game.addChild(background);
// Create ship
ship = new Ship();
ship.reset();
game.addChild(ship);
// Create UI elements
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.x = 120; // Avoid top-left corner
scoreTxt.y = 20;
var highScoreTxt = new Text2('High Score: ' + highScore, {
size: 60,
fill: 0xFFCC00
});
highScoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(highScoreTxt);
highScoreTxt.x = -20;
highScoreTxt.y = 20;
// UI for powerups
var powerupIndicator = new Text2('', {
size: 60,
fill: 0x00FFFF
});
powerupIndicator.anchor.set(0.5, 0);
LK.gui.top.addChild(powerupIndicator);
powerupIndicator.x = 2048 / 2;
powerupIndicator.y = 20;
// Game controls
var dragMode = false;
var targetX = ship.x;
function updateScoreDisplay() {
scoreTxt.setText('Score: ' + score);
if (score > highScore) {
highScore = score;
storage.highScore = highScore;
highScoreTxt.setText('High Score: ' + highScore);
}
}
function spawnAsteroid() {
var isSmall = Math.random() < 0.3;
var asteroid = new Asteroid(isSmall);
asteroid.x = Math.random() * 2048;
asteroid.y = -asteroid.height;
// Increase speed based on difficulty
asteroid.speedY *= 1 + (difficultyLevel - 1) * 0.1;
asteroids.push(asteroid);
game.addChild(asteroid);
}
function spawnStar() {
var star = new Star();
star.x = Math.random() * 2048;
star.y = -star.height;
stars.push(star);
game.addChild(star);
}
function spawnPowerup() {
var powerup = new PowerUp();
powerup.x = Math.random() * 2048;
powerup.y = -powerup.height;
powerups.push(powerup);
game.addChild(powerup);
}
function activatePowerup(type) {
LK.getSound('powerup').play();
if (type === 'shield') {
ship.setInvulnerable(8000);
powerupIndicator.setText('SHIELD ACTIVE');
powerupIndicator.setStyle({
fill: 0x00ffff
});
LK.setTimeout(function () {
powerupIndicator.setText('');
}, 8000);
} else if (type === 'slowTime') {
isSlowTimeActive = true;
slowTimeRemaining = 5000;
powerupIndicator.setText('SLOW TIME ACTIVE');
powerupIndicator.setStyle({
fill: 0xFF9900
});
LK.setTimeout(function () {
isSlowTimeActive = false;
powerupIndicator.setText('');
}, 5000);
}
}
function checkCollisions() {
// Check asteroid collisions
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
if (ship.intersects(asteroid) && !ship.isInvulnerable) {
// Game over on collision
LK.effects.flashScreen(0xff0000, 1000);
LK.getSound('explosion').play();
LK.showGameOver();
gameActive = false;
return;
}
// Remove asteroids that have moved off screen
if (asteroid.y > 2732 + asteroid.height) {
asteroid.destroy();
asteroids.splice(i, 1);
}
}
// Check star collections
for (var j = stars.length - 1; j >= 0; j--) {
var star = stars[j];
if (ship.intersects(star)) {
score += 10;
updateScoreDisplay();
LK.setScore(score);
star.destroy();
stars.splice(j, 1);
LK.getSound('collect').play();
continue;
}
// Remove stars that have moved off screen
if (star.y > 2732 + star.height) {
star.destroy();
stars.splice(j, 1);
}
}
// Check powerup collections
for (var k = powerups.length - 1; k >= 0; k--) {
var powerup = powerups[k];
if (ship.intersects(powerup)) {
activatePowerup(powerup.type);
powerup.destroy();
powerups.splice(k, 1);
continue;
}
// Remove powerups that have moved off screen
if (powerup.y > 2732 + powerup.height) {
powerup.destroy();
powerups.splice(k, 1);
}
}
}
// Event handlers
game.down = function (x, y, obj) {
dragMode = true;
targetX = x;
};
game.up = function (x, y, obj) {
dragMode = false;
};
game.move = function (x, y, obj) {
if (dragMode) {
targetX = x;
// Ensure the ship only moves horizontally
ship.x = targetX;
}
};
// Main game update
game.update = function () {
if (!gameActive) {
return;
}
gameTime += 16.67; // Approximate ms per frame at 60fps
// Increase difficulty over time
difficultyLevel = 1 + Math.floor(gameTime / 30000); // Increase every 30 seconds
// Update ship position (smooth movement)
var speedFactor = isSlowTimeActive ? ship.speed * 0.7 : ship.speed;
if (Math.abs(ship.x - targetX) > speedFactor) {
if (ship.x < targetX) {
ship.x += speedFactor;
} else {
ship.x -= speedFactor;
}
}
// Keep ship on screen
if (ship.x < ship.width / 2) {
ship.x = ship.width / 2;
} else if (ship.x > 2048 - ship.width / 2) {
ship.x = 2048 - ship.width / 2;
}
// Update slow time effect
if (isSlowTimeActive) {
slowTimeRemaining -= 16.67;
if (slowTimeRemaining <= 0) {
isSlowTimeActive = false;
}
}
// Spawn game objects
var spawnSpeedFactor = isSlowTimeActive ? slowTimeFactor : 1;
var currentTime = gameTime;
if (currentTime - lastAsteroidTime > asteroidSpawnRate * spawnSpeedFactor) {
spawnAsteroid();
lastAsteroidTime = currentTime;
// Gradually decrease spawn rate with difficulty
asteroidSpawnRate = Math.max(500, 1500 - (difficultyLevel - 1) * 200);
}
if (currentTime - lastStarTime > starSpawnRate) {
spawnStar();
lastStarTime = currentTime;
}
if (currentTime - lastPowerupTime > powerupSpawnRate) {
spawnPowerup();
lastPowerupTime = currentTime;
}
// Update all game objects
for (var i = 0; i < asteroids.length; i++) {
asteroids[i].update();
}
for (var j = 0; j < stars.length; j++) {
stars[j].update();
}
for (var k = 0; k < powerups.length; k++) {
powerups[k].update();
}
// Check for collisions
checkCollisions();
};
// Play background music
LK.playMusic('gameMusic');
background image like ultra hd space or galaxy. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
2d interactive and dynamic asteroid images. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
give me a good and high quality 2d spaceship. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
a single 2d star image. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
a shield power up. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
a single asteriod texture. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows