/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { highScore: 0 }); /**** * Classes ****/ var Asteroid = Container.expand(function (size, speed) { var self = Container.call(this); self.size = size || 'large'; self.speed = speed || 5; var assetId = self.size === 'small' ? 'smallAsteroid' : 'asteroid'; var asteroidGraphics = self.attachAsset(assetId, { anchorX: 0.5, anchorY: 0.5 }); // Random rotation self.rotationSpeed = (Math.random() - 0.5) * 0.1; self.update = function () { self.y += self.speed; asteroidGraphics.rotation += self.rotationSpeed; }; return self; }); var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.update = function () { self.y -= self.speed; }; return self; }); var PlayerShip = Container.expand(function () { var self = Container.call(this); var shipGraphics = self.attachAsset('playerShip', { anchorX: 0.5, anchorY: 0.5 }); self.shield = false; self.speedBoost = false; self.weaponPowerup = false; self.shieldGraphic = self.attachAsset('shield', { anchorX: 0.5, anchorY: 0.5, alpha: 0 }); self.width = shipGraphics.width; self.height = shipGraphics.height; self.shootCooldown = 0; self.activateShield = function (duration) { self.shield = true; self.shieldGraphic.alpha = 0.5; LK.clearTimeout(self.shieldTimeout); self.shieldTimeout = LK.setTimeout(function () { self.shield = false; tween(self.shieldGraphic, { alpha: 0 }, { duration: 300 }); }, duration); }; self.activateSpeedBoost = function (duration) { self.speedBoost = true; LK.clearTimeout(self.speedBoostTimeout); self.speedBoostTimeout = LK.setTimeout(function () { self.speedBoost = false; }, duration); }; self.activateWeaponPowerup = function (duration) { self.weaponPowerup = true; LK.clearTimeout(self.weaponPowerupTimeout); self.weaponPowerupTimeout = LK.setTimeout(function () { self.weaponPowerup = false; }, duration); }; self.update = function () { if (self.shootCooldown > 0) { self.shootCooldown--; } }; self.shoot = function () { if (self.shootCooldown <= 0) { self.shootCooldown = self.weaponPowerup ? 10 : 20; return true; } return false; }; return self; }); var PowerUp = Container.expand(function (type) { var self = Container.call(this); self.type = type || 'shield'; var assetId; switch (self.type) { case 'shield': assetId = 'shield'; break; case 'speed': assetId = 'speedBoost'; break; case 'weapon': assetId = 'weaponPowerup'; break; default: assetId = 'shield'; } var powerupGraphics = self.attachAsset(assetId, { anchorX: 0.5, anchorY: 0.5 }); self.speed = 3; self.update = function () { self.y += self.speed; powerupGraphics.rotation += 0.02; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000033 }); /**** * Game Code ****/ // Game variables var player; var asteroids = []; var powerups = []; var bullets = []; var score = 0; var gameActive = true; var lastAsteroidTime = 0; var lastPowerupTime = 0; var difficultyLevel = 1; var difficultyTimer = 0; var highScore = storage.highScore || 0; // Game dimension constants var GAME_WIDTH = 2048; var GAME_HEIGHT = 2732; // Create score display var scoreTxt = new Text2('SCORE: 0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); scoreTxt.y = 50; // Create high score display var highScoreTxt = new Text2('HIGH SCORE: ' + highScore, { size: 60, fill: 0xFFCC00 }); highScoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(highScoreTxt); highScoreTxt.y = 140; // Initialize the player ship function initPlayer() { player = new PlayerShip(); player.x = GAME_WIDTH / 2; player.y = GAME_HEIGHT - 200; game.addChild(player); } // Initialize game elements function initGame() { initPlayer(); asteroids = []; powerups = []; bullets = []; score = 0; gameActive = true; lastAsteroidTime = 0; lastPowerupTime = 0; difficultyLevel = 1; difficultyTimer = 0; updateScore(0); // Play background music LK.playMusic('bgmusic'); } // Clean up game elements function cleanupGame() { if (player) { player.destroy(); player = null; } for (var i = 0; i < asteroids.length; i++) { asteroids[i].destroy(); } asteroids = []; for (var i = 0; i < powerups.length; i++) { powerups[i].destroy(); } powerups = []; for (var i = 0; i < bullets.length; i++) { bullets[i].destroy(); } bullets = []; LK.stopMusic(); } // Update score display function updateScore(newScore) { score = newScore; scoreTxt.setText('SCORE: ' + score); if (score > highScore) { highScore = score; storage.highScore = highScore; highScoreTxt.setText('HIGH SCORE: ' + highScore); } } // Spawn a new asteroid function spawnAsteroid() { var size = Math.random() < 0.3 ? 'small' : 'large'; var speed = Math.random() * 2 + 3 + difficultyLevel * 0.5; if (size === 'small') speed += 1; var asteroid = new Asteroid(size, speed); asteroid.x = Math.random() * (GAME_WIDTH - 200) + 100; asteroid.y = -100; asteroids.push(asteroid); game.addChild(asteroid); } // Spawn a new power-up function spawnPowerup() { var types = ['shield', 'speed', 'weapon']; var type = types[Math.floor(Math.random() * types.length)]; var powerup = new PowerUp(type); powerup.x = Math.random() * (GAME_WIDTH - 200) + 100; powerup.y = -100; powerups.push(powerup); game.addChild(powerup); } // Create a new bullet function fireBullet() { if (player && player.shoot()) { var bullet = new Bullet(); bullet.x = player.x; bullet.y = player.y - player.height / 2; bullets.push(bullet); game.addChild(bullet); LK.getSound('shoot').play(); // If player has weapon powerup, fire additional bullets if (player.weaponPowerup) { var bulletLeft = new Bullet(); bulletLeft.x = player.x - 30; bulletLeft.y = player.y - player.height / 2; bullets.push(bulletLeft); game.addChild(bulletLeft); var bulletRight = new Bullet(); bulletRight.x = player.x + 30; bulletRight.y = player.y - player.height / 2; bullets.push(bulletRight); game.addChild(bulletRight); } } } // Check collisions between game objects function checkCollisions() { if (!player || !gameActive) return; // Check asteroid collisions with player for (var i = asteroids.length - 1; i >= 0; i--) { var asteroid = asteroids[i]; // Check if asteroid is off screen if (asteroid.y > GAME_HEIGHT + 100) { asteroid.destroy(); asteroids.splice(i, 1); continue; } // Check collision with player if (player.intersects(asteroid)) { if (player.shield) { // Destroy asteroid but player is protected asteroid.destroy(); asteroids.splice(i, 1); updateScore(score + 5); } else { // Game over LK.effects.flashScreen(0xff0000, 1000); LK.getSound('collision').play(); gameActive = false; LK.showGameOver(); return; } } // Check collision with bullets for (var j = bullets.length - 1; j >= 0; j--) { var bullet = bullets[j]; if (asteroid.intersects(bullet)) { // Destroy both asteroid and bullet asteroid.destroy(); asteroids.splice(i, 1); bullet.destroy(); bullets.splice(j, 1); // Add score based on asteroid size updateScore(score + (asteroid.size === 'small' ? 15 : 10)); break; } } } // Check powerup collisions with player for (var i = powerups.length - 1; i >= 0; i--) { var powerup = powerups[i]; // Check if powerup is off screen if (powerup.y > GAME_HEIGHT + 100) { powerup.destroy(); powerups.splice(i, 1); continue; } // Check collision with player if (player.intersects(powerup)) { // Apply power-up effect switch (powerup.type) { case 'shield': player.activateShield(8000); break; case 'speed': player.activateSpeedBoost(10000); break; case 'weapon': player.activateWeaponPowerup(12000); break; } LK.getSound('powerup').play(); powerup.destroy(); powerups.splice(i, 1); updateScore(score + 5); } } // Check if bullets are off screen for (var i = bullets.length - 1; i >= 0; i--) { var bullet = bullets[i]; if (bullet.y < -100) { bullet.destroy(); bullets.splice(i, 1); } } } // Handle moving the player ship var dragging = false; var touchOffset = { x: 0, y: 0 }; function handleMove(x, y, obj) { if (dragging && player) { player.x = Math.max(player.width / 2, Math.min(GAME_WIDTH - player.width / 2, x - touchOffset.x)); player.y = Math.max(player.height / 2, Math.min(GAME_HEIGHT - player.height / 2, y - touchOffset.y)); } } game.down = function (x, y, obj) { if (!gameActive) return; if (player && !dragging) { // Calculate offset for smooth dragging var playerPos = game.toLocal(player.parent.toGlobal(player.position)); touchOffset.x = x - playerPos.x; touchOffset.y = y - playerPos.y; dragging = true; // Fire bullet on tap fireBullet(); } handleMove(x, y, obj); }; game.up = function (x, y, obj) { dragging = false; }; game.move = handleMove; // Main game update loop game.update = function () { if (!gameActive) return; // Update player if (player) { player.update(); } // Update difficulty difficultyTimer++; if (difficultyTimer >= 1800) { // Every 30 seconds difficultyLevel = Math.min(difficultyLevel + 1, 10); difficultyTimer = 0; } // Spawn asteroids var asteroidSpawnRate = Math.max(60 - difficultyLevel * 5, 20); if (LK.ticks - lastAsteroidTime >= asteroidSpawnRate) { spawnAsteroid(); lastAsteroidTime = LK.ticks; } // Spawn power-ups (less frequently) var powerupSpawnRate = 300 - difficultyLevel * 10; if (LK.ticks - lastPowerupTime >= powerupSpawnRate) { if (Math.random() < 0.7) { // 70% chance to spawn a powerup when the timer hits spawnPowerup(); } lastPowerupTime = LK.ticks; } // Update asteroids for (var i = 0; i < asteroids.length; i++) { asteroids[i].update(); } // Update power-ups for (var i = 0; i < powerups.length; i++) { powerups[i].update(); } // Update bullets for (var i = 0; i < bullets.length; i++) { bullets[i].update(); } // Check for collisions checkCollisions(); // Increment score based on survival time if (LK.ticks % 60 === 0) { // Once per second updateScore(score + 1); } }; // Initialize the game initGame();
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Asteroid = Container.expand(function (size, speed) {
var self = Container.call(this);
self.size = size || 'large';
self.speed = speed || 5;
var assetId = self.size === 'small' ? 'smallAsteroid' : 'asteroid';
var asteroidGraphics = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
// Random rotation
self.rotationSpeed = (Math.random() - 0.5) * 0.1;
self.update = function () {
self.y += self.speed;
asteroidGraphics.rotation += self.rotationSpeed;
};
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 10;
self.update = function () {
self.y -= self.speed;
};
return self;
});
var PlayerShip = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('playerShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.shield = false;
self.speedBoost = false;
self.weaponPowerup = false;
self.shieldGraphic = self.attachAsset('shield', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
self.width = shipGraphics.width;
self.height = shipGraphics.height;
self.shootCooldown = 0;
self.activateShield = function (duration) {
self.shield = true;
self.shieldGraphic.alpha = 0.5;
LK.clearTimeout(self.shieldTimeout);
self.shieldTimeout = LK.setTimeout(function () {
self.shield = false;
tween(self.shieldGraphic, {
alpha: 0
}, {
duration: 300
});
}, duration);
};
self.activateSpeedBoost = function (duration) {
self.speedBoost = true;
LK.clearTimeout(self.speedBoostTimeout);
self.speedBoostTimeout = LK.setTimeout(function () {
self.speedBoost = false;
}, duration);
};
self.activateWeaponPowerup = function (duration) {
self.weaponPowerup = true;
LK.clearTimeout(self.weaponPowerupTimeout);
self.weaponPowerupTimeout = LK.setTimeout(function () {
self.weaponPowerup = false;
}, duration);
};
self.update = function () {
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
self.shoot = function () {
if (self.shootCooldown <= 0) {
self.shootCooldown = self.weaponPowerup ? 10 : 20;
return true;
}
return false;
};
return self;
});
var PowerUp = Container.expand(function (type) {
var self = Container.call(this);
self.type = type || 'shield';
var assetId;
switch (self.type) {
case 'shield':
assetId = 'shield';
break;
case 'speed':
assetId = 'speedBoost';
break;
case 'weapon':
assetId = 'weaponPowerup';
break;
default:
assetId = 'shield';
}
var powerupGraphics = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
powerupGraphics.rotation += 0.02;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000033
});
/****
* Game Code
****/
// Game variables
var player;
var asteroids = [];
var powerups = [];
var bullets = [];
var score = 0;
var gameActive = true;
var lastAsteroidTime = 0;
var lastPowerupTime = 0;
var difficultyLevel = 1;
var difficultyTimer = 0;
var highScore = storage.highScore || 0;
// Game dimension constants
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
// Create score display
var scoreTxt = new Text2('SCORE: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 50;
// Create high score display
var highScoreTxt = new Text2('HIGH SCORE: ' + highScore, {
size: 60,
fill: 0xFFCC00
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 140;
// Initialize the player ship
function initPlayer() {
player = new PlayerShip();
player.x = GAME_WIDTH / 2;
player.y = GAME_HEIGHT - 200;
game.addChild(player);
}
// Initialize game elements
function initGame() {
initPlayer();
asteroids = [];
powerups = [];
bullets = [];
score = 0;
gameActive = true;
lastAsteroidTime = 0;
lastPowerupTime = 0;
difficultyLevel = 1;
difficultyTimer = 0;
updateScore(0);
// Play background music
LK.playMusic('bgmusic');
}
// Clean up game elements
function cleanupGame() {
if (player) {
player.destroy();
player = null;
}
for (var i = 0; i < asteroids.length; i++) {
asteroids[i].destroy();
}
asteroids = [];
for (var i = 0; i < powerups.length; i++) {
powerups[i].destroy();
}
powerups = [];
for (var i = 0; i < bullets.length; i++) {
bullets[i].destroy();
}
bullets = [];
LK.stopMusic();
}
// Update score display
function updateScore(newScore) {
score = newScore;
scoreTxt.setText('SCORE: ' + score);
if (score > highScore) {
highScore = score;
storage.highScore = highScore;
highScoreTxt.setText('HIGH SCORE: ' + highScore);
}
}
// Spawn a new asteroid
function spawnAsteroid() {
var size = Math.random() < 0.3 ? 'small' : 'large';
var speed = Math.random() * 2 + 3 + difficultyLevel * 0.5;
if (size === 'small') speed += 1;
var asteroid = new Asteroid(size, speed);
asteroid.x = Math.random() * (GAME_WIDTH - 200) + 100;
asteroid.y = -100;
asteroids.push(asteroid);
game.addChild(asteroid);
}
// Spawn a new power-up
function spawnPowerup() {
var types = ['shield', 'speed', 'weapon'];
var type = types[Math.floor(Math.random() * types.length)];
var powerup = new PowerUp(type);
powerup.x = Math.random() * (GAME_WIDTH - 200) + 100;
powerup.y = -100;
powerups.push(powerup);
game.addChild(powerup);
}
// Create a new bullet
function fireBullet() {
if (player && player.shoot()) {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y - player.height / 2;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
// If player has weapon powerup, fire additional bullets
if (player.weaponPowerup) {
var bulletLeft = new Bullet();
bulletLeft.x = player.x - 30;
bulletLeft.y = player.y - player.height / 2;
bullets.push(bulletLeft);
game.addChild(bulletLeft);
var bulletRight = new Bullet();
bulletRight.x = player.x + 30;
bulletRight.y = player.y - player.height / 2;
bullets.push(bulletRight);
game.addChild(bulletRight);
}
}
}
// Check collisions between game objects
function checkCollisions() {
if (!player || !gameActive) return;
// Check asteroid collisions with player
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
// Check if asteroid is off screen
if (asteroid.y > GAME_HEIGHT + 100) {
asteroid.destroy();
asteroids.splice(i, 1);
continue;
}
// Check collision with player
if (player.intersects(asteroid)) {
if (player.shield) {
// Destroy asteroid but player is protected
asteroid.destroy();
asteroids.splice(i, 1);
updateScore(score + 5);
} else {
// Game over
LK.effects.flashScreen(0xff0000, 1000);
LK.getSound('collision').play();
gameActive = false;
LK.showGameOver();
return;
}
}
// Check collision with bullets
for (var j = bullets.length - 1; j >= 0; j--) {
var bullet = bullets[j];
if (asteroid.intersects(bullet)) {
// Destroy both asteroid and bullet
asteroid.destroy();
asteroids.splice(i, 1);
bullet.destroy();
bullets.splice(j, 1);
// Add score based on asteroid size
updateScore(score + (asteroid.size === 'small' ? 15 : 10));
break;
}
}
}
// Check powerup collisions with player
for (var i = powerups.length - 1; i >= 0; i--) {
var powerup = powerups[i];
// Check if powerup is off screen
if (powerup.y > GAME_HEIGHT + 100) {
powerup.destroy();
powerups.splice(i, 1);
continue;
}
// Check collision with player
if (player.intersects(powerup)) {
// Apply power-up effect
switch (powerup.type) {
case 'shield':
player.activateShield(8000);
break;
case 'speed':
player.activateSpeedBoost(10000);
break;
case 'weapon':
player.activateWeaponPowerup(12000);
break;
}
LK.getSound('powerup').play();
powerup.destroy();
powerups.splice(i, 1);
updateScore(score + 5);
}
}
// Check if bullets are off screen
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
if (bullet.y < -100) {
bullet.destroy();
bullets.splice(i, 1);
}
}
}
// Handle moving the player ship
var dragging = false;
var touchOffset = {
x: 0,
y: 0
};
function handleMove(x, y, obj) {
if (dragging && player) {
player.x = Math.max(player.width / 2, Math.min(GAME_WIDTH - player.width / 2, x - touchOffset.x));
player.y = Math.max(player.height / 2, Math.min(GAME_HEIGHT - player.height / 2, y - touchOffset.y));
}
}
game.down = function (x, y, obj) {
if (!gameActive) return;
if (player && !dragging) {
// Calculate offset for smooth dragging
var playerPos = game.toLocal(player.parent.toGlobal(player.position));
touchOffset.x = x - playerPos.x;
touchOffset.y = y - playerPos.y;
dragging = true;
// Fire bullet on tap
fireBullet();
}
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragging = false;
};
game.move = handleMove;
// Main game update loop
game.update = function () {
if (!gameActive) return;
// Update player
if (player) {
player.update();
}
// Update difficulty
difficultyTimer++;
if (difficultyTimer >= 1800) {
// Every 30 seconds
difficultyLevel = Math.min(difficultyLevel + 1, 10);
difficultyTimer = 0;
}
// Spawn asteroids
var asteroidSpawnRate = Math.max(60 - difficultyLevel * 5, 20);
if (LK.ticks - lastAsteroidTime >= asteroidSpawnRate) {
spawnAsteroid();
lastAsteroidTime = LK.ticks;
}
// Spawn power-ups (less frequently)
var powerupSpawnRate = 300 - difficultyLevel * 10;
if (LK.ticks - lastPowerupTime >= powerupSpawnRate) {
if (Math.random() < 0.7) {
// 70% chance to spawn a powerup when the timer hits
spawnPowerup();
}
lastPowerupTime = LK.ticks;
}
// Update asteroids
for (var i = 0; i < asteroids.length; i++) {
asteroids[i].update();
}
// Update power-ups
for (var i = 0; i < powerups.length; i++) {
powerups[i].update();
}
// Update bullets
for (var i = 0; i < bullets.length; i++) {
bullets[i].update();
}
// Check for collisions
checkCollisions();
// Increment score based on survival time
if (LK.ticks % 60 === 0) {
// Once per second
updateScore(score + 1);
}
};
// Initialize the game
initGame();