User prompt
Level increases every 10 kills
User prompt
The rocket only moves left and right.
User prompt
rocket moves and fires with the left mouse button
User prompt
bu roket atesฬง etmiyor
User prompt
The rocket can move and shoot at the same time with the mouse.
User prompt
The rocket is fired with the right mouse button.
User prompt
let the rocket move as the mouse moves
User prompt
let the rocket move
Code edit (1 edits merged)
Please save this source code
User prompt
Rocket vs Alien Swarm
Initial prompt
let's have a rocket in space. Let the aliens attack this rocket. And let the rocket shoot at these aliens. for every alien who touches the rocket, let the rocket's life decrease. And for every alien hit by the rocket, let the rocket's level rise. Let the aliens disappear in 2 strokes. An increase in the level 7 of the rocket alien.
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ // Alien class var Alien = Container.expand(function () { var self = Container.call(this); var alienAsset = self.attachAsset('alien', { anchorX: 0.5, anchorY: 0.5 }); self.width = alienAsset.width; self.height = alienAsset.height; self.hp = 2; // Each alien takes 2 hits self.isHit = false; self.hitTween = null; // For hit flash self.flash = function () { if (self.hitTween) { tween.stop(alienAsset, { tint: true }); } alienAsset.tint = 0xff5252; self.hitTween = tween(alienAsset, { tint: 0x8d3abf }, { duration: 200, easing: tween.linear, onFinish: function onFinish() { self.hitTween = null; } }); }; // For movement self.speed = 8 + Math.random() * 4; // Slightly random speed self.update = function () { self.y += self.speed; }; return self; }); // Boss Alien class (for level 7) var BossAlien = Container.expand(function () { var self = Container.call(this); var bossAsset = self.attachAsset('alien', { anchorX: 0.5, anchorY: 0.5, scaleX: 1.7, scaleY: 1.7 }); self.width = bossAsset.width * 1.7; self.height = bossAsset.height * 1.7; self.hp = 6; // Boss takes 6 hits self.isHit = false; self.hitTween = null; self.flash = function () { if (self.hitTween) { tween.stop(bossAsset, { tint: true }); } bossAsset.tint = 0xff5252; self.hitTween = tween(bossAsset, { tint: 0x8d3abf }, { duration: 200, easing: tween.linear, onFinish: function onFinish() { self.hitTween = null; } }); }; self.speed = 10; // Boss is faster self.update = function () { self.y += self.speed; }; return self; }); // Bullet class var Bullet = Container.expand(function () { var self = Container.call(this); var bulletAsset = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = -32; // Upwards self.update = function () { self.y += self.speed; }; return self; }); // Rocket (player) class var Rocket = Container.expand(function () { var self = Container.call(this); var rocketAsset = self.attachAsset('rocket', { anchorX: 0.5, anchorY: 0.5 }); self.width = rocketAsset.width; self.height = rocketAsset.height; // For hit flash self.flashTween = null; // Show hit flash self.flash = function () { if (self.flashTween) { tween.stop(rocketAsset, { tint: true }); } rocketAsset.tint = 0xff5252; self.flashTween = tween(rocketAsset, { tint: 0x4fc3f7 }, { duration: 400, easing: tween.linear, onFinish: function onFinish() { self.flashTween = null; } }); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ // Life bar background // Life bar // Alien hit flash // Bullet // Alien (enemy) // Rocket (player) // Game constants var GAME_WIDTH = 2048; var GAME_HEIGHT = 2732; var ROCKET_START_LIFE = 3; var LEVEL_TO_BOSS = 7; // Game state var rocket = null; var bullets = []; var aliens = []; var level = 1; var rocketLife = ROCKET_START_LIFE; var canShoot = true; var shootCooldown = 0; var dragNode = null; var lastMoveX = 0; var lastMoveY = 0; var score = 0; var bossSpawned = false; // GUI elements var levelTxt = new Text2('Level 1', { size: 90, fill: "#fff" }); levelTxt.anchor.set(0.5, 0); LK.gui.top.addChild(levelTxt); var scoreTxt = new Text2('0', { size: 90, fill: "#fff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.topRight.addChild(scoreTxt); var lifeBarBg = LK.getAsset('lifeBarBg', { anchorX: 0.5, anchorY: 0.5, x: 0, y: 0 }); var lifeBar = LK.getAsset('lifeBar', { anchorX: 0, anchorY: 0.5, x: -200, y: 0 }); var lifeBarContainer = new Container(); lifeBarContainer.addChild(lifeBarBg); lifeBarContainer.addChild(lifeBar); lifeBarContainer.x = GAME_WIDTH / 2; lifeBarContainer.y = 180; LK.gui.top.addChild(lifeBarContainer); // Helper to update life bar function updateLifeBar() { var percent = rocketLife / ROCKET_START_LIFE; if (percent < 0) percent = 0; lifeBar.width = 400 * percent; if (percent > 0.5) { lifeBar.tint = 0x43a047; } else if (percent > 0.2) { lifeBar.tint = 0xffc107; } else { lifeBar.tint = 0xd32f2f; } } // Helper to update level text function updateLevelText() { levelTxt.setText('Level ' + level); } // Helper to update score text function updateScoreText() { scoreTxt.setText(score); } // Spawn a new alien function spawnAlien() { var alien; if (level === LEVEL_TO_BOSS && !bossSpawned) { alien = new BossAlien(); bossSpawned = true; } else { alien = new Alien(); } // Random x, but keep inside screen var margin = 120; alien.x = margin + Math.random() * (GAME_WIDTH - margin * 2); alien.y = -alien.height / 2; aliens.push(alien); game.addChild(alien); } // Reset game state function resetGame() { // Remove all bullets and aliens for (var i = 0; i < bullets.length; ++i) { bullets[i].destroy(); } for (var j = 0; j < aliens.length; ++j) { aliens[j].destroy(); } bullets = []; aliens = []; level = 1; rocketLife = ROCKET_START_LIFE; canShoot = true; shootCooldown = 0; dragNode = null; lastMoveX = 0; lastMoveY = 0; score = 0; bossSpawned = false; updateLevelText(); updateScoreText(); updateLifeBar(); } // Initialize rocket rocket = new Rocket(); rocket.x = GAME_WIDTH / 2; rocket.y = GAME_HEIGHT - 400; game.addChild(rocket); // Initial GUI updateLevelText(); updateScoreText(); updateLifeBar(); // Touch/move controls game.down = function (x, y, obj) { // Only start drag if touch is on rocket var local = rocket.toLocal(game.toGlobal({ x: x, y: y })); if (local.x > -rocket.width / 2 && local.x < rocket.width / 2 && local.y > -rocket.height / 2 && local.y < rocket.height / 2) { dragNode = rocket; lastMoveX = x; lastMoveY = y; } }; game.move = function (x, y, obj) { // Always move the rocket to the mouse/touch position, clamped to screen var newX = x; var halfW = rocket.width / 2; if (newX < halfW) newX = halfW; if (newX > GAME_WIDTH - halfW) newX = GAME_WIDTH - halfW; rocket.x = newX; // Y position stays fixed at the starting Y rocket.y = GAME_HEIGHT - 400; lastMoveX = x; lastMoveY = y; }; game.up = function (x, y, obj) { dragNode = null; }; // Tap to shoot (left mouse button or tap) game.down = function (x, y, obj) { // Only start drag if touch is on rocket var local = rocket.toLocal(game.toGlobal({ x: x, y: y })); if (local.x > -rocket.width / 2 && local.x < rocket.width / 2 && local.y > -rocket.height / 2 && local.y < rocket.height / 2) { dragNode = rocket; lastMoveX = x; lastMoveY = y; } // Always allow shooting with left mouse button/tap, regardless of drag state if (canShoot) { shootBullet(); } }; // Shoot bullet function shootBullet() { if (!canShoot) return; var bullet = new Bullet(); bullet.x = rocket.x; bullet.y = rocket.y - rocket.height / 2 - 20; bullets.push(bullet); game.addChild(bullet); canShoot = false; shootCooldown = 10; // 10 ticks cooldown } // Main update loop game.update = function () { // Shooting cooldown if (!canShoot) { shootCooldown--; if (shootCooldown <= 0) { canShoot = true; } } // Update bullets for (var i = bullets.length - 1; i >= 0; --i) { var b = bullets[i]; b.update(); // Remove if off screen if (b.y < -100) { b.destroy(); bullets.splice(i, 1); continue; } } // Update aliens for (var j = aliens.length - 1; j >= 0; --j) { var a = aliens[j]; a.update(); // Remove if off screen if (a.y > GAME_HEIGHT + 200) { a.destroy(); aliens.splice(j, 1); continue; } // Check collision with rocket if (a.intersects(rocket)) { // Rocket hit! rocketLife--; updateLifeBar(); rocket.flash(); a.flash(); if (a.hp > 1) { a.hp--; // Alien stays, but flashes } else { a.destroy(); aliens.splice(j, 1); score++; updateScoreText(); // Increase level every 10 kills if (level < LEVEL_TO_BOSS && score % 10 === 0) { level++; updateLevelText(); } } // Check for game over if (rocketLife <= 0) { LK.effects.flashScreen(0xff0000, 1000); LK.showGameOver(); return; } } } // Bullet-alien collisions for (var i2 = bullets.length - 1; i2 >= 0; --i2) { var b2 = bullets[i2]; for (var j2 = aliens.length - 1; j2 >= 0; --j2) { var a2 = aliens[j2]; if (b2.intersects(a2)) { a2.hp--; a2.flash(); b2.destroy(); bullets.splice(i2, 1); if (a2.hp <= 0) { a2.destroy(); aliens.splice(j2, 1); score++; updateScoreText(); // Increase level every 10 kills if (level < LEVEL_TO_BOSS && score % 10 === 0) { level++; updateLevelText(); } // If boss defeated, win if (level === LEVEL_TO_BOSS && bossSpawned && aliens.length === 0) { LK.effects.flashScreen(0x00e676, 1000); LK.showYouWin(); return; } } break; } } } // Alien spawn logic if (level < LEVEL_TO_BOSS) { // Spawn aliens at interval, max 2 on screen if (aliens.length < 2 && LK.ticks % (60 - level * 4) === 0) { spawnAlien(); } } else if (level === LEVEL_TO_BOSS && !bossSpawned) { // Spawn boss spawnAlien(); } }; // Initial spawn spawnAlien();
===================================================================
--- original.js
+++ change.js
@@ -360,9 +360,10 @@
a.destroy();
aliens.splice(j, 1);
score++;
updateScoreText();
- if (level < LEVEL_TO_BOSS) {
+ // Increase level every 10 kills
+ if (level < LEVEL_TO_BOSS && score % 10 === 0) {
level++;
updateLevelText();
}
}
@@ -388,9 +389,10 @@
a2.destroy();
aliens.splice(j2, 1);
score++;
updateScoreText();
- if (level < LEVEL_TO_BOSS) {
+ // Increase level every 10 kills
+ if (level < LEVEL_TO_BOSS && score % 10 === 0) {
level++;
updateLevelText();
}
// If boss defeated, win