User prompt
Please add a map in the HUD
User prompt
Please add stars to the background
User prompt
Please make the Asteroids pixel art style
User prompt
Please make the character a pixel art style space ship
User prompt
Please add aliens
Code edit (1 edits merged)
Please save this source code
User prompt
Asteroid Miner: Space Resource Rush
User prompt
Please continue polishing my design document.
Initial prompt
Please make a car racer game with multiple tracks and characters
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { credits: 0, laserLevel: 1, shieldLevel: 1, engineLevel: 1, highScore: 0 }); /**** * Classes ****/ var Asteroid = Container.expand(function () { var self = Container.call(this); self.size = 'large'; self.value = Math.floor(Math.random() * 5) + 5; self.health = 100; var asteroidGraphics = self.attachAsset('asteroid', { anchorX: 0.5, anchorY: 0.5 }); // Random rotation speed self.rotationSpeed = Math.random() * 0.02 - 0.01; // Random velocity self.vx = Math.random() * 2 - 1; self.vy = Math.random() * 3 + 1; self.update = function () { self.rotation += self.rotationSpeed; self.x += self.vx; self.y += self.vy; // Wrap around screen edges horizontally if (self.x < -100) { self.x = 2148; } if (self.x > 2148) { self.x = -100; } }; self.takeDamage = function (amount) { self.health -= amount; // Visual feedback tween(asteroidGraphics, { tint: 0xff8800 }, { duration: 100, onFinish: function onFinish() { tween(asteroidGraphics, { tint: 0xffffff }, { duration: 200 }); } }); return self.health <= 0; }; self.split = function () { var fragments = []; if (self.size === 'large') { // Create 2 smaller asteroids for (var i = 0; i < 2; i++) { var smallAsteroid = new SmallAsteroid(); smallAsteroid.x = self.x + (Math.random() * 40 - 20); smallAsteroid.y = self.y + (Math.random() * 40 - 20); smallAsteroid.vx = self.vx + (Math.random() * 2 - 1); smallAsteroid.vy = self.vy + (Math.random() * 2 - 1); fragments.push(smallAsteroid); } // Create resources for (var j = 0; j < self.value; j++) { var resource = new Resource(); resource.x = self.x + (Math.random() * 60 - 30); resource.y = self.y + (Math.random() * 60 - 30); resource.vx = Math.random() * 4 - 2; resource.vy = Math.random() * 4 - 2 + self.vy; fragments.push(resource); } } return fragments; }; return self; }); var Laser = Container.expand(function () { var self = Container.call(this); var laserGraphics = self.attachAsset('laser', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -15; self.power = 25; self.update = function () { self.y += self.speed; }; return self; }); var MiningShip = Container.expand(function () { var self = Container.call(this); var shipGraphics = self.attachAsset('miningShip', { anchorX: 0.5, anchorY: 0.5 }); var shieldGraphics = self.attachAsset('shield', { anchorX: 0.5, anchorY: 0.5, alpha: 0.3 }); self.health = 100; self.maxHealth = 100; self.speed = 5; self.laserPower = storage.laserLevel; self.shieldStrength = storage.shieldLevel; // Increase speed based on engine level self.speed += storage.engineLevel; // Make shield size based on shield level shieldGraphics.scale.set(1 + storage.shieldLevel * 0.1); self.activateShield = function () { if (self.shield > 0) { tween(shieldGraphics, { alpha: 0.5 }, { duration: 200, onFinish: function onFinish() { tween(shieldGraphics, { alpha: 0.3 }, { duration: 800 }); } }); } }; self.takeDamage = function (amount) { var actualDamage = amount / self.shieldStrength; self.health -= actualDamage; if (self.health <= 0) { self.health = 0; } // Flash red to indicate damage LK.getSound('shipDamage').play(); tween(shipGraphics, { tint: 0xff0000 }, { duration: 100, onFinish: function onFinish() { tween(shipGraphics, { tint: 0xffffff }, { duration: 200 }); } }); // Activate shield effect self.activateShield(); return self.health <= 0; }; return self; }); var Radar = Container.expand(function () { var self = Container.call(this); var background = self.attachAsset('radarBackground', { anchorX: 0.5, anchorY: 0.5, alpha: 0.5 }); self.blips = []; self.updateRadar = function (asteroids) { // Clear existing blips for (var i = 0; i < self.blips.length; i++) { self.removeChild(self.blips[i]); } self.blips = []; // Add new blips for asteroids for (var j = 0; j < asteroids.length; j++) { var asteroid = asteroids[j]; // Only show blips for asteroids that are off-screen or about to enter if (asteroid.y < -100) { var blip = LK.getAsset('radarBlip', { anchorX: 0.5, anchorY: 0.5 }); // Scale radar coordinates to fit in radar display var radarX = asteroid.x / 2048 * background.width - background.width / 2; var radarY = (asteroid.y + 300) / 800 * background.height - background.height / 2; // Clamp to radar boundaries radarY = Math.max(-background.height / 2, Math.min(radarY, background.height / 2)); radarX = Math.max(-background.width / 2, Math.min(radarX, background.width / 2)); blip.x = radarX; blip.y = radarY; // Size based on asteroid size if (asteroid.size === 'large') { blip.scale.set(1.5); } self.addChild(blip); self.blips.push(blip); } } }; return self; }); var Resource = Container.expand(function () { var self = Container.call(this); var resourceGraphics = self.attachAsset('resource', { anchorX: 0.5, anchorY: 0.5 }); self.value = Math.floor(Math.random() * 5) + 1; self.collected = false; // Set random value-based tint - higher value = more golden var colorValue = Math.min(0xFFFFFF, 0xC0C0C0 + self.value * 0x101000); resourceGraphics.tint = colorValue; // Random velocity self.vx = 0; self.vy = 0; self.update = function () { self.x += self.vx; self.y += self.vy; // Gradually slow down self.vx *= 0.98; self.vy *= 0.98; // Resource magnetized to ship when close enough if (ship && !self.collected) { var dx = ship.x - self.x; var dy = ship.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < 200) { // Apply force proportional to distance var strength = (1 - distance / 200) * 0.5; self.vx += dx * strength; self.vy += dy * strength; } } }; self.collect = function () { if (!self.collected) { self.collected = true; // Animation for collection tween(self, { alpha: 0, scaleX: 2, scaleY: 2 }, { duration: 500, easing: tween.easeOut }); return self.value; } return 0; }; return self; }); var SmallAsteroid = Container.expand(function () { var self = Container.call(this); self.size = 'small'; self.value = Math.floor(Math.random() * 3) + 1; self.health = 50; var asteroidGraphics = self.attachAsset('smallAsteroid', { anchorX: 0.5, anchorY: 0.5 }); // Random rotation speed self.rotationSpeed = Math.random() * 0.04 - 0.02; // Random velocity self.vx = Math.random() * 3 - 1.5; self.vy = Math.random() * 3 + 2; self.update = function () { self.rotation += self.rotationSpeed; self.x += self.vx; self.y += self.vy; // Wrap around screen edges horizontally if (self.x < -50) { self.x = 2098; } if (self.x > 2098) { self.x = -50; } }; self.takeDamage = function (amount) { self.health -= amount; // Visual feedback tween(asteroidGraphics, { tint: 0xff8800 }, { duration: 100, onFinish: function onFinish() { tween(asteroidGraphics, { tint: 0xffffff }, { duration: 200 }); } }); return self.health <= 0; }; self.split = function () { var fragments = []; // Create resources for (var i = 0; i < self.value; i++) { var resource = new Resource(); resource.x = self.x + (Math.random() * 30 - 15); resource.y = self.y + (Math.random() * 30 - 15); resource.vx = Math.random() * 3 - 1.5; resource.vy = Math.random() * 3 - 1.5 + self.vy; fragments.push(resource); } return fragments; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ game.setBackgroundColor(0x0A0A2A); // Deep space blue // Game state variables var ship; var lasers = []; var asteroids = []; var resources = []; var gameLevel = 1; var spawnTimer = 0; var spawnInterval = 120; // Frames between asteroid spawns var levelTimer = 0; var levelDuration = 1800; // 30 seconds at 60fps var score = 0; var credits = storage.credits || 0; var levelUpPopupVisible = false; // UI elements var scoreTxt = new Text2('Score: 0', { size: 30, fill: 0xFFFFFF }); scoreTxt.anchor.set(0, 0); LK.gui.topRight.addChild(scoreTxt); scoreTxt.x = -200; scoreTxt.y = 20; var creditsTxt = new Text2('Credits: ' + credits, { size: 30, fill: 0xFFDD00 }); creditsTxt.anchor.set(0, 0); LK.gui.topRight.addChild(creditsTxt); creditsTxt.x = -200; creditsTxt.y = 60; var levelTxt = new Text2('Level: ' + gameLevel, { size: 30, fill: 0xFFFFFF }); levelTxt.anchor.set(0, 0); LK.gui.topRight.addChild(levelTxt); levelTxt.x = -200; levelTxt.y = 100; var healthBar = new Container(); var healthBarBg = LK.getAsset('miningShip', { anchorX: 0, anchorY: 0.5, scaleX: 2, scaleY: 0.3, tint: 0x444444 }); var healthBarFill = LK.getAsset('miningShip', { anchorX: 0, anchorY: 0.5, scaleX: 2, scaleY: 0.25, tint: 0x00FF00 }); healthBar.addChild(healthBarBg); healthBar.addChild(healthBarFill); healthBar.x = 120; healthBar.y = 30; LK.gui.topLeft.addChild(healthBar); // Upgrade UI section (initially hidden) var upgradeContainer = new Container(); upgradeContainer.visible = false; LK.gui.center.addChild(upgradeContainer); var upgradePanel = LK.getAsset('radarBackground', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2.5, tint: 0x223344 }); upgradeContainer.addChild(upgradePanel); var upgradeTitle = new Text2('UPGRADE SHIP', { size: 50, fill: 0xFFFFFF }); upgradeTitle.anchor.set(0.5, 0); upgradeTitle.y = -200; upgradeContainer.addChild(upgradeTitle); var laserUpgradeBtn = new Container(); laserUpgradeBtn.y = -100; upgradeContainer.addChild(laserUpgradeBtn); var laserBtnBg = LK.getAsset('miningShip', { anchorX: 0.5, anchorY: 0.5, scaleX: 3, scaleY: 0.6, tint: 0x2ecc71 }); laserUpgradeBtn.addChild(laserBtnBg); var laserBtnTxt = new Text2('Laser Lvl ' + storage.laserLevel + ' → ' + (storage.laserLevel + 1) + ' (Cost: 100)', { size: 25, fill: 0xFFFFFF }); laserBtnTxt.anchor.set(0.5, 0.5); laserUpgradeBtn.addChild(laserBtnTxt); var shieldUpgradeBtn = new Container(); shieldUpgradeBtn.y = 0; upgradeContainer.addChild(shieldUpgradeBtn); var shieldBtnBg = LK.getAsset('miningShip', { anchorX: 0.5, anchorY: 0.5, scaleX: 3, scaleY: 0.6, tint: 0x3498db }); shieldUpgradeBtn.addChild(shieldBtnBg); var shieldBtnTxt = new Text2('Shield Lvl ' + storage.shieldLevel + ' → ' + (storage.shieldLevel + 1) + ' (Cost: 150)', { size: 25, fill: 0xFFFFFF }); shieldBtnTxt.anchor.set(0.5, 0.5); shieldUpgradeBtn.addChild(shieldBtnTxt); var engineUpgradeBtn = new Container(); engineUpgradeBtn.y = 100; upgradeContainer.addChild(engineUpgradeBtn); var engineBtnBg = LK.getAsset('miningShip', { anchorX: 0.5, anchorY: 0.5, scaleX: 3, scaleY: 0.6, tint: 0xf39c12 }); engineUpgradeBtn.addChild(engineBtnBg); var engineBtnTxt = new Text2('Engine Lvl ' + storage.engineLevel + ' → ' + (storage.engineLevel + 1) + ' (Cost: 125)', { size: 25, fill: 0xFFFFFF }); engineBtnTxt.anchor.set(0.5, 0.5); engineUpgradeBtn.addChild(engineBtnTxt); var continueBtn = new Container(); continueBtn.y = 200; upgradeContainer.addChild(continueBtn); var continueBtnBg = LK.getAsset('miningShip', { anchorX: 0.5, anchorY: 0.5, scaleX: 3, scaleY: 0.6, tint: 0xe74c3c }); continueBtn.addChild(continueBtnBg); var continueBtnTxt = new Text2('CONTINUE MINING', { size: 30, fill: 0xFFFFFF }); continueBtnTxt.anchor.set(0.5, 0.5); continueBtn.addChild(continueBtnTxt); // Radar system at bottom var radar = new Radar(); radar.x = 2048 / 2; radar.y = 2732 - 120; game.addChild(radar); // Initialize the ship function initShip() { ship = new MiningShip(); ship.x = 2048 / 2; ship.y = 2732 - 400; game.addChild(ship); } // Initialize game function initGame() { // Start with a clean slate clearGameElements(); // Load credits from storage credits = storage.credits || 0; // Initialize ship initShip(); // Reset game state gameLevel = 1; score = 0; spawnTimer = 0; levelTimer = 0; levelUpPopupVisible = false; // Update UI updateUI(); // Start background music LK.playMusic('spaceAmbience'); } // Clear all game elements function clearGameElements() { // Remove all asteroids for (var i = 0; i < asteroids.length; i++) { if (asteroids[i].parent) { asteroids[i].parent.removeChild(asteroids[i]); } } asteroids = []; // Remove all lasers for (var j = 0; j < lasers.length; j++) { if (lasers[j].parent) { lasers[j].parent.removeChild(lasers[j]); } } lasers = []; // Remove all resources for (var k = 0; k < resources.length; k++) { if (resources[k].parent) { resources[k].parent.removeChild(resources[k]); } } resources = []; // Remove ship if it exists if (ship && ship.parent) { ship.parent.removeChild(ship); } // Hide upgrade panel upgradeContainer.visible = false; } // Update UI elements function updateUI() { scoreTxt.setText('Score: ' + score); creditsTxt.setText('Credits: ' + credits); levelTxt.setText('Level: ' + gameLevel); // Update health bar if (ship) { healthBarFill.scale.x = 2 * (ship.health / ship.maxHealth); // Change color based on health if (ship.health > 70) { healthBarFill.tint = 0x00FF00; } else if (ship.health > 30) { healthBarFill.tint = 0xFFAA00; } else { healthBarFill.tint = 0xFF0000; } } // Update upgrade buttons laserBtnTxt.setText('Laser Lvl ' + storage.laserLevel + ' → ' + (storage.laserLevel + 1) + ' (Cost: 100)'); shieldBtnTxt.setText('Shield Lvl ' + storage.shieldLevel + ' → ' + (storage.shieldLevel + 1) + ' (Cost: 150)'); engineBtnTxt.setText('Engine Lvl ' + storage.engineLevel + ' → ' + (storage.engineLevel + 1) + ' (Cost: 125)'); } // Spawn an asteroid function spawnAsteroid() { var asteroid = new Asteroid(); asteroid.x = Math.random() * 2048; asteroid.y = -200; // Adjust velocity based on level asteroid.vy = 1 + gameLevel * 0.5; if (gameLevel > 5) { // More horizontal movement in higher levels asteroid.vx = (Math.random() * 3 - 1.5) * (gameLevel * 0.2); } asteroids.push(asteroid); game.addChild(asteroid); } // Fire a laser from the ship function fireLaser() { var laser = new Laser(); laser.x = ship.x; laser.y = ship.y - 40; laser.power = 25 * ship.laserPower; lasers.push(laser); game.addChild(laser); LK.getSound('laserSound').play(); } // Show upgrade UI between levels function showUpgradeUI() { upgradeContainer.visible = true; levelUpPopupVisible = true; // Update button texts laserBtnTxt.setText('Laser Lvl ' + storage.laserLevel + ' → ' + (storage.laserLevel + 1) + ' (Cost: 100)'); shieldBtnTxt.setText('Shield Lvl ' + storage.shieldLevel + ' → ' + (storage.shieldLevel + 1) + ' (Cost: 150)'); engineBtnTxt.setText('Engine Lvl ' + storage.engineLevel + ' → ' + (storage.engineLevel + 1) + ' (Cost: 125)'); // Update button colors based on affordability laserBtnBg.tint = credits >= 100 ? 0x2ecc71 : 0x95a5a6; shieldBtnBg.tint = credits >= 150 ? 0x3498db : 0x95a5a6; engineBtnBg.tint = credits >= 125 ? 0xf39c12 : 0x95a5a6; } // Hide upgrade UI function hideUpgradeUI() { upgradeContainer.visible = false; levelUpPopupVisible = false; // Reinitialize ship with new upgrades if (ship && ship.parent) { ship.parent.removeChild(ship); } initShip(); } // Handle upgrade button clicks function handleUpgradeClick(x, y) { if (!levelUpPopupVisible) { return; } // Convert global coordinates to local coordinates for the upgrade container var local = upgradeContainer.toLocal({ x: x, y: y }); // Check if laser upgrade button was clicked if (local.y > -130 && local.y < -70 && local.x > -150 && local.x < 150 && credits >= 100) { // Upgrade laser credits -= 100; storage.laserLevel += 1; storage.credits = credits; updateUI(); // Visual feedback tween(laserUpgradeBtn, { scaleX: 1.1, scaleY: 1.1 }, { duration: 100, onFinish: function onFinish() { tween(laserUpgradeBtn, { scaleX: 1, scaleY: 1 }, { duration: 100 }); } }); } // Check if shield upgrade button was clicked else if (local.y > -30 && local.y < 30 && local.x > -150 && local.x < 150 && credits >= 150) { // Upgrade shield credits -= 150; storage.shieldLevel += 1; storage.credits = credits; updateUI(); // Visual feedback tween(shieldUpgradeBtn, { scaleX: 1.1, scaleY: 1.1 }, { duration: 100, onFinish: function onFinish() { tween(shieldUpgradeBtn, { scaleX: 1, scaleY: 1 }, { duration: 100 }); } }); } // Check if engine upgrade button was clicked else if (local.y > 70 && local.y < 130 && local.x > -150 && local.x < 150 && credits >= 125) { // Upgrade engine credits -= 125; storage.engineLevel += 1; storage.credits = credits; updateUI(); // Visual feedback tween(engineUpgradeBtn, { scaleX: 1.1, scaleY: 1.1 }, { duration: 100, onFinish: function onFinish() { tween(engineUpgradeBtn, { scaleX: 1, scaleY: 1 }, { duration: 100 }); } }); } // Check if continue button was clicked else if (local.y > 170 && local.y < 230 && local.x > -150 && local.x < 150) { // Continue to next level hideUpgradeUI(); // Visual feedback tween(continueBtn, { scaleX: 1.1, scaleY: 1.1 }, { duration: 100, onFinish: function onFinish() { tween(continueBtn, { scaleX: 1, scaleY: 1 }, { duration: 100 }); } }); } } // Mouse/touch events var dragMode = false; var lastX = 0; game.down = function (x, y, obj) { // Handle upgrade UI clicks if visible if (levelUpPopupVisible) { handleUpgradeClick(x, y); return; } // Start tracking for ship movement dragMode = true; lastX = x; // Fire laser when tapping/clicking if (ship && !levelUpPopupVisible) { fireLaser(); } }; game.up = function (x, y, obj) { dragMode = false; }; game.move = function (x, y, obj) { if (dragMode && ship && !levelUpPopupVisible) { // Move ship based on drag distance var dx = x - lastX; ship.x += dx * 1.5; // Constrain to screen ship.x = Math.max(50, Math.min(ship.x, 2048 - 50)); lastX = x; } }; // Game logic update game.update = function () { if (levelUpPopupVisible) { // Only handle UI events when upgrade screen is visible return; } // Spawning logic spawnTimer++; if (spawnTimer >= spawnInterval) { spawnTimer = 0; spawnAsteroid(); // Adjust spawn rate based on level spawnInterval = Math.max(30, 120 - gameLevel * 10); } // Level progression levelTimer++; if (levelTimer >= levelDuration) { levelTimer = 0; gameLevel++; levelTxt.setText('Level: ' + gameLevel); // Show upgrade UI between levels showUpgradeUI(); // Save progress storage.credits = credits; } // Update ship if it exists if (ship) { // Constrain to screen ship.x = Math.max(50, Math.min(ship.x, 2048 - 50)); } // Update lasers for (var i = lasers.length - 1; i >= 0; i--) { var laser = lasers[i]; laser.update(); // Remove lasers that go off screen if (laser.y < -50) { game.removeChild(laser); lasers.splice(i, 1); continue; } // Check for collision with asteroids for (var j = asteroids.length - 1; j >= 0; j--) { var asteroid = asteroids[j]; if (laser.intersects(asteroid)) { // Apply damage to asteroid var destroyed = asteroid.takeDamage(laser.power); if (destroyed) { // Break asteroid LK.getSound('asteroidBreak').play(); // Get fragments (smaller asteroids and resources) var fragments = asteroid.split(); // Add fragments to the game for (var k = 0; k < fragments.length; k++) { var fragment = fragments[k]; if (fragment instanceof SmallAsteroid) { asteroids.push(fragment); game.addChild(fragment); } else if (fragment instanceof Resource) { resources.push(fragment); game.addChild(fragment); } } // Remove the destroyed asteroid game.removeChild(asteroid); asteroids.splice(j, 1); // Add to score score += 10; scoreTxt.setText('Score: ' + score); } // Remove the laser that hit game.removeChild(laser); lasers.splice(i, 1); break; } } } // Update asteroids for (var m = asteroids.length - 1; m >= 0; m--) { var asteroid = asteroids[m]; asteroid.update(); // Remove asteroids that go off screen if (asteroid.y > 2832) { game.removeChild(asteroid); asteroids.splice(m, 1); continue; } // Check collision with ship if (ship && asteroid.intersects(ship)) { // Ship takes damage based on asteroid size var damage = asteroid.size === 'large' ? 25 : 10; var gameOver = ship.takeDamage(damage); if (gameOver) { // Check if a new high score was achieved if (score > storage.highScore) { storage.highScore = score; } // Save credits earned storage.credits = credits; // Show game over screen LK.showGameOver(); return; } // Remove asteroid that hit the ship game.removeChild(asteroid); asteroids.splice(m, 1); // Flash screen to indicate damage LK.effects.flashScreen(0xFF0000, 300); continue; } } // Update resources for (var n = resources.length - 1; n >= 0; n--) { var resource = resources[n]; resource.update(); // Check if collected by ship if (ship && resource.intersects(ship)) { var value = resource.collect(); if (value > 0) { // Add to credits credits += value; creditsTxt.setText('Credits: ' + credits); // Store credits storage.credits = credits; // Play collection sound LK.getSound('collectResource').play(); // Add to score score += value; scoreTxt.setText('Score: ' + score); // Remove after animation completes LK.setTimeout(function () { if (resources.indexOf(resource) !== -1) { game.removeChild(resource); resources.splice(resources.indexOf(resource), 1); } }, 500); } } // Remove resources that go off screen if (resource.y > 2832) { game.removeChild(resource); resources.splice(n, 1); continue; } } // Update radar radar.updateRadar(asteroids); // Update UI updateUI(); }; // Start the game initGame();
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,878 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1", {
+ credits: 0,
+ laserLevel: 1,
+ shieldLevel: 1,
+ engineLevel: 1,
+ highScore: 0
+});
+
+/****
+* Classes
+****/
+var Asteroid = Container.expand(function () {
+ var self = Container.call(this);
+ self.size = 'large';
+ self.value = Math.floor(Math.random() * 5) + 5;
+ self.health = 100;
+ var asteroidGraphics = self.attachAsset('asteroid', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Random rotation speed
+ self.rotationSpeed = Math.random() * 0.02 - 0.01;
+ // Random velocity
+ self.vx = Math.random() * 2 - 1;
+ self.vy = Math.random() * 3 + 1;
+ self.update = function () {
+ self.rotation += self.rotationSpeed;
+ self.x += self.vx;
+ self.y += self.vy;
+ // Wrap around screen edges horizontally
+ if (self.x < -100) {
+ self.x = 2148;
+ }
+ if (self.x > 2148) {
+ self.x = -100;
+ }
+ };
+ self.takeDamage = function (amount) {
+ self.health -= amount;
+ // Visual feedback
+ tween(asteroidGraphics, {
+ tint: 0xff8800
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(asteroidGraphics, {
+ tint: 0xffffff
+ }, {
+ duration: 200
+ });
+ }
+ });
+ return self.health <= 0;
+ };
+ self.split = function () {
+ var fragments = [];
+ if (self.size === 'large') {
+ // Create 2 smaller asteroids
+ for (var i = 0; i < 2; i++) {
+ var smallAsteroid = new SmallAsteroid();
+ smallAsteroid.x = self.x + (Math.random() * 40 - 20);
+ smallAsteroid.y = self.y + (Math.random() * 40 - 20);
+ smallAsteroid.vx = self.vx + (Math.random() * 2 - 1);
+ smallAsteroid.vy = self.vy + (Math.random() * 2 - 1);
+ fragments.push(smallAsteroid);
+ }
+ // Create resources
+ for (var j = 0; j < self.value; j++) {
+ var resource = new Resource();
+ resource.x = self.x + (Math.random() * 60 - 30);
+ resource.y = self.y + (Math.random() * 60 - 30);
+ resource.vx = Math.random() * 4 - 2;
+ resource.vy = Math.random() * 4 - 2 + self.vy;
+ fragments.push(resource);
+ }
+ }
+ return fragments;
+ };
+ return self;
+});
+var Laser = Container.expand(function () {
+ var self = Container.call(this);
+ var laserGraphics = self.attachAsset('laser', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -15;
+ self.power = 25;
+ self.update = function () {
+ self.y += self.speed;
+ };
+ return self;
+});
+var MiningShip = Container.expand(function () {
+ var self = Container.call(this);
+ var shipGraphics = self.attachAsset('miningShip', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ var shieldGraphics = self.attachAsset('shield', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ alpha: 0.3
+ });
+ self.health = 100;
+ self.maxHealth = 100;
+ self.speed = 5;
+ self.laserPower = storage.laserLevel;
+ self.shieldStrength = storage.shieldLevel;
+ // Increase speed based on engine level
+ self.speed += storage.engineLevel;
+ // Make shield size based on shield level
+ shieldGraphics.scale.set(1 + storage.shieldLevel * 0.1);
+ self.activateShield = function () {
+ if (self.shield > 0) {
+ tween(shieldGraphics, {
+ alpha: 0.5
+ }, {
+ duration: 200,
+ onFinish: function onFinish() {
+ tween(shieldGraphics, {
+ alpha: 0.3
+ }, {
+ duration: 800
+ });
+ }
+ });
+ }
+ };
+ self.takeDamage = function (amount) {
+ var actualDamage = amount / self.shieldStrength;
+ self.health -= actualDamage;
+ if (self.health <= 0) {
+ self.health = 0;
+ }
+ // Flash red to indicate damage
+ LK.getSound('shipDamage').play();
+ tween(shipGraphics, {
+ tint: 0xff0000
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(shipGraphics, {
+ tint: 0xffffff
+ }, {
+ duration: 200
+ });
+ }
+ });
+ // Activate shield effect
+ self.activateShield();
+ return self.health <= 0;
+ };
+ return self;
+});
+var Radar = Container.expand(function () {
+ var self = Container.call(this);
+ var background = self.attachAsset('radarBackground', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ alpha: 0.5
+ });
+ self.blips = [];
+ self.updateRadar = function (asteroids) {
+ // Clear existing blips
+ for (var i = 0; i < self.blips.length; i++) {
+ self.removeChild(self.blips[i]);
+ }
+ self.blips = [];
+ // Add new blips for asteroids
+ for (var j = 0; j < asteroids.length; j++) {
+ var asteroid = asteroids[j];
+ // Only show blips for asteroids that are off-screen or about to enter
+ if (asteroid.y < -100) {
+ var blip = LK.getAsset('radarBlip', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Scale radar coordinates to fit in radar display
+ var radarX = asteroid.x / 2048 * background.width - background.width / 2;
+ var radarY = (asteroid.y + 300) / 800 * background.height - background.height / 2;
+ // Clamp to radar boundaries
+ radarY = Math.max(-background.height / 2, Math.min(radarY, background.height / 2));
+ radarX = Math.max(-background.width / 2, Math.min(radarX, background.width / 2));
+ blip.x = radarX;
+ blip.y = radarY;
+ // Size based on asteroid size
+ if (asteroid.size === 'large') {
+ blip.scale.set(1.5);
+ }
+ self.addChild(blip);
+ self.blips.push(blip);
+ }
+ }
+ };
+ return self;
+});
+var Resource = Container.expand(function () {
+ var self = Container.call(this);
+ var resourceGraphics = self.attachAsset('resource', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.value = Math.floor(Math.random() * 5) + 1;
+ self.collected = false;
+ // Set random value-based tint - higher value = more golden
+ var colorValue = Math.min(0xFFFFFF, 0xC0C0C0 + self.value * 0x101000);
+ resourceGraphics.tint = colorValue;
+ // Random velocity
+ self.vx = 0;
+ self.vy = 0;
+ self.update = function () {
+ self.x += self.vx;
+ self.y += self.vy;
+ // Gradually slow down
+ self.vx *= 0.98;
+ self.vy *= 0.98;
+ // Resource magnetized to ship when close enough
+ if (ship && !self.collected) {
+ var dx = ship.x - self.x;
+ var dy = ship.y - self.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance < 200) {
+ // Apply force proportional to distance
+ var strength = (1 - distance / 200) * 0.5;
+ self.vx += dx * strength;
+ self.vy += dy * strength;
+ }
+ }
+ };
+ self.collect = function () {
+ if (!self.collected) {
+ self.collected = true;
+ // Animation for collection
+ tween(self, {
+ alpha: 0,
+ scaleX: 2,
+ scaleY: 2
+ }, {
+ duration: 500,
+ easing: tween.easeOut
+ });
+ return self.value;
+ }
+ return 0;
+ };
+ return self;
+});
+var SmallAsteroid = Container.expand(function () {
+ var self = Container.call(this);
+ self.size = 'small';
+ self.value = Math.floor(Math.random() * 3) + 1;
+ self.health = 50;
+ var asteroidGraphics = self.attachAsset('smallAsteroid', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Random rotation speed
+ self.rotationSpeed = Math.random() * 0.04 - 0.02;
+ // Random velocity
+ self.vx = Math.random() * 3 - 1.5;
+ self.vy = Math.random() * 3 + 2;
+ self.update = function () {
+ self.rotation += self.rotationSpeed;
+ self.x += self.vx;
+ self.y += self.vy;
+ // Wrap around screen edges horizontally
+ if (self.x < -50) {
+ self.x = 2098;
+ }
+ if (self.x > 2098) {
+ self.x = -50;
+ }
+ };
+ self.takeDamage = function (amount) {
+ self.health -= amount;
+ // Visual feedback
+ tween(asteroidGraphics, {
+ tint: 0xff8800
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(asteroidGraphics, {
+ tint: 0xffffff
+ }, {
+ duration: 200
+ });
+ }
+ });
+ return self.health <= 0;
+ };
+ self.split = function () {
+ var fragments = [];
+ // Create resources
+ for (var i = 0; i < self.value; i++) {
+ var resource = new Resource();
+ resource.x = self.x + (Math.random() * 30 - 15);
+ resource.y = self.y + (Math.random() * 30 - 15);
+ resource.vx = Math.random() * 3 - 1.5;
+ resource.vy = Math.random() * 3 - 1.5 + self.vy;
+ fragments.push(resource);
+ }
+ return fragments;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x000000
-});
\ No newline at end of file
+});
+
+/****
+* Game Code
+****/
+game.setBackgroundColor(0x0A0A2A); // Deep space blue
+// Game state variables
+var ship;
+var lasers = [];
+var asteroids = [];
+var resources = [];
+var gameLevel = 1;
+var spawnTimer = 0;
+var spawnInterval = 120; // Frames between asteroid spawns
+var levelTimer = 0;
+var levelDuration = 1800; // 30 seconds at 60fps
+var score = 0;
+var credits = storage.credits || 0;
+var levelUpPopupVisible = false;
+// UI elements
+var scoreTxt = new Text2('Score: 0', {
+ size: 30,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(scoreTxt);
+scoreTxt.x = -200;
+scoreTxt.y = 20;
+var creditsTxt = new Text2('Credits: ' + credits, {
+ size: 30,
+ fill: 0xFFDD00
+});
+creditsTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(creditsTxt);
+creditsTxt.x = -200;
+creditsTxt.y = 60;
+var levelTxt = new Text2('Level: ' + gameLevel, {
+ size: 30,
+ fill: 0xFFFFFF
+});
+levelTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(levelTxt);
+levelTxt.x = -200;
+levelTxt.y = 100;
+var healthBar = new Container();
+var healthBarBg = LK.getAsset('miningShip', {
+ anchorX: 0,
+ anchorY: 0.5,
+ scaleX: 2,
+ scaleY: 0.3,
+ tint: 0x444444
+});
+var healthBarFill = LK.getAsset('miningShip', {
+ anchorX: 0,
+ anchorY: 0.5,
+ scaleX: 2,
+ scaleY: 0.25,
+ tint: 0x00FF00
+});
+healthBar.addChild(healthBarBg);
+healthBar.addChild(healthBarFill);
+healthBar.x = 120;
+healthBar.y = 30;
+LK.gui.topLeft.addChild(healthBar);
+// Upgrade UI section (initially hidden)
+var upgradeContainer = new Container();
+upgradeContainer.visible = false;
+LK.gui.center.addChild(upgradeContainer);
+var upgradePanel = LK.getAsset('radarBackground', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 2,
+ scaleY: 2.5,
+ tint: 0x223344
+});
+upgradeContainer.addChild(upgradePanel);
+var upgradeTitle = new Text2('UPGRADE SHIP', {
+ size: 50,
+ fill: 0xFFFFFF
+});
+upgradeTitle.anchor.set(0.5, 0);
+upgradeTitle.y = -200;
+upgradeContainer.addChild(upgradeTitle);
+var laserUpgradeBtn = new Container();
+laserUpgradeBtn.y = -100;
+upgradeContainer.addChild(laserUpgradeBtn);
+var laserBtnBg = LK.getAsset('miningShip', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 3,
+ scaleY: 0.6,
+ tint: 0x2ecc71
+});
+laserUpgradeBtn.addChild(laserBtnBg);
+var laserBtnTxt = new Text2('Laser Lvl ' + storage.laserLevel + ' → ' + (storage.laserLevel + 1) + ' (Cost: 100)', {
+ size: 25,
+ fill: 0xFFFFFF
+});
+laserBtnTxt.anchor.set(0.5, 0.5);
+laserUpgradeBtn.addChild(laserBtnTxt);
+var shieldUpgradeBtn = new Container();
+shieldUpgradeBtn.y = 0;
+upgradeContainer.addChild(shieldUpgradeBtn);
+var shieldBtnBg = LK.getAsset('miningShip', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 3,
+ scaleY: 0.6,
+ tint: 0x3498db
+});
+shieldUpgradeBtn.addChild(shieldBtnBg);
+var shieldBtnTxt = new Text2('Shield Lvl ' + storage.shieldLevel + ' → ' + (storage.shieldLevel + 1) + ' (Cost: 150)', {
+ size: 25,
+ fill: 0xFFFFFF
+});
+shieldBtnTxt.anchor.set(0.5, 0.5);
+shieldUpgradeBtn.addChild(shieldBtnTxt);
+var engineUpgradeBtn = new Container();
+engineUpgradeBtn.y = 100;
+upgradeContainer.addChild(engineUpgradeBtn);
+var engineBtnBg = LK.getAsset('miningShip', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 3,
+ scaleY: 0.6,
+ tint: 0xf39c12
+});
+engineUpgradeBtn.addChild(engineBtnBg);
+var engineBtnTxt = new Text2('Engine Lvl ' + storage.engineLevel + ' → ' + (storage.engineLevel + 1) + ' (Cost: 125)', {
+ size: 25,
+ fill: 0xFFFFFF
+});
+engineBtnTxt.anchor.set(0.5, 0.5);
+engineUpgradeBtn.addChild(engineBtnTxt);
+var continueBtn = new Container();
+continueBtn.y = 200;
+upgradeContainer.addChild(continueBtn);
+var continueBtnBg = LK.getAsset('miningShip', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 3,
+ scaleY: 0.6,
+ tint: 0xe74c3c
+});
+continueBtn.addChild(continueBtnBg);
+var continueBtnTxt = new Text2('CONTINUE MINING', {
+ size: 30,
+ fill: 0xFFFFFF
+});
+continueBtnTxt.anchor.set(0.5, 0.5);
+continueBtn.addChild(continueBtnTxt);
+// Radar system at bottom
+var radar = new Radar();
+radar.x = 2048 / 2;
+radar.y = 2732 - 120;
+game.addChild(radar);
+// Initialize the ship
+function initShip() {
+ ship = new MiningShip();
+ ship.x = 2048 / 2;
+ ship.y = 2732 - 400;
+ game.addChild(ship);
+}
+// Initialize game
+function initGame() {
+ // Start with a clean slate
+ clearGameElements();
+ // Load credits from storage
+ credits = storage.credits || 0;
+ // Initialize ship
+ initShip();
+ // Reset game state
+ gameLevel = 1;
+ score = 0;
+ spawnTimer = 0;
+ levelTimer = 0;
+ levelUpPopupVisible = false;
+ // Update UI
+ updateUI();
+ // Start background music
+ LK.playMusic('spaceAmbience');
+}
+// Clear all game elements
+function clearGameElements() {
+ // Remove all asteroids
+ for (var i = 0; i < asteroids.length; i++) {
+ if (asteroids[i].parent) {
+ asteroids[i].parent.removeChild(asteroids[i]);
+ }
+ }
+ asteroids = [];
+ // Remove all lasers
+ for (var j = 0; j < lasers.length; j++) {
+ if (lasers[j].parent) {
+ lasers[j].parent.removeChild(lasers[j]);
+ }
+ }
+ lasers = [];
+ // Remove all resources
+ for (var k = 0; k < resources.length; k++) {
+ if (resources[k].parent) {
+ resources[k].parent.removeChild(resources[k]);
+ }
+ }
+ resources = [];
+ // Remove ship if it exists
+ if (ship && ship.parent) {
+ ship.parent.removeChild(ship);
+ }
+ // Hide upgrade panel
+ upgradeContainer.visible = false;
+}
+// Update UI elements
+function updateUI() {
+ scoreTxt.setText('Score: ' + score);
+ creditsTxt.setText('Credits: ' + credits);
+ levelTxt.setText('Level: ' + gameLevel);
+ // Update health bar
+ if (ship) {
+ healthBarFill.scale.x = 2 * (ship.health / ship.maxHealth);
+ // Change color based on health
+ if (ship.health > 70) {
+ healthBarFill.tint = 0x00FF00;
+ } else if (ship.health > 30) {
+ healthBarFill.tint = 0xFFAA00;
+ } else {
+ healthBarFill.tint = 0xFF0000;
+ }
+ }
+ // Update upgrade buttons
+ laserBtnTxt.setText('Laser Lvl ' + storage.laserLevel + ' → ' + (storage.laserLevel + 1) + ' (Cost: 100)');
+ shieldBtnTxt.setText('Shield Lvl ' + storage.shieldLevel + ' → ' + (storage.shieldLevel + 1) + ' (Cost: 150)');
+ engineBtnTxt.setText('Engine Lvl ' + storage.engineLevel + ' → ' + (storage.engineLevel + 1) + ' (Cost: 125)');
+}
+// Spawn an asteroid
+function spawnAsteroid() {
+ var asteroid = new Asteroid();
+ asteroid.x = Math.random() * 2048;
+ asteroid.y = -200;
+ // Adjust velocity based on level
+ asteroid.vy = 1 + gameLevel * 0.5;
+ if (gameLevel > 5) {
+ // More horizontal movement in higher levels
+ asteroid.vx = (Math.random() * 3 - 1.5) * (gameLevel * 0.2);
+ }
+ asteroids.push(asteroid);
+ game.addChild(asteroid);
+}
+// Fire a laser from the ship
+function fireLaser() {
+ var laser = new Laser();
+ laser.x = ship.x;
+ laser.y = ship.y - 40;
+ laser.power = 25 * ship.laserPower;
+ lasers.push(laser);
+ game.addChild(laser);
+ LK.getSound('laserSound').play();
+}
+// Show upgrade UI between levels
+function showUpgradeUI() {
+ upgradeContainer.visible = true;
+ levelUpPopupVisible = true;
+ // Update button texts
+ laserBtnTxt.setText('Laser Lvl ' + storage.laserLevel + ' → ' + (storage.laserLevel + 1) + ' (Cost: 100)');
+ shieldBtnTxt.setText('Shield Lvl ' + storage.shieldLevel + ' → ' + (storage.shieldLevel + 1) + ' (Cost: 150)');
+ engineBtnTxt.setText('Engine Lvl ' + storage.engineLevel + ' → ' + (storage.engineLevel + 1) + ' (Cost: 125)');
+ // Update button colors based on affordability
+ laserBtnBg.tint = credits >= 100 ? 0x2ecc71 : 0x95a5a6;
+ shieldBtnBg.tint = credits >= 150 ? 0x3498db : 0x95a5a6;
+ engineBtnBg.tint = credits >= 125 ? 0xf39c12 : 0x95a5a6;
+}
+// Hide upgrade UI
+function hideUpgradeUI() {
+ upgradeContainer.visible = false;
+ levelUpPopupVisible = false;
+ // Reinitialize ship with new upgrades
+ if (ship && ship.parent) {
+ ship.parent.removeChild(ship);
+ }
+ initShip();
+}
+// Handle upgrade button clicks
+function handleUpgradeClick(x, y) {
+ if (!levelUpPopupVisible) {
+ return;
+ }
+ // Convert global coordinates to local coordinates for the upgrade container
+ var local = upgradeContainer.toLocal({
+ x: x,
+ y: y
+ });
+ // Check if laser upgrade button was clicked
+ if (local.y > -130 && local.y < -70 && local.x > -150 && local.x < 150 && credits >= 100) {
+ // Upgrade laser
+ credits -= 100;
+ storage.laserLevel += 1;
+ storage.credits = credits;
+ updateUI();
+ // Visual feedback
+ tween(laserUpgradeBtn, {
+ scaleX: 1.1,
+ scaleY: 1.1
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(laserUpgradeBtn, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 100
+ });
+ }
+ });
+ }
+ // Check if shield upgrade button was clicked
+ else if (local.y > -30 && local.y < 30 && local.x > -150 && local.x < 150 && credits >= 150) {
+ // Upgrade shield
+ credits -= 150;
+ storage.shieldLevel += 1;
+ storage.credits = credits;
+ updateUI();
+ // Visual feedback
+ tween(shieldUpgradeBtn, {
+ scaleX: 1.1,
+ scaleY: 1.1
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(shieldUpgradeBtn, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 100
+ });
+ }
+ });
+ }
+ // Check if engine upgrade button was clicked
+ else if (local.y > 70 && local.y < 130 && local.x > -150 && local.x < 150 && credits >= 125) {
+ // Upgrade engine
+ credits -= 125;
+ storage.engineLevel += 1;
+ storage.credits = credits;
+ updateUI();
+ // Visual feedback
+ tween(engineUpgradeBtn, {
+ scaleX: 1.1,
+ scaleY: 1.1
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(engineUpgradeBtn, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 100
+ });
+ }
+ });
+ }
+ // Check if continue button was clicked
+ else if (local.y > 170 && local.y < 230 && local.x > -150 && local.x < 150) {
+ // Continue to next level
+ hideUpgradeUI();
+ // Visual feedback
+ tween(continueBtn, {
+ scaleX: 1.1,
+ scaleY: 1.1
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(continueBtn, {
+ scaleX: 1,
+ scaleY: 1
+ }, {
+ duration: 100
+ });
+ }
+ });
+ }
+}
+// Mouse/touch events
+var dragMode = false;
+var lastX = 0;
+game.down = function (x, y, obj) {
+ // Handle upgrade UI clicks if visible
+ if (levelUpPopupVisible) {
+ handleUpgradeClick(x, y);
+ return;
+ }
+ // Start tracking for ship movement
+ dragMode = true;
+ lastX = x;
+ // Fire laser when tapping/clicking
+ if (ship && !levelUpPopupVisible) {
+ fireLaser();
+ }
+};
+game.up = function (x, y, obj) {
+ dragMode = false;
+};
+game.move = function (x, y, obj) {
+ if (dragMode && ship && !levelUpPopupVisible) {
+ // Move ship based on drag distance
+ var dx = x - lastX;
+ ship.x += dx * 1.5;
+ // Constrain to screen
+ ship.x = Math.max(50, Math.min(ship.x, 2048 - 50));
+ lastX = x;
+ }
+};
+// Game logic update
+game.update = function () {
+ if (levelUpPopupVisible) {
+ // Only handle UI events when upgrade screen is visible
+ return;
+ }
+ // Spawning logic
+ spawnTimer++;
+ if (spawnTimer >= spawnInterval) {
+ spawnTimer = 0;
+ spawnAsteroid();
+ // Adjust spawn rate based on level
+ spawnInterval = Math.max(30, 120 - gameLevel * 10);
+ }
+ // Level progression
+ levelTimer++;
+ if (levelTimer >= levelDuration) {
+ levelTimer = 0;
+ gameLevel++;
+ levelTxt.setText('Level: ' + gameLevel);
+ // Show upgrade UI between levels
+ showUpgradeUI();
+ // Save progress
+ storage.credits = credits;
+ }
+ // Update ship if it exists
+ if (ship) {
+ // Constrain to screen
+ ship.x = Math.max(50, Math.min(ship.x, 2048 - 50));
+ }
+ // Update lasers
+ for (var i = lasers.length - 1; i >= 0; i--) {
+ var laser = lasers[i];
+ laser.update();
+ // Remove lasers that go off screen
+ if (laser.y < -50) {
+ game.removeChild(laser);
+ lasers.splice(i, 1);
+ continue;
+ }
+ // Check for collision with asteroids
+ for (var j = asteroids.length - 1; j >= 0; j--) {
+ var asteroid = asteroids[j];
+ if (laser.intersects(asteroid)) {
+ // Apply damage to asteroid
+ var destroyed = asteroid.takeDamage(laser.power);
+ if (destroyed) {
+ // Break asteroid
+ LK.getSound('asteroidBreak').play();
+ // Get fragments (smaller asteroids and resources)
+ var fragments = asteroid.split();
+ // Add fragments to the game
+ for (var k = 0; k < fragments.length; k++) {
+ var fragment = fragments[k];
+ if (fragment instanceof SmallAsteroid) {
+ asteroids.push(fragment);
+ game.addChild(fragment);
+ } else if (fragment instanceof Resource) {
+ resources.push(fragment);
+ game.addChild(fragment);
+ }
+ }
+ // Remove the destroyed asteroid
+ game.removeChild(asteroid);
+ asteroids.splice(j, 1);
+ // Add to score
+ score += 10;
+ scoreTxt.setText('Score: ' + score);
+ }
+ // Remove the laser that hit
+ game.removeChild(laser);
+ lasers.splice(i, 1);
+ break;
+ }
+ }
+ }
+ // Update asteroids
+ for (var m = asteroids.length - 1; m >= 0; m--) {
+ var asteroid = asteroids[m];
+ asteroid.update();
+ // Remove asteroids that go off screen
+ if (asteroid.y > 2832) {
+ game.removeChild(asteroid);
+ asteroids.splice(m, 1);
+ continue;
+ }
+ // Check collision with ship
+ if (ship && asteroid.intersects(ship)) {
+ // Ship takes damage based on asteroid size
+ var damage = asteroid.size === 'large' ? 25 : 10;
+ var gameOver = ship.takeDamage(damage);
+ if (gameOver) {
+ // Check if a new high score was achieved
+ if (score > storage.highScore) {
+ storage.highScore = score;
+ }
+ // Save credits earned
+ storage.credits = credits;
+ // Show game over screen
+ LK.showGameOver();
+ return;
+ }
+ // Remove asteroid that hit the ship
+ game.removeChild(asteroid);
+ asteroids.splice(m, 1);
+ // Flash screen to indicate damage
+ LK.effects.flashScreen(0xFF0000, 300);
+ continue;
+ }
+ }
+ // Update resources
+ for (var n = resources.length - 1; n >= 0; n--) {
+ var resource = resources[n];
+ resource.update();
+ // Check if collected by ship
+ if (ship && resource.intersects(ship)) {
+ var value = resource.collect();
+ if (value > 0) {
+ // Add to credits
+ credits += value;
+ creditsTxt.setText('Credits: ' + credits);
+ // Store credits
+ storage.credits = credits;
+ // Play collection sound
+ LK.getSound('collectResource').play();
+ // Add to score
+ score += value;
+ scoreTxt.setText('Score: ' + score);
+ // Remove after animation completes
+ LK.setTimeout(function () {
+ if (resources.indexOf(resource) !== -1) {
+ game.removeChild(resource);
+ resources.splice(resources.indexOf(resource), 1);
+ }
+ }, 500);
+ }
+ }
+ // Remove resources that go off screen
+ if (resource.y > 2832) {
+ game.removeChild(resource);
+ resources.splice(n, 1);
+ continue;
+ }
+ }
+ // Update radar
+ radar.updateRadar(asteroids);
+ // Update UI
+ updateUI();
+};
+// Start the game
+initGame();
\ No newline at end of file