User prompt
Increase number of available ships to 4
User prompt
As the game goes on, make asteroids more frequent
User prompt
Allow defensive ships to be selected after placement but not moved. Instead, when selected after placement, allow the player to select any point in the solar system and have the lasers shoot toward that point. If the lasers hit an asteroid, destroy the asteroid. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Only allow defensive ships to be placed once and not moved again.
User prompt
When selected, allow defensive ships to be dragged to a location in the solar system. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Increase size of defensive ships
User prompt
Allow defensive ships to be selected. When selected, make the asset more transparent so the player knows it has been selected. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Put a defensive ship below the score/lives display
User prompt
Create new asset for defensive ships
User prompt
Create a new asset for ships instead of using bullet asset
User prompt
Allow ships to be selected and then dragged when selected
User prompt
Allow ships to be dragged to place
User prompt
Increase size of ships
User prompt
Below score and lives, put 4 ships that can be selected and then placed about the solar system. These ships can shoot lasers and destroy asteroids. When selected after being placed, and then a point in the solar system is clicked, the ship turns and the lasers point in the direction of that point. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Center the score and lives displays
User prompt
Put the lives display next to the score
User prompt
Do not sway asteroid trajectory
User prompt
Only flash red when Earth is hit ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Give the player four “lives”, displayed in the bottom right corner
User prompt
Add the other four planets too
User prompt
Remove the defensive ships
Code edit (1 edits merged)
Please save this source code
User prompt
Solar System Defender
User prompt
Instead of earth at the center, put the Sun at the center. The goal is still to protect the Earth but all of the planets are in orbit.
Initial prompt
I have an idea for a tower defense-style game, but it’s space themed. So asteroids fly through the solar system and the player has to deflect them from hitting earth.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Asteroid = Container.expand(function () { var self = Container.call(this); var asteroidGraphics = self.attachAsset('asteroid', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.targetPlanet = null; self.update = function () { self.x += self.velocityX; self.y += self.velocityY; asteroidGraphics.rotation += 0.05; }; return self; }); var Bullet = Container.expand(function () { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.velocityX = 0; self.velocityY = 0; self.update = function () { self.x += self.velocityX; self.y += self.velocityY; }; return self; }); var Planet = Container.expand(function (size, color, orbitRadius, orbitSpeed) { var self = Container.call(this); self.orbitRadius = orbitRadius; self.orbitSpeed = orbitSpeed; self.angle = Math.random() * Math.PI * 2; var assetName = 'mercury'; if (color === 0x4169e1) assetName = 'earth';else if (color === 0xff4500) assetName = 'mars';else if (color === 0xffc649) assetName = 'venus';else if (color === 0xd2691e) assetName = 'jupiter';else if (color === 0xffd700) assetName = 'saturn';else if (color === 0x40e0d0) assetName = 'uranus';else if (color === 0x4169e1 && size === 85) assetName = 'neptune'; var planetGraphics = self.attachAsset(assetName, { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { self.angle += self.orbitSpeed; self.x = sunX + Math.cos(self.angle) * self.orbitRadius; self.y = sunY + Math.sin(self.angle) * self.orbitRadius; }; return self; }); var Ship = Container.expand(function () { var self = Container.call(this); var shipGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5, scaleX: 2, scaleY: 2 }); self.placed = false; self.selected = false; self.targetAngle = 0; self.shootTimer = 0; self.down = function (x, y, obj) { if (!self.placed) { self.selected = true; selectedShip = self; } }; self.update = function () { if (self.placed) { self.shootTimer++; if (self.shootTimer > 60) { var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y; var speed = 5; bullet.velocityX = Math.cos(self.targetAngle) * speed; bullet.velocityY = Math.sin(self.targetAngle) * speed; bullets.push(bullet); game.addChild(bullet); self.shootTimer = 0; } shipGraphics.rotation = self.targetAngle; } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000011 }); /**** * Game Code ****/ var sunX = 2048 / 2; var sunY = 2732 / 2; var sun = game.addChild(LK.getAsset('sun', { anchorX: 0.5, anchorY: 0.5, x: sunX, y: sunY })); var planets = []; var mercury = new Planet(50, 0x8c7853, 150, 0.015); var venus = new Planet(70, 0xffc649, 200, 0.012); var earth = new Planet(80, 0x4169e1, 300, 0.008); var mars = new Planet(60, 0xff4500, 450, 0.005); var jupiter = new Planet(120, 0xd2691e, 600, 0.003); var saturn = new Planet(100, 0xffd700, 750, 0.002); var uranus = new Planet(90, 0x40e0d0, 900, 0.0015); var neptune = new Planet(85, 0x4169e1, 1050, 0.001); planets.push(mercury); planets.push(venus); planets.push(earth); planets.push(mars); planets.push(jupiter); planets.push(saturn); planets.push(uranus); planets.push(neptune); for (var i = 0; i < planets.length; i++) { game.addChild(planets[i]); } var bullets = []; var asteroids = []; var asteroidSpawnTimer = 0; var lives = 4; var scoreTxt = new Text2('Score: 0', { size: 60, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); scoreTxt.x = -100; LK.gui.top.addChild(scoreTxt); var livesTxt = new Text2('Lives: 4', { size: 60, fill: 0xFFFFFF }); livesTxt.anchor.set(0, 0); livesTxt.x = 100; LK.gui.top.addChild(livesTxt); var ships = []; var selectedShip = null; var shipSlots = []; // Create 4 ship slots below score and lives for (var i = 0; i < 4; i++) { var ship = new Ship(); ship.x = -150 + i * 75; ship.y = 120; ships.push(ship); LK.gui.top.addChild(ship); } game.down = function (x, y, obj) { if (selectedShip && !selectedShip.placed) { // Place the ship selectedShip.destroy(); var newShip = new Ship(); newShip.x = x; newShip.y = y; newShip.placed = true; newShip.selected = false; game.addChild(newShip); ships.push(newShip); selectedShip = null; } else { // Check if clicking on a placed ship for (var i = 0; i < ships.length; i++) { if (ships[i].placed && ships[i].intersects({ x: x, y: y, width: 10, height: 10 })) { selectedShip = ships[i]; selectedShip.selected = true; return; } } // If a ship is selected and we click elsewhere, aim it if (selectedShip && selectedShip.placed) { var dx = x - selectedShip.x; var dy = y - selectedShip.y; selectedShip.targetAngle = Math.atan2(dy, dx); tween(selectedShip, { rotation: selectedShip.targetAngle }, { duration: 200 }); selectedShip.selected = false; selectedShip = null; } } }; game.update = function () { for (var i = 0; i < planets.length; i++) { planets[i].update(); } for (var i = 0; i < ships.length; i++) { if (ships[i].placed) { ships[i].update(); } } asteroidSpawnTimer++; if (asteroidSpawnTimer > 120) { var asteroid = new Asteroid(); var spawnAngle = Math.random() * Math.PI * 2; var spawnDistance = 1500; asteroid.x = sunX + Math.cos(spawnAngle) * spawnDistance; asteroid.y = sunY + Math.sin(spawnAngle) * spawnDistance; // Set initial velocity toward Earth's current position var dx = earth.x - asteroid.x; var dy = earth.y - asteroid.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance > 0) { asteroid.velocityX = dx / distance * 2; asteroid.velocityY = dy / distance * 2; } asteroids.push(asteroid); game.addChild(asteroid); asteroidSpawnTimer = 0; } for (var i = bullets.length - 1; i >= 0; i--) { var bullet = bullets[i]; if (bullet.x < -50 || bullet.x > 2098 || bullet.y < -50 || bullet.y > 2782) { bullet.destroy(); bullets.splice(i, 1); continue; } for (var j = asteroids.length - 1; j >= 0; j--) { if (bullet.intersects(asteroids[j])) { LK.setScore(LK.getScore() + 10); scoreTxt.setText('Score: ' + LK.getScore()); LK.getSound('explosion').play(); bullet.destroy(); bullets.splice(i, 1); asteroids[j].destroy(); asteroids.splice(j, 1); break; } } } for (var i = asteroids.length - 1; i >= 0; i--) { var asteroid = asteroids[i]; for (var j = 0; j < planets.length; j++) { if (asteroid.intersects(planets[j])) { if (planets[j] === earth) { LK.effects.flashScreen(0xff0000, 1000); lives--; livesTxt.setText('Lives: ' + lives); if (lives <= 0) { LK.showGameOver(); return; } } asteroid.destroy(); asteroids.splice(i, 1); break; } } var distanceFromSun = Math.sqrt(Math.pow(asteroid.x - sunX, 2) + Math.pow(asteroid.y - sunY, 2)); if (distanceFromSun < 120) { asteroid.destroy(); asteroids.splice(i, 1); } } };
===================================================================
--- original.js
+++ change.js
@@ -53,8 +53,45 @@
self.y = sunY + Math.sin(self.angle) * self.orbitRadius;
};
return self;
});
+var Ship = Container.expand(function () {
+ var self = Container.call(this);
+ var shipGraphics = self.attachAsset('bullet', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 2,
+ scaleY: 2
+ });
+ self.placed = false;
+ self.selected = false;
+ self.targetAngle = 0;
+ self.shootTimer = 0;
+ self.down = function (x, y, obj) {
+ if (!self.placed) {
+ self.selected = true;
+ selectedShip = self;
+ }
+ };
+ self.update = function () {
+ if (self.placed) {
+ self.shootTimer++;
+ if (self.shootTimer > 60) {
+ var bullet = new Bullet();
+ bullet.x = self.x;
+ bullet.y = self.y;
+ var speed = 5;
+ bullet.velocityX = Math.cos(self.targetAngle) * speed;
+ bullet.velocityY = Math.sin(self.targetAngle) * speed;
+ bullets.push(bullet);
+ game.addChild(bullet);
+ self.shootTimer = 0;
+ }
+ shipGraphics.rotation = self.targetAngle;
+ }
+ };
+ return self;
+});
/****
* Initialize Game
****/
@@ -110,12 +147,69 @@
});
livesTxt.anchor.set(0, 0);
livesTxt.x = 100;
LK.gui.top.addChild(livesTxt);
+var ships = [];
+var selectedShip = null;
+var shipSlots = [];
+// Create 4 ship slots below score and lives
+for (var i = 0; i < 4; i++) {
+ var ship = new Ship();
+ ship.x = -150 + i * 75;
+ ship.y = 120;
+ ships.push(ship);
+ LK.gui.top.addChild(ship);
+}
+game.down = function (x, y, obj) {
+ if (selectedShip && !selectedShip.placed) {
+ // Place the ship
+ selectedShip.destroy();
+ var newShip = new Ship();
+ newShip.x = x;
+ newShip.y = y;
+ newShip.placed = true;
+ newShip.selected = false;
+ game.addChild(newShip);
+ ships.push(newShip);
+ selectedShip = null;
+ } else {
+ // Check if clicking on a placed ship
+ for (var i = 0; i < ships.length; i++) {
+ if (ships[i].placed && ships[i].intersects({
+ x: x,
+ y: y,
+ width: 10,
+ height: 10
+ })) {
+ selectedShip = ships[i];
+ selectedShip.selected = true;
+ return;
+ }
+ }
+ // If a ship is selected and we click elsewhere, aim it
+ if (selectedShip && selectedShip.placed) {
+ var dx = x - selectedShip.x;
+ var dy = y - selectedShip.y;
+ selectedShip.targetAngle = Math.atan2(dy, dx);
+ tween(selectedShip, {
+ rotation: selectedShip.targetAngle
+ }, {
+ duration: 200
+ });
+ selectedShip.selected = false;
+ selectedShip = null;
+ }
+ }
+};
game.update = function () {
for (var i = 0; i < planets.length; i++) {
planets[i].update();
}
+ for (var i = 0; i < ships.length; i++) {
+ if (ships[i].placed) {
+ ships[i].update();
+ }
+ }
asteroidSpawnTimer++;
if (asteroidSpawnTimer > 120) {
var asteroid = new Asteroid();
var spawnAngle = Math.random() * Math.PI * 2;