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 = 150;
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);
}
// Create single asteroid
var singleAsteroid = game.addChild(new Asteroid());
singleAsteroid.x = Math.random() * 1500 + 274; // Random position but not too close to edges
singleAsteroid.y = Math.random() * 1000 + 800; // Middle area of screen
singleAsteroid.velocityX = 0; // Make it stationary
singleAsteroid.velocityY = 0;
asteroids.push(singleAsteroid);
// Create single landing platform at bottom
var landingPlatform = game.addChild(new Platform());
landingPlatform.x = 1024;
landingPlatform.y = 2600;
landingPlatform.velocityY = 0; // Make it stationary
platforms.push(landingPlatform);
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);
}
// Single asteroid and platform gameplay - no spawning needed
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// Remove bullets that go off screen (bottom)
if (bullet.lastY <= 2762 && bullet.y > 2762) {
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];
// Check collision with spaceship
if (asteroid.intersects(spaceship)) {
LK.effects.flashScreen(0xff0000, 500);
lives--;
updateLives();
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];
// 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);
// Win condition - successfully landed
LK.showYouWin();
} else {
// Hard landing - lose life
LK.effects.flashScreen(0xff0000, 500);
lives--;
updateLives();
if (lives <= 0) {
LK.showGameOver();
return;
}
}
}
}
// Check win condition
if (LK.getScore() >= 1000) {
LK.showYouWin();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -27,9 +27,9 @@
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
- self.speed = -12;
+ self.speed = 12;
self.update = function () {
self.y += self.speed;
};
return self;
@@ -84,9 +84,9 @@
* Game Code
****/
var spaceship = game.addChild(new Spaceship());
spaceship.x = 1024;
-spaceship.y = 200;
+spaceship.y = 150;
var asteroids = [];
var bullets = [];
var platforms = [];
var dragNode = null;
@@ -129,9 +129,9 @@
platforms.push(landingPlatform);
function fireBullet() {
var bullet = new Bullet();
bullet.x = spaceship.x;
- bullet.y = spaceship.y - 60;
+ bullet.y = spaceship.y + 60;
bullet.lastY = bullet.y;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
@@ -173,10 +173,10 @@
// Single asteroid and platform gameplay - no spawning needed
// 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) {
+ // Remove bullets that go off screen (bottom)
+ if (bullet.lastY <= 2762 && bullet.y > 2762) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}