User prompt
now use a pixelated spaceship sprite for the spaceship next
User prompt
there should be no cooldown to how fast u can click
User prompt
try using a pixely asteroid sprite instead of these ellipses
User prompt
the laser instantly destroys asteroids it touches, and i changed my mind, cooldown is 10 seconds
Code edit (1 edits merged)
Please save this source code
User prompt
Asteroid Blaster
Initial prompt
there is a spaceship in the bottom of the screen that follows the cursors x position, when you tap, the spaceship shoots a bullet, asteroids are falling endlessly, when you successfully hit an asteroid, you get one score, if the asteroid is not min size, it gets 1 size smaller, you can swipe up to shoot a laser, but it has a 20 second cooldown
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Asteroid = Container.expand(function (size) { var self = Container.call(this); self.size = size || 'large'; // large, medium, small // Define size properties if (self.size === 'large') { self.health = 3; self.speed = 2; self.points = 3; self.assetId = 'largeAsteroid'; } else if (self.size === 'medium') { self.health = 2; self.speed = 3; self.points = 2; self.assetId = 'mediumAsteroid'; } else { self.health = 1; self.speed = 4; self.points = 1; self.assetId = 'smallAsteroid'; } // Create and attach asteroid graphic self.graphic = self.attachAsset(self.assetId, { anchorX: 0.5, anchorY: 0.5 }); // Add rotation property self.rotationSpeed = (Math.random() - 0.5) * 0.05; // Add hit method self.hit = function () { self.health--; // Flash the asteroid red LK.effects.flashObject(self, 0xff0000, 300); // If health is depleted, destroy or split if (self.health <= 0) { // Add score LK.setScore(LK.getScore() + self.points); // Split asteroid if it's not small if (self.size !== 'small') { // Play asteroid break sound LK.getSound('asteroidBreak').play(); // Create smaller asteroids var newSize = self.size === 'large' ? 'medium' : 'small'; var count = self.size === 'large' ? 2 : 1; for (var i = 0; i < count; i++) { var newAsteroid = new Asteroid(newSize); newAsteroid.x = self.x + (Math.random() * 40 - 20); newAsteroid.y = self.y + (Math.random() * 40 - 20); asteroids.push(newAsteroid); game.addChild(newAsteroid); } } else { // Play explosion sound LK.getSound('explosion').play(); } // Remove this asteroid return true; } return false; }; // Update method called on each tick self.update = function () { // Move asteroid down self.y += self.speed; // Rotate asteroid self.rotation += self.rotationSpeed; // If asteroid is off-screen, remove it if (self.y > 2832) { return true; // Return true to indicate asteroid should be removed } return false; }; return self; }); var Bullet = Container.expand(function () { var self = Container.call(this); // Create and attach bullet graphic var bulletGraphic = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); // Set bullet speed self.speed = -15; // Update method called on each tick self.update = function () { self.y += self.speed; // If bullet is off-screen, remove it if (self.y < -50) { return true; // Return true to indicate bullet should be removed } return false; }; return self; }); var Ship = Container.expand(function () { var self = Container.call(this); // Create and attach ship graphic var shipGraphic = self.attachAsset('ship', { anchorX: 0.5, anchorY: 0.5 }); // Set ship properties self.canShoot = true; self.canFireLaser = true; self.laserCooldown = 0; // Shoot method self.shoot = function () { if (!self.canShoot) return; var bullet = new Bullet(); bullet.x = self.x; bullet.y = self.y - 50; bullets.push(bullet); game.addChild(bullet); // Play shoot sound LK.getSound('shoot').play(); // Set cooldown for shooting self.canShoot = false; LK.setTimeout(function () { self.canShoot = true; }, 200); }; // Fire laser method self.fireLaser = function () { if (!self.canFireLaser) return; // Create laser beam var laser = LK.getAsset('laser', { anchorX: 0.5, anchorY: 1.0 }); laser.x = self.x; laser.y = self.y - 50; game.addChild(laser); // Play laser sound LK.getSound('laserBeam').play(); // Flash the laser tween(laser, { alpha: 0 }, { duration: 1000, onFinish: function onFinish() { laser.destroy(); } }); // Destroy all asteroids in the laser's path for (var i = asteroids.length - 1; i >= 0; i--) { var asteroid = asteroids[i]; if (Math.abs(asteroid.x - self.x) < 25) { // Destroy the asteroid and get points LK.setScore(LK.getScore() + asteroid.points); asteroids.splice(i, 1); asteroid.destroy(); // Play explosion sound LK.getSound('explosion').play(); } } // Set cooldown for laser self.canFireLaser = false; self.laserCooldown = 20000; // 20 seconds // Update cooldown bar updateCooldownBar(); }; // Update method called on each tick self.update = function () { // Update laser cooldown if (!self.canFireLaser) { self.laserCooldown -= 16.67; // Approx time per tick at 60 FPS if (self.laserCooldown <= 0) { self.canFireLaser = true; updateCooldownBar(); } else { // Update cooldown bar periodically if (LK.ticks % 10 === 0) { updateCooldownBar(); } } } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Create ship var ship = new Ship(); ship.x = 2048 / 2; ship.y = 2732 - 150; game.addChild(ship); // Create game containers var bullets = []; var asteroids = []; var lastAsteroidSpawn = 0; var spawnInterval = 1500; // Start with 1.5 seconds between asteroid spawns var gameStartTime = Date.now(); var swipeStartY = null; // Create score display var scoreTxt = new Text2('0', { size: 80, fill: 0xFFFFFF }); scoreTxt.anchor.set(1.0, 0); scoreTxt.x = -20; // Offset from right edge scoreTxt.y = 20; // Offset from top LK.gui.topRight.addChild(scoreTxt); // Create cooldown bar var cooldownBarBackground = LK.getAsset('cooldownBar', { anchorX: 0, anchorY: 0, tint: 0x333333 }); cooldownBarBackground.x = 20; cooldownBarBackground.y = 20; LK.gui.topLeft.addChild(cooldownBarBackground); var cooldownBarFill = LK.getAsset('cooldownBar', { anchorX: 0, anchorY: 0 }); cooldownBarFill.x = 20; cooldownBarFill.y = 20; LK.gui.topLeft.addChild(cooldownBarFill); // Create cooldown text var cooldownText = new Text2('LASER READY', { size: 30, fill: 0xFFFFFF }); cooldownText.anchor.set(0.5, 0.5); cooldownText.x = cooldownBarBackground.x + 150; cooldownText.y = cooldownBarBackground.y + 15; LK.gui.topLeft.addChild(cooldownText); // Function to update cooldown bar function updateCooldownBar() { if (ship.canFireLaser) { cooldownBarFill.width = 300; cooldownText.setText('LASER READY'); } else { var percentage = ship.laserCooldown / 20000; cooldownBarFill.width = 300 * (1 - percentage); cooldownText.setText('COOLDOWN: ' + Math.ceil(ship.laserCooldown / 1000) + 's'); } } // Function to spawn asteroid function spawnAsteroid() { var asteroid = new Asteroid(); asteroid.x = Math.random() * 2048; asteroid.y = -100; asteroids.push(asteroid); game.addChild(asteroid); lastAsteroidSpawn = LK.ticks; } // Mouse/touch down handler game.down = function (x, y, obj) { // Store swipe start position swipeStartY = y; // Normal click shoots ship.shoot(); }; // Mouse/touch up handler game.up = function (x, y, obj) { // Check for swipe up (must be minimum 200px) if (swipeStartY !== null && swipeStartY - y > 200) { ship.fireLaser(); } swipeStartY = null; }; // Mouse/touch move handler game.move = function (x, y, obj) { // Move ship to follow cursor X-position ship.x = x; }; // Game update function (called each tick) game.update = function () { // Update score display scoreTxt.setText(LK.getScore()); // Increase difficulty over time var gameTimeSeconds = (Date.now() - gameStartTime) / 1000; spawnInterval = Math.max(300, 1500 - gameTimeSeconds * 5); // Decrease spawn interval over time, minimum 300ms // Spawn asteroids if (LK.ticks - lastAsteroidSpawn > spawnInterval / 16.67) { // Convert ms to ticks (60 FPS) spawnAsteroid(); } // Update ship ship.update(); // Update bullets for (var i = bullets.length - 1; i >= 0; i--) { var bullet = bullets[i]; // Update bullet position and check if it should be removed if (bullet.update()) { bullets.splice(i, 1); bullet.destroy(); continue; } // Check for collisions with asteroids for (var j = asteroids.length - 1; j >= 0; j--) { var asteroid = asteroids[j]; if (bullet.intersects(asteroid)) { // Bullet hit asteroid if (asteroid.hit()) { asteroids.splice(j, 1); asteroid.destroy(); } // Remove bullet bullets.splice(i, 1); bullet.destroy(); break; } } } // Update asteroids for (var k = asteroids.length - 1; k >= 0; k--) { var asteroid = asteroids[k]; // Update asteroid position and check if it should be removed if (asteroid.update()) { asteroids.splice(k, 1); asteroid.destroy(); continue; } // Check for collision with ship if (ship.intersects(asteroid)) { // Game over LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } } }; // Start background music LK.playMusic('backgroundMusic', { fade: { start: 0, end: 0.3, duration: 1000 } });
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,349 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var Asteroid = Container.expand(function (size) {
+ var self = Container.call(this);
+ self.size = size || 'large'; // large, medium, small
+ // Define size properties
+ if (self.size === 'large') {
+ self.health = 3;
+ self.speed = 2;
+ self.points = 3;
+ self.assetId = 'largeAsteroid';
+ } else if (self.size === 'medium') {
+ self.health = 2;
+ self.speed = 3;
+ self.points = 2;
+ self.assetId = 'mediumAsteroid';
+ } else {
+ self.health = 1;
+ self.speed = 4;
+ self.points = 1;
+ self.assetId = 'smallAsteroid';
+ }
+ // Create and attach asteroid graphic
+ self.graphic = self.attachAsset(self.assetId, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Add rotation property
+ self.rotationSpeed = (Math.random() - 0.5) * 0.05;
+ // Add hit method
+ self.hit = function () {
+ self.health--;
+ // Flash the asteroid red
+ LK.effects.flashObject(self, 0xff0000, 300);
+ // If health is depleted, destroy or split
+ if (self.health <= 0) {
+ // Add score
+ LK.setScore(LK.getScore() + self.points);
+ // Split asteroid if it's not small
+ if (self.size !== 'small') {
+ // Play asteroid break sound
+ LK.getSound('asteroidBreak').play();
+ // Create smaller asteroids
+ var newSize = self.size === 'large' ? 'medium' : 'small';
+ var count = self.size === 'large' ? 2 : 1;
+ for (var i = 0; i < count; i++) {
+ var newAsteroid = new Asteroid(newSize);
+ newAsteroid.x = self.x + (Math.random() * 40 - 20);
+ newAsteroid.y = self.y + (Math.random() * 40 - 20);
+ asteroids.push(newAsteroid);
+ game.addChild(newAsteroid);
+ }
+ } else {
+ // Play explosion sound
+ LK.getSound('explosion').play();
+ }
+ // Remove this asteroid
+ return true;
+ }
+ return false;
+ };
+ // Update method called on each tick
+ self.update = function () {
+ // Move asteroid down
+ self.y += self.speed;
+ // Rotate asteroid
+ self.rotation += self.rotationSpeed;
+ // If asteroid is off-screen, remove it
+ if (self.y > 2832) {
+ return true; // Return true to indicate asteroid should be removed
+ }
+ return false;
+ };
+ return self;
+});
+var Bullet = Container.expand(function () {
+ var self = Container.call(this);
+ // Create and attach bullet graphic
+ var bulletGraphic = self.attachAsset('bullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Set bullet speed
+ self.speed = -15;
+ // Update method called on each tick
+ self.update = function () {
+ self.y += self.speed;
+ // If bullet is off-screen, remove it
+ if (self.y < -50) {
+ return true; // Return true to indicate bullet should be removed
+ }
+ return false;
+ };
+ return self;
+});
+var Ship = Container.expand(function () {
+ var self = Container.call(this);
+ // Create and attach ship graphic
+ var shipGraphic = self.attachAsset('ship', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Set ship properties
+ self.canShoot = true;
+ self.canFireLaser = true;
+ self.laserCooldown = 0;
+ // Shoot method
+ self.shoot = function () {
+ if (!self.canShoot) return;
+ var bullet = new Bullet();
+ bullet.x = self.x;
+ bullet.y = self.y - 50;
+ bullets.push(bullet);
+ game.addChild(bullet);
+ // Play shoot sound
+ LK.getSound('shoot').play();
+ // Set cooldown for shooting
+ self.canShoot = false;
+ LK.setTimeout(function () {
+ self.canShoot = true;
+ }, 200);
+ };
+ // Fire laser method
+ self.fireLaser = function () {
+ if (!self.canFireLaser) return;
+ // Create laser beam
+ var laser = LK.getAsset('laser', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ laser.x = self.x;
+ laser.y = self.y - 50;
+ game.addChild(laser);
+ // Play laser sound
+ LK.getSound('laserBeam').play();
+ // Flash the laser
+ tween(laser, {
+ alpha: 0
+ }, {
+ duration: 1000,
+ onFinish: function onFinish() {
+ laser.destroy();
+ }
+ });
+ // Destroy all asteroids in the laser's path
+ for (var i = asteroids.length - 1; i >= 0; i--) {
+ var asteroid = asteroids[i];
+ if (Math.abs(asteroid.x - self.x) < 25) {
+ // Destroy the asteroid and get points
+ LK.setScore(LK.getScore() + asteroid.points);
+ asteroids.splice(i, 1);
+ asteroid.destroy();
+ // Play explosion sound
+ LK.getSound('explosion').play();
+ }
+ }
+ // Set cooldown for laser
+ self.canFireLaser = false;
+ self.laserCooldown = 20000; // 20 seconds
+ // Update cooldown bar
+ updateCooldownBar();
+ };
+ // Update method called on each tick
+ self.update = function () {
+ // Update laser cooldown
+ if (!self.canFireLaser) {
+ self.laserCooldown -= 16.67; // Approx time per tick at 60 FPS
+ if (self.laserCooldown <= 0) {
+ self.canFireLaser = true;
+ updateCooldownBar();
+ } else {
+ // Update cooldown bar periodically
+ if (LK.ticks % 10 === 0) {
+ updateCooldownBar();
+ }
+ }
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x000000
+});
+
+/****
+* Game Code
+****/
+// Create ship
+var ship = new Ship();
+ship.x = 2048 / 2;
+ship.y = 2732 - 150;
+game.addChild(ship);
+// Create game containers
+var bullets = [];
+var asteroids = [];
+var lastAsteroidSpawn = 0;
+var spawnInterval = 1500; // Start with 1.5 seconds between asteroid spawns
+var gameStartTime = Date.now();
+var swipeStartY = null;
+// Create score display
+var scoreTxt = new Text2('0', {
+ size: 80,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(1.0, 0);
+scoreTxt.x = -20; // Offset from right edge
+scoreTxt.y = 20; // Offset from top
+LK.gui.topRight.addChild(scoreTxt);
+// Create cooldown bar
+var cooldownBarBackground = LK.getAsset('cooldownBar', {
+ anchorX: 0,
+ anchorY: 0,
+ tint: 0x333333
+});
+cooldownBarBackground.x = 20;
+cooldownBarBackground.y = 20;
+LK.gui.topLeft.addChild(cooldownBarBackground);
+var cooldownBarFill = LK.getAsset('cooldownBar', {
+ anchorX: 0,
+ anchorY: 0
+});
+cooldownBarFill.x = 20;
+cooldownBarFill.y = 20;
+LK.gui.topLeft.addChild(cooldownBarFill);
+// Create cooldown text
+var cooldownText = new Text2('LASER READY', {
+ size: 30,
+ fill: 0xFFFFFF
+});
+cooldownText.anchor.set(0.5, 0.5);
+cooldownText.x = cooldownBarBackground.x + 150;
+cooldownText.y = cooldownBarBackground.y + 15;
+LK.gui.topLeft.addChild(cooldownText);
+// Function to update cooldown bar
+function updateCooldownBar() {
+ if (ship.canFireLaser) {
+ cooldownBarFill.width = 300;
+ cooldownText.setText('LASER READY');
+ } else {
+ var percentage = ship.laserCooldown / 20000;
+ cooldownBarFill.width = 300 * (1 - percentage);
+ cooldownText.setText('COOLDOWN: ' + Math.ceil(ship.laserCooldown / 1000) + 's');
+ }
+}
+// Function to spawn asteroid
+function spawnAsteroid() {
+ var asteroid = new Asteroid();
+ asteroid.x = Math.random() * 2048;
+ asteroid.y = -100;
+ asteroids.push(asteroid);
+ game.addChild(asteroid);
+ lastAsteroidSpawn = LK.ticks;
+}
+// Mouse/touch down handler
+game.down = function (x, y, obj) {
+ // Store swipe start position
+ swipeStartY = y;
+ // Normal click shoots
+ ship.shoot();
+};
+// Mouse/touch up handler
+game.up = function (x, y, obj) {
+ // Check for swipe up (must be minimum 200px)
+ if (swipeStartY !== null && swipeStartY - y > 200) {
+ ship.fireLaser();
+ }
+ swipeStartY = null;
+};
+// Mouse/touch move handler
+game.move = function (x, y, obj) {
+ // Move ship to follow cursor X-position
+ ship.x = x;
+};
+// Game update function (called each tick)
+game.update = function () {
+ // Update score display
+ scoreTxt.setText(LK.getScore());
+ // Increase difficulty over time
+ var gameTimeSeconds = (Date.now() - gameStartTime) / 1000;
+ spawnInterval = Math.max(300, 1500 - gameTimeSeconds * 5); // Decrease spawn interval over time, minimum 300ms
+ // Spawn asteroids
+ if (LK.ticks - lastAsteroidSpawn > spawnInterval / 16.67) {
+ // Convert ms to ticks (60 FPS)
+ spawnAsteroid();
+ }
+ // Update ship
+ ship.update();
+ // Update bullets
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ var bullet = bullets[i];
+ // Update bullet position and check if it should be removed
+ if (bullet.update()) {
+ bullets.splice(i, 1);
+ bullet.destroy();
+ continue;
+ }
+ // Check for collisions with asteroids
+ for (var j = asteroids.length - 1; j >= 0; j--) {
+ var asteroid = asteroids[j];
+ if (bullet.intersects(asteroid)) {
+ // Bullet hit asteroid
+ if (asteroid.hit()) {
+ asteroids.splice(j, 1);
+ asteroid.destroy();
+ }
+ // Remove bullet
+ bullets.splice(i, 1);
+ bullet.destroy();
+ break;
+ }
+ }
+ }
+ // Update asteroids
+ for (var k = asteroids.length - 1; k >= 0; k--) {
+ var asteroid = asteroids[k];
+ // Update asteroid position and check if it should be removed
+ if (asteroid.update()) {
+ asteroids.splice(k, 1);
+ asteroid.destroy();
+ continue;
+ }
+ // Check for collision with ship
+ if (ship.intersects(asteroid)) {
+ // Game over
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ return;
+ }
+ }
+};
+// Start background music
+LK.playMusic('backgroundMusic', {
+ fade: {
+ start: 0,
+ end: 0.3,
+ duration: 1000
+ }
});
\ No newline at end of file