User prompt
Our aim is to bring the ship to the landing area from above, and the shots will be downwards.
User prompt
ship starts from the top, landing area is always at the lower part, asteroid can comes only one
Code edit (1 edits merged)
Please save this source code
User prompt
Asteroid Landing Challenge
Initial prompt
its gonna be a space game, make a space ship, a platform for landing for the ship, make a fire system for ship, an a asteroid for crash the ship
/****
* 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 = (Math.random() - 0.5) * 4;
self.velocityY = Math.random() * 3 + 2;
self.rotationSpeed = (Math.random() - 0.5) * 0.2;
self.update = function () {
self.x += self.velocityX;
self.y += self.velocityY;
self.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 = -12;
self.update = function () {
self.y += self.speed;
};
return self;
});
var Platform = Container.expand(function () {
var self = Container.call(this);
var platformGraphics = self.attachAsset('platform', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityY = 2;
self.hasLanded = false;
self.update = function () {
self.y += self.velocityY;
};
return self;
});
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.velocityX = 0;
self.velocityY = 0;
self.maxSpeed = 8;
self.friction = 0.85;
self.update = function () {
// Apply velocity
self.x += self.velocityX;
self.y += self.velocityY;
// Apply friction
self.velocityX *= self.friction;
self.velocityY *= self.friction;
// Keep ship within bounds
if (self.x < 40) self.x = 40;
if (self.x > 2008) self.x = 2008;
if (self.y < 60) self.y = 60;
if (self.y > 2672) self.y = 2672;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000011
});
/****
* Game Code
****/
var spaceship = game.addChild(new Spaceship());
spaceship.x = 1024;
spaceship.y = 2400;
var asteroids = [];
var bullets = [];
var platforms = [];
var dragNode = null;
var lastTouchTime = 0;
var difficultyLevel = 1;
var asteroidSpawnRate = 180; // Initial spawn rate in ticks
// Score display
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Lives display
var livesTxt = new Text2('Lives: 3', {
size: 60,
fill: 0xFFFFFF
});
livesTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(livesTxt);
var lives = 3;
function updateScore() {
scoreTxt.setText('Score: ' + LK.getScore());
}
function updateLives() {
livesTxt.setText('Lives: ' + lives);
}
function spawnAsteroid() {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -50;
asteroids.push(asteroid);
game.addChild(asteroid);
}
function spawnPlatform() {
var platform = new Platform();
platform.x = Math.random() * (2048 - 200) + 100;
platform.y = -50;
platforms.push(platform);
game.addChild(platform);
}
function fireBullet() {
var bullet = new Bullet();
bullet.x = spaceship.x;
bullet.y = spaceship.y - 60;
bullet.lastY = bullet.y;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
}
function handleMove(x, y, obj) {
if (dragNode) {
var deltaX = x - dragNode.x;
var deltaY = y - dragNode.y;
dragNode.velocityX += deltaX * 0.3;
dragNode.velocityY += deltaY * 0.3;
// Limit velocity
if (dragNode.velocityX > dragNode.maxSpeed) dragNode.velocityX = dragNode.maxSpeed;
if (dragNode.velocityX < -dragNode.maxSpeed) dragNode.velocityX = -dragNode.maxSpeed;
if (dragNode.velocityY > dragNode.maxSpeed) dragNode.velocityY = dragNode.maxSpeed;
if (dragNode.velocityY < -dragNode.maxSpeed) dragNode.velocityY = -dragNode.maxSpeed;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
var currentTime = Date.now();
// Check for tap to shoot (if not dragging and within reasonable time)
if (!dragNode && currentTime - lastTouchTime > 200) {
fireBullet();
}
// Start dragging spaceship
dragNode = spaceship;
lastTouchTime = currentTime;
};
game.up = function (x, y, obj) {
dragNode = null;
};
game.update = function () {
// Increase difficulty over time
if (LK.ticks % 1800 == 0) {
// Every 30 seconds
difficultyLevel++;
asteroidSpawnRate = Math.max(60, asteroidSpawnRate - 20);
}
// Spawn asteroids
if (LK.ticks % asteroidSpawnRate == 0) {
spawnAsteroid();
}
// Spawn platforms occasionally
if (LK.ticks % 300 == 0) {
spawnPlatform();
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// Remove bullets that go off screen
if (bullet.lastY >= -30 && bullet.y < -30) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}
bullet.lastY = bullet.y;
}
// Update asteroids
for (var j = asteroids.length - 1; j >= 0; j--) {
var asteroid = asteroids[j];
// Remove asteroids that go off screen
if (asteroid.y > 2800) {
asteroid.destroy();
asteroids.splice(j, 1);
continue;
}
// Check collision with spaceship
if (asteroid.intersects(spaceship)) {
LK.effects.flashScreen(0xff0000, 500);
lives--;
updateLives();
// Remove asteroid
asteroid.destroy();
asteroids.splice(j, 1);
if (lives <= 0) {
LK.showGameOver();
return;
}
continue;
}
// Check collision with bullets
for (var k = bullets.length - 1; k >= 0; k--) {
var bullet = bullets[k];
if (bullet.intersects(asteroid)) {
// Destroy both asteroid and bullet
LK.setScore(LK.getScore() + 10);
updateScore();
LK.getSound('explosion').play();
asteroid.destroy();
asteroids.splice(j, 1);
bullet.destroy();
bullets.splice(k, 1);
break;
}
}
}
// Update platforms
for (var l = platforms.length - 1; l >= 0; l--) {
var platform = platforms[l];
// Remove platforms that go off screen
if (platform.y > 2800) {
platform.destroy();
platforms.splice(l, 1);
continue;
}
// Check landing on platform
if (!platform.hasLanded && platform.intersects(spaceship)) {
// Successful landing
var landingSpeed = Math.abs(spaceship.velocityY);
if (landingSpeed < 4) {
// Gentle landing - award points
LK.setScore(LK.getScore() + 50);
updateScore();
LK.getSound('landing').play();
platform.hasLanded = true;
// Flash platform green
LK.effects.flashObject(platform, 0x00ff00, 1000);
} else {
// Hard landing - lose life
LK.effects.flashScreen(0xff0000, 500);
lives--;
updateLives();
platform.destroy();
platforms.splice(l, 1);
if (lives <= 0) {
LK.showGameOver();
return;
}
}
}
}
// Check win condition
if (LK.getScore() >= 1000) {
LK.showYouWin();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,271 @@
-/****
+/****
+* 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 = (Math.random() - 0.5) * 4;
+ self.velocityY = Math.random() * 3 + 2;
+ self.rotationSpeed = (Math.random() - 0.5) * 0.2;
+ self.update = function () {
+ self.x += self.velocityX;
+ self.y += self.velocityY;
+ self.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 = -12;
+ self.update = function () {
+ self.y += self.speed;
+ };
+ return self;
+});
+var Platform = Container.expand(function () {
+ var self = Container.call(this);
+ var platformGraphics = self.attachAsset('platform', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.velocityY = 2;
+ self.hasLanded = false;
+ self.update = function () {
+ self.y += self.velocityY;
+ };
+ return self;
+});
+var Spaceship = Container.expand(function () {
+ var self = Container.call(this);
+ var shipGraphics = self.attachAsset('spaceship', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.velocityX = 0;
+ self.velocityY = 0;
+ self.maxSpeed = 8;
+ self.friction = 0.85;
+ self.update = function () {
+ // Apply velocity
+ self.x += self.velocityX;
+ self.y += self.velocityY;
+ // Apply friction
+ self.velocityX *= self.friction;
+ self.velocityY *= self.friction;
+ // Keep ship within bounds
+ if (self.x < 40) self.x = 40;
+ if (self.x > 2008) self.x = 2008;
+ if (self.y < 60) self.y = 60;
+ if (self.y > 2672) self.y = 2672;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x000011
+});
+
+/****
+* Game Code
+****/
+var spaceship = game.addChild(new Spaceship());
+spaceship.x = 1024;
+spaceship.y = 2400;
+var asteroids = [];
+var bullets = [];
+var platforms = [];
+var dragNode = null;
+var lastTouchTime = 0;
+var difficultyLevel = 1;
+var asteroidSpawnRate = 180; // Initial spawn rate in ticks
+// Score display
+var scoreTxt = new Text2('Score: 0', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreTxt);
+// Lives display
+var livesTxt = new Text2('Lives: 3', {
+ size: 60,
+ fill: 0xFFFFFF
+});
+livesTxt.anchor.set(1, 0);
+LK.gui.topRight.addChild(livesTxt);
+var lives = 3;
+function updateScore() {
+ scoreTxt.setText('Score: ' + LK.getScore());
+}
+function updateLives() {
+ livesTxt.setText('Lives: ' + lives);
+}
+function spawnAsteroid() {
+ var asteroid = new Asteroid();
+ asteroid.x = Math.random() * 2048;
+ asteroid.y = -50;
+ asteroids.push(asteroid);
+ game.addChild(asteroid);
+}
+function spawnPlatform() {
+ var platform = new Platform();
+ platform.x = Math.random() * (2048 - 200) + 100;
+ platform.y = -50;
+ platforms.push(platform);
+ game.addChild(platform);
+}
+function fireBullet() {
+ var bullet = new Bullet();
+ bullet.x = spaceship.x;
+ bullet.y = spaceship.y - 60;
+ bullet.lastY = bullet.y;
+ bullets.push(bullet);
+ game.addChild(bullet);
+ LK.getSound('shoot').play();
+}
+function handleMove(x, y, obj) {
+ if (dragNode) {
+ var deltaX = x - dragNode.x;
+ var deltaY = y - dragNode.y;
+ dragNode.velocityX += deltaX * 0.3;
+ dragNode.velocityY += deltaY * 0.3;
+ // Limit velocity
+ if (dragNode.velocityX > dragNode.maxSpeed) dragNode.velocityX = dragNode.maxSpeed;
+ if (dragNode.velocityX < -dragNode.maxSpeed) dragNode.velocityX = -dragNode.maxSpeed;
+ if (dragNode.velocityY > dragNode.maxSpeed) dragNode.velocityY = dragNode.maxSpeed;
+ if (dragNode.velocityY < -dragNode.maxSpeed) dragNode.velocityY = -dragNode.maxSpeed;
+ }
+}
+game.move = handleMove;
+game.down = function (x, y, obj) {
+ var currentTime = Date.now();
+ // Check for tap to shoot (if not dragging and within reasonable time)
+ if (!dragNode && currentTime - lastTouchTime > 200) {
+ fireBullet();
+ }
+ // Start dragging spaceship
+ dragNode = spaceship;
+ lastTouchTime = currentTime;
+};
+game.up = function (x, y, obj) {
+ dragNode = null;
+};
+game.update = function () {
+ // Increase difficulty over time
+ if (LK.ticks % 1800 == 0) {
+ // Every 30 seconds
+ difficultyLevel++;
+ asteroidSpawnRate = Math.max(60, asteroidSpawnRate - 20);
+ }
+ // Spawn asteroids
+ if (LK.ticks % asteroidSpawnRate == 0) {
+ spawnAsteroid();
+ }
+ // Spawn platforms occasionally
+ if (LK.ticks % 300 == 0) {
+ spawnPlatform();
+ }
+ // Update bullets
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ var bullet = bullets[i];
+ // Remove bullets that go off screen
+ if (bullet.lastY >= -30 && bullet.y < -30) {
+ bullet.destroy();
+ bullets.splice(i, 1);
+ continue;
+ }
+ bullet.lastY = bullet.y;
+ }
+ // Update asteroids
+ for (var j = asteroids.length - 1; j >= 0; j--) {
+ var asteroid = asteroids[j];
+ // Remove asteroids that go off screen
+ if (asteroid.y > 2800) {
+ asteroid.destroy();
+ asteroids.splice(j, 1);
+ continue;
+ }
+ // Check collision with spaceship
+ if (asteroid.intersects(spaceship)) {
+ LK.effects.flashScreen(0xff0000, 500);
+ lives--;
+ updateLives();
+ // Remove asteroid
+ asteroid.destroy();
+ asteroids.splice(j, 1);
+ if (lives <= 0) {
+ LK.showGameOver();
+ return;
+ }
+ continue;
+ }
+ // Check collision with bullets
+ for (var k = bullets.length - 1; k >= 0; k--) {
+ var bullet = bullets[k];
+ if (bullet.intersects(asteroid)) {
+ // Destroy both asteroid and bullet
+ LK.setScore(LK.getScore() + 10);
+ updateScore();
+ LK.getSound('explosion').play();
+ asteroid.destroy();
+ asteroids.splice(j, 1);
+ bullet.destroy();
+ bullets.splice(k, 1);
+ break;
+ }
+ }
+ }
+ // Update platforms
+ for (var l = platforms.length - 1; l >= 0; l--) {
+ var platform = platforms[l];
+ // Remove platforms that go off screen
+ if (platform.y > 2800) {
+ platform.destroy();
+ platforms.splice(l, 1);
+ continue;
+ }
+ // Check landing on platform
+ if (!platform.hasLanded && platform.intersects(spaceship)) {
+ // Successful landing
+ var landingSpeed = Math.abs(spaceship.velocityY);
+ if (landingSpeed < 4) {
+ // Gentle landing - award points
+ LK.setScore(LK.getScore() + 50);
+ updateScore();
+ LK.getSound('landing').play();
+ platform.hasLanded = true;
+ // Flash platform green
+ LK.effects.flashObject(platform, 0x00ff00, 1000);
+ } else {
+ // Hard landing - lose life
+ LK.effects.flashScreen(0xff0000, 500);
+ lives--;
+ updateLives();
+ platform.destroy();
+ platforms.splice(l, 1);
+ if (lives <= 0) {
+ LK.showGameOver();
+ return;
+ }
+ }
+ }
+ }
+ // Check win condition
+ if (LK.getScore() >= 1000) {
+ LK.showYouWin();
+ }
+};
\ No newline at end of file