User prompt
Show turret firing radius
User prompt
The turret must fire within a radius
Code edit (21 edits merged)
Please save this source code
User prompt
turn the bullet towards the target it is moving towards
Code edit (1 edits merged)
Please save this source code
User prompt
путь по которому идут враги замостить изображнием road
User prompt
add 1 path for all enemies from bottom to top
User prompt
Show background
User prompt
add an image of a green meadow to the background
Initial prompt
Fantasy tower defence
/**** * Classes ****/ // Bullet class for projectiles fired by towers var Bullet = Container.expand(function (startX, startY, target) { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 10; self.target = target; self.x = startX; self.y = startY; self.update = function () { var dx = self.target.x - self.x; var dy = self.target.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < 10) { self.target.hit(); self.destroy(); } else { self.x += dx / distance * self.speed; self.y += dy / distance * self.speed; // Calculate the angle to the target var angle = Math.atan2(dy, dx); // Rotate the bullet towards the target bulletGraphics.rotation = angle + 90; } }; }); // Enemy class for the creatures attacking the realm var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.health = 100; self.path = [{ x: 2010, y: 2600 }, { x: 1000, y: 2600 }, { x: 1000, y: 100 }, { x: 0, y: 100 }]; self.pathIndex = 0; self.update = function () { var target = self.path[self.pathIndex]; var dx = target.x - self.x; var dy = target.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < self.speed) { self.x = target.x; self.y = target.y; self.pathIndex++; if (self.pathIndex >= self.path.length) { // Enemy reached the end, game over logic LK.showGameOver(); } } else { self.x += dx / distance * self.speed; self.y += dy / distance * self.speed; } }; self.hit = function () { self.health -= 50; if (self.health <= 0) { self.destroy(); enemies.splice(enemies.indexOf(self), 1); } }; }); //<Assets used in the game will automatically appear here> //<Write imports for supported plugins here> // Tower class to represent defense towers var Tower = Container.expand(function () { var self = Container.call(this); var towerGraphics = self.attachAsset('tower', { anchorX: 0.5, anchorY: 0.5 }); self.range = 300; self.fireRate = 60; // Fire every 60 frames self.fireCooldown = 0; // Create a circle to represent the firing range self.rangeCircle = LK.init.shape('rangeCircle', { width: self.range * 2, height: self.range * 2, color: 0x0000ff, shape: 'ellipse' }); self.rangeCircle.alpha = 0.5; self.addChild(self.rangeCircle); self.update = function () { if (self.fireCooldown > 0) { self.fireCooldown--; } }; self.canFire = function () { return self.fireCooldown === 0; }; self.fire = function (target) { if (self.canFire()) { var bullet = new Bullet(self.x, self.y, target); game.addChild(bullet); bullets.push(bullet); self.fireCooldown = self.fireRate; } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ // Initialize arrays and variables var towers = []; var bullets = []; var enemies = []; // Create and position towers function createTowers() { var tower1 = new Tower(); tower1.x = 500; tower1.y = 1366; game.addChild(tower1); towers.push(tower1); var tower2 = new Tower(); tower2.x = 1500; tower2.y = 1366; game.addChild(tower2); towers.push(tower2); } // Spawn enemies at intervals function spawnEnemy() { var enemy = new Enemy(); enemy.x = 2010; enemy.y = 0; game.addChild(enemy); enemies.push(enemy); } // Create a road for the enemies to follow function createRoad(x, y) { if (Array.isArray(x)) { for (var i = x[0]; i < x[1]; i += 100) { var roadBlock = LK.getAsset('road', { anchorX: 0.5, anchorY: 0.5, x: i, y: y }); game.addChild(roadBlock); } } else { for (var i = y[0]; i < y[1]; i += 100) { var roadBlock = LK.getAsset('road', { anchorX: 0.5, anchorY: 0.5, x: x, y: i }); game.addChild(roadBlock); } } } createRoad(2000, [0, 2601]); createRoad([1000, 2000], 2601); createRoad(1000, [100, 2601]); createRoad([0, 1000], 100); // Game update loop game.update = function () { // Update towers towers.forEach(function (tower) { tower.update(); enemies.forEach(function (enemy) { var dx = tower.x - enemy.x; var dy = tower.y - enemy.y; var distance = Math.sqrt(dx * dx + dy * dy); if (tower.canFire() && distance < tower.range) { tower.fire(enemy); } }); }); // Update bullets bullets.forEach(function (bullet) { bullet.update(); }); // Update enemies enemies.forEach(function (enemy) { enemy.update(); }); // Spawn enemies every 120 frames if (LK.ticks % 120 === 0) { spawnEnemy(); } }; // Initialize game elements createTowers();
===================================================================
--- original.js
+++ change.js
@@ -89,8 +89,17 @@
});
self.range = 300;
self.fireRate = 60; // Fire every 60 frames
self.fireCooldown = 0;
+ // Create a circle to represent the firing range
+ self.rangeCircle = LK.init.shape('rangeCircle', {
+ width: self.range * 2,
+ height: self.range * 2,
+ color: 0x0000ff,
+ shape: 'ellipse'
+ });
+ self.rangeCircle.alpha = 0.5;
+ self.addChild(self.rangeCircle);
self.update = function () {
if (self.fireCooldown > 0) {
self.fireCooldown--;
}
No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat. Goblin. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat. Tower with ballista. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
One bow arrow facing upwards. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
texture of the road made of large stones like tiles for a game. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Top view. Texture for 2D games. Green meadow.