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.speed = 1.5 + difficultyLevel * 0.5; // Speed multiplier increases with difficulty level
self.update = function () {
self.x += self.velocityX * self.speed;
self.y += self.velocityY * self.speed;
self.rotation += self.rotationSpeed;
// Bounce off screen edges with angle reflection
if (self.x <= 0 || self.x >= 2048) {
self.velocityX = -self.velocityX; // Reverse horizontal velocity
if (self.x <= 0) self.x = 0;
if (self.x >= 2048) self.x = 2048;
}
if (self.y <= 0 || self.y >= 2732) {
self.velocityY = -self.velocityY; // Reverse vertical velocity
if (self.y <= 0) self.y = 0;
if (self.y >= 2732) self.y = 2732;
}
};
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 = 30 + difficultyLevel * 6; // 1.5x increased base speed + higher level multiplier
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);
// Level display
var levelTxt = new Text2('Level: 1', {
size: 60,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(levelTxt);
function updateScore() {
scoreTxt.setText('Score: ' + LK.getScore());
}
function updateLevel() {
levelTxt.setText('Level: ' + difficultyLevel);
}
// Create single asteroid
var singleAsteroid = game.addChild(new Asteroid());
// Always spawn from sides (left or right)
if (Math.random() < 0.5) {
// Spawn from left side
singleAsteroid.x = 0;
singleAsteroid.velocityX = Math.random() * 3 + 2; // Move right
} else {
// Spawn from right side
singleAsteroid.x = 2048;
singleAsteroid.velocityX = -(Math.random() * 3 + 2); // Move left
}
singleAsteroid.y = Math.random() * 1000 + 800; // Middle area of screen
singleAsteroid.velocityY = (Math.random() - 0.5) * 3; // Random vertical movement
singleAsteroid.speed = 1.5; // Initial speed multiplier
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.speed = 30 + difficultyLevel * 6; // Update speed based on current level with 1.5x increased values
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);
// Increase existing asteroid speed
if (singleAsteroid) {
singleAsteroid.speed = 1.5 + difficultyLevel * 0.5;
}
}
// 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);
LK.showGameOver();
return;
}
// 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 - award points and level up
LK.setScore(LK.getScore() + 50);
updateScore();
LK.getSound('landing').play();
platform.hasLanded = true;
// Flash platform green
LK.effects.flashObject(platform, 0x00ff00, 1000);
// Level up
difficultyLevel++;
updateLevel();
// Reset spaceship to top
spaceship.x = 1024;
spaceship.y = 150;
spaceship.velocityX = 0;
spaceship.velocityY = 0;
// Reset platform
platform.hasLanded = false;
// Create new asteroid with increased difficulty
if (singleAsteroid) {
singleAsteroid.destroy();
asteroids.splice(0, 1);
}
singleAsteroid = game.addChild(new Asteroid());
// Always spawn from sides (left or right)
if (Math.random() < 0.5) {
// Spawn from left side
singleAsteroid.x = 0;
singleAsteroid.velocityX = Math.random() * (3 + difficultyLevel) + 2; // Move right
} else {
// Spawn from right side
singleAsteroid.x = 2048;
singleAsteroid.velocityX = -(Math.random() * (3 + difficultyLevel) + 2); // Move left
}
singleAsteroid.y = Math.random() * 1000 + 800;
singleAsteroid.velocityY = (Math.random() - 0.5) * (3 + difficultyLevel);
singleAsteroid.speed = 1.5 + difficultyLevel * 0.5; // Increase speed with level - higher multiplier
asteroids.push(singleAsteroid);
}
}
// Check win condition
if (LK.getScore() >= 1000) {
LK.showYouWin();
}
}; /****
* 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.speed = 1.5 + difficultyLevel * 0.5; // Speed multiplier increases with difficulty level
self.update = function () {
self.x += self.velocityX * self.speed;
self.y += self.velocityY * self.speed;
self.rotation += self.rotationSpeed;
// Bounce off screen edges with angle reflection
if (self.x <= 0 || self.x >= 2048) {
self.velocityX = -self.velocityX; // Reverse horizontal velocity
if (self.x <= 0) self.x = 0;
if (self.x >= 2048) self.x = 2048;
}
if (self.y <= 0 || self.y >= 2732) {
self.velocityY = -self.velocityY; // Reverse vertical velocity
if (self.y <= 0) self.y = 0;
if (self.y >= 2732) self.y = 2732;
}
};
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 = 30 + difficultyLevel * 6; // 1.5x increased base speed + higher level multiplier
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);
// Level display
var levelTxt = new Text2('Level: 1', {
size: 60,
fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(levelTxt);
function updateScore() {
scoreTxt.setText('Score: ' + LK.getScore());
}
function updateLevel() {
levelTxt.setText('Level: ' + difficultyLevel);
}
// Create single asteroid
var singleAsteroid = game.addChild(new Asteroid());
// Always spawn from sides (left or right)
if (Math.random() < 0.5) {
// Spawn from left side
singleAsteroid.x = 0;
singleAsteroid.velocityX = Math.random() * 3 + 2; // Move right
} else {
// Spawn from right side
singleAsteroid.x = 2048;
singleAsteroid.velocityX = -(Math.random() * 3 + 2); // Move left
}
singleAsteroid.y = Math.random() * 1000 + 800; // Middle area of screen
singleAsteroid.velocityY = (Math.random() - 0.5) * 3; // Random vertical movement
singleAsteroid.speed = 1.5; // Initial speed multiplier
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.speed = 30 + difficultyLevel * 6; // Update speed based on current level with 1.5x increased values
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);
// Increase existing asteroid speed
if (singleAsteroid) {
singleAsteroid.speed = 1.5 + difficultyLevel * 0.5;
}
}
// 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);
LK.showGameOver();
return;
}
// 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 - award points and level up
LK.setScore(LK.getScore() + 50);
updateScore();
LK.getSound('landing').play();
platform.hasLanded = true;
// Flash platform green
LK.effects.flashObject(platform, 0x00ff00, 1000);
// Level up
difficultyLevel++;
updateLevel();
// Reset spaceship to top
spaceship.x = 1024;
spaceship.y = 150;
spaceship.velocityX = 0;
spaceship.velocityY = 0;
// Reset platform
platform.hasLanded = false;
// Create new asteroid with increased difficulty
if (singleAsteroid) {
singleAsteroid.destroy();
asteroids.splice(0, 1);
}
singleAsteroid = game.addChild(new Asteroid());
// Always spawn from sides (left or right)
if (Math.random() < 0.5) {
// Spawn from left side
singleAsteroid.x = 0;
singleAsteroid.velocityX = Math.random() * (3 + difficultyLevel) + 2; // Move right
} else {
// Spawn from right side
singleAsteroid.x = 2048;
singleAsteroid.velocityX = -(Math.random() * (3 + difficultyLevel) + 2); // Move left
}
singleAsteroid.y = Math.random() * 1000 + 800;
singleAsteroid.velocityY = (Math.random() - 0.5) * (3 + difficultyLevel);
singleAsteroid.speed = 1.5 + difficultyLevel * 0.5; // Increase speed with level - higher multiplier
asteroids.push(singleAsteroid);
}
}
// Check win condition
if (LK.getScore() >= 1000) {
LK.showYouWin();
}
};