User prompt
the start wave button to make an animation when touched. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
the life of the enemies to come out
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'to')' in or related to this line: 'tween(startWaveButton).to({' Line Number: 408 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
the start wave button should be bigger and more colourful.
User prompt
the basic turret to fire 2 bullets in a single shot
User prompt
when a single enemy reaches the end, you lose the game.
User prompt
when you lose, a game over screen will appear and the game will freeze and a button will appear to play again.
User prompt
that if an enemy arrives at the end you lose
User prompt
that in the top right-hand corner the money you have is displayed
User prompt
make you money
Code edit (1 edits merged)
Please save this source code
User prompt
Tower Defense Rush
Initial prompt
make a tower defense
/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var Bullet = Container.expand(function (damage, target) { var self = Container.call(this); var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); self.damage = damage || 10; self.target = target; self.speed = 8; self.update = function () { if (!self.target || !self.target.parent) { self.destroy(); return; } 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.takeDamage(self.damage); self.destroy(); return; } var angle = Math.atan2(dy, dx); self.x += Math.cos(angle) * self.speed; self.y += Math.sin(angle) * self.speed; }; return self; }); var Enemy = Container.expand(function (enemyType) { var self = Container.call(this); self.enemyType = enemyType || 'basic'; self.pathIndex = 0; self.health = 20; self.maxHealth = 20; self.speed = 2; self.reward = 10; self.slowed = false; self.slowDuration = 0; var enemyGraphics; if (self.enemyType === 'basic') { enemyGraphics = self.attachAsset('basicEnemy', { anchorX: 0.5, anchorY: 0.5 }); self.health = 20; self.maxHealth = 20; self.speed = 2; self.reward = 10; } else if (self.enemyType === 'fast') { enemyGraphics = self.attachAsset('fastEnemy', { anchorX: 0.5, anchorY: 0.5 }); self.health = 15; self.maxHealth = 15; self.speed = 4; self.reward = 15; } else if (self.enemyType === 'tank') { enemyGraphics = self.attachAsset('tankEnemy', { anchorX: 0.5, anchorY: 0.5 }); self.health = 60; self.maxHealth = 60; self.speed = 1; self.reward = 25; } else if (self.enemyType === 'gold') { enemyGraphics = self.attachAsset('goldEnemy', { anchorX: 0.5, anchorY: 0.5 }); self.health = 10; self.maxHealth = 10; self.speed = 3; self.reward = 100; // High reward! } // Create health bar background var healthBarBg = LK.getAsset('pathTile', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.4, scaleY: 0.1 }); healthBarBg.y = -enemyGraphics.height / 2 - 20; healthBarBg.tint = 0x000000; self.addChild(healthBarBg); // Create health bar foreground var healthBarFg = LK.getAsset('grassTile', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.4, scaleY: 0.1 }); healthBarFg.y = -enemyGraphics.height / 2 - 20; healthBarFg.tint = 0x00FF00; self.addChild(healthBarFg); self.healthBarBg = healthBarBg; self.healthBarFg = healthBarFg; self.updateHealthBar = function () { var healthPercent = self.health / self.maxHealth; self.healthBarFg.scaleX = 0.4 * healthPercent; // Change color based on health if (healthPercent > 0.6) { self.healthBarFg.tint = 0x00FF00; // Green } else if (healthPercent > 0.3) { self.healthBarFg.tint = 0xFFFF00; // Yellow } else { self.healthBarFg.tint = 0xFF0000; // Red } }; self.takeDamage = function (damage) { self.health -= damage; self.updateHealthBar(); LK.getSound('enemyHit').play(); if (self.health <= 0) { self.die(); } }; self.die = function () { // Calculate bonus multiplier based on kill streak var bonusMultiplier = 1; if (killStreak >= 10) bonusMultiplier = 2;else if (killStreak >= 5) bonusMultiplier = 1.5; coins += Math.floor(self.reward * bonusMultiplier); enemiesKilled++; killStreak++; LK.getSound('enemyDeath').play(); for (var i = 0; i < enemies.length; i++) { if (enemies[i] === self) { enemies.splice(i, 1); break; } } self.destroy(); updateUI(); }; self.update = function () { if (self.slowed) { self.slowDuration--; if (self.slowDuration <= 0) { self.slowed = false; } } if (self.pathIndex >= pathPoints.length - 1) { // Game over immediately when any enemy reaches the end LK.showGameOver(); return; } var currentSpeed = self.slowed ? self.speed * 0.5 : self.speed; var target = pathPoints[self.pathIndex + 1]; var dx = target.x - self.x; var dy = target.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < 10) { self.pathIndex++; } else { var angle = Math.atan2(dy, dx); self.x += Math.cos(angle) * currentSpeed; self.y += Math.sin(angle) * currentSpeed; } }; // Initialize health bar display self.updateHealthBar(); return self; }); var Tower = Container.expand(function (towerType) { var self = Container.call(this); self.towerType = towerType || 'basic'; self.level = 1; self.range = 120; self.damage = 10; self.fireRate = 60; self.cost = 50; self.upgradeCost = 30; self.lastShot = 0; var towerGraphics; if (self.towerType === 'basic') { towerGraphics = self.attachAsset('basicTower', { anchorX: 0.5, anchorY: 0.5 }); self.damage = 10; self.range = 120; self.fireRate = 60; self.cost = 50; } else if (self.towerType === 'splash') { towerGraphics = self.attachAsset('splashTower', { anchorX: 0.5, anchorY: 0.5 }); self.damage = 15; self.range = 100; self.fireRate = 90; self.cost = 80; } else if (self.towerType === 'slow') { towerGraphics = self.attachAsset('slowTower', { anchorX: 0.5, anchorY: 0.5 }); self.damage = 5; self.range = 140; self.fireRate = 30; self.cost = 70; } else if (self.towerType === 'sniper') { towerGraphics = self.attachAsset('sniperTower', { anchorX: 0.5, anchorY: 0.5 }); self.damage = 40; self.range = 200; self.fireRate = 120; self.cost = 120; } self.canShoot = function () { return LK.ticks - self.lastShot >= self.fireRate; }; self.findTarget = function () { var closestEnemy = null; var closestDistance = self.range; for (var i = 0; i < enemies.length; i++) { var enemy = enemies[i]; var dx = enemy.x - self.x; var dy = enemy.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance <= self.range && distance < closestDistance) { closestEnemy = enemy; closestDistance = distance; } } return closestEnemy; }; self.shoot = function (target) { if (!self.canShoot()) return; if (self.towerType === 'basic') { // Fire 2 bullets with slight spread for basic tower var bullet1 = new Bullet(self.damage, target); var bullet2 = new Bullet(self.damage, target); bullet1.x = self.x - 10; // Offset slightly left bullet1.y = self.y; bullet2.x = self.x + 10; // Offset slightly right bullet2.y = self.y; bullets.push(bullet1); bullets.push(bullet2); game.addChild(bullet1); game.addChild(bullet2); } else { // Other tower types fire single bullet var bullet = new Bullet(self.damage, target); bullet.x = self.x; bullet.y = self.y; bullets.push(bullet); game.addChild(bullet); } self.lastShot = LK.ticks; LK.getSound('shoot').play(); }; self.upgrade = function () { if (coins >= self.upgradeCost && self.level < 3) { coins -= self.upgradeCost; self.level++; self.damage = Math.floor(self.damage * 1.5); self.range += 20; self.upgradeCost = Math.floor(self.upgradeCost * 1.5); updateUI(); } }; self.update = function () { var target = self.findTarget(); if (target) { self.shoot(target); } }; self.down = function (x, y, obj) { selectedTower = self; showTowerMenu = true; updateUI(); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87ceeb }); /**** * Game Code ****/ var gridSize = 100; var gridWidth = 20; var gridHeight = 27; var pathPoints = [{ x: 0, y: 400 }, { x: 300, y: 400 }, { x: 300, y: 800 }, { x: 700, y: 800 }, { x: 700, y: 1200 }, { x: 1100, y: 1200 }, { x: 1100, y: 1600 }, { x: 1500, y: 1600 }, { x: 1500, y: 2000 }, { x: 2048, y: 2000 }]; var gameGrid = []; var towers = []; var enemies = []; var bullets = []; var coins = 100; var lives = 20; var wave = 1; var enemiesKilled = 0; var killStreak = 0; var lastLives = 20; var enemiesSpawned = 0; var enemiesPerWave = 10; var spawnTimer = 0; var waveInProgress = false; var selectedTower = null; var showTowerMenu = false; var towerMenuButtons = []; // Create grid for (var y = 0; y < gridHeight; y++) { gameGrid[y] = []; for (var x = 0; x < gridWidth; x++) { gameGrid[y][x] = { x: x * gridSize + 50, y: y * gridSize + 50, occupied: false, isPath: false }; } } // Mark path tiles for (var i = 0; i < pathPoints.length - 1; i++) { var start = pathPoints[i]; var end = pathPoints[i + 1]; var steps = Math.max(Math.abs(end.x - start.x), Math.abs(end.y - start.y)) / gridSize; for (var step = 0; step <= steps; step++) { var x = Math.floor((start.x + (end.x - start.x) * step / steps) / gridSize); var y = Math.floor((start.y + (end.y - start.y) * step / steps) / gridSize); if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight) { gameGrid[y][x].isPath = true; } } } // Create visual grid for (var y = 0; y < gridHeight; y++) { for (var x = 0; x < gridWidth; x++) { var tile; if (gameGrid[y][x].isPath) { tile = LK.getAsset('pathTile', { anchorX: 0.5, anchorY: 0.5 }); } else { tile = LK.getAsset('grassTile', { anchorX: 0.5, anchorY: 0.5 }); } tile.x = gameGrid[y][x].x; tile.y = gameGrid[y][x].y; tile.alpha = 0.3; game.addChild(tile); } } // UI Elements var coinsText = new Text2('Coins: ' + coins, { size: 60, fill: 0xFFD700 }); coinsText.anchor.set(1, 0); LK.gui.topRight.addChild(coinsText); var livesText = new Text2('Lives: ' + lives, { size: 50, fill: 0xFFFFFF }); livesText.anchor.set(0, 0); LK.gui.topRight.addChild(livesText); var waveText = new Text2('Wave: ' + wave, { size: 50, fill: 0xFFFFFF }); waveText.anchor.set(0.5, 0); LK.gui.top.addChild(waveText); var startWaveButton = new Text2('🚀 START WAVE 🚀', { size: 80, fill: 0x00FF00 }); startWaveButton.anchor.set(0.5, 1); LK.gui.bottom.addChild(startWaveButton); // Add colorful tween animation to make it more eye-catching function animateStartButton() { tween(startWaveButton, { scaleX: 1.2, scaleY: 1.2 }, { duration: 500, onFinish: function onFinish() { tween(startWaveButton, { scaleX: 1.0, scaleY: 1.0 }, { duration: 500, onFinish: animateStartButton }); } }); } animateStartButton(); // Tower selection buttons var basicTowerButton = new Text2('Basic ($50)', { size: 40, fill: 0xFFFFFF }); basicTowerButton.anchor.set(0, 1); LK.gui.bottomLeft.addChild(basicTowerButton); var splashTowerButton = new Text2('Splash ($80)', { size: 40, fill: 0xFFFFFF }); splashTowerButton.anchor.set(0, 1); LK.gui.bottomLeft.addChild(splashTowerButton); var slowTowerButton = new Text2('Slow ($70)', { size: 40, fill: 0xFFFFFF }); slowTowerButton.anchor.set(0, 1); LK.gui.bottomLeft.addChild(slowTowerButton); var sniperTowerButton = new Text2('Sniper ($120)', { size: 40, fill: 0xFFFFFF }); sniperTowerButton.anchor.set(0, 1); LK.gui.bottomLeft.addChild(sniperTowerButton); // Position tower buttons basicTowerButton.y = -200; splashTowerButton.y = -150; slowTowerButton.y = -100; sniperTowerButton.y = -50; coinsText.y = 50; livesText.y = 100; var selectedTowerType = 'basic'; function updateUI() { var coinDisplay = 'Coins: ' + coins; if (killStreak >= 5) { var multiplier = killStreak >= 10 ? '2x' : '1.5x'; coinDisplay += ' (Streak: ' + killStreak + ' - ' + multiplier + ' bonus!)'; } coinsText.setText(coinDisplay); livesText.setText('Lives: ' + lives); waveText.setText('Wave: ' + wave); if (showTowerMenu && selectedTower) { // Show upgrade option } } function getGridPosition(x, y) { var gridX = Math.floor(x / gridSize); var gridY = Math.floor(y / gridSize); if (gridX >= 0 && gridX < gridWidth && gridY >= 0 && gridY < gridHeight) { return gameGrid[gridY][gridX]; } return null; } function canPlaceTower(gridPos) { return gridPos && !gridPos.occupied && !gridPos.isPath; } function spawnEnemy() { var enemyType = 'basic'; var rand = Math.random(); // 5% chance for gold enemy (high reward, low health) if (rand < 0.05) { enemyType = 'gold'; } else if (wave > 5) { if (rand < 0.3) enemyType = 'fast';else if (rand < 0.6) enemyType = 'tank'; } else if (wave > 2) { if (rand < 0.2) enemyType = 'fast'; } var enemy = new Enemy(enemyType); enemy.x = pathPoints[0].x; enemy.y = pathPoints[0].y; enemies.push(enemy); game.addChild(enemy); enemiesSpawned++; } function startWave() { if (waveInProgress) return; waveInProgress = true; enemiesSpawned = 0; enemiesPerWave = 10 + wave * 2; spawnTimer = 0; showTowerMenu = false; selectedTower = null; } // Event handlers startWaveButton.down = function () { // Add touch animation - scale down then back up tween(startWaveButton, { scaleX: 0.8, scaleY: 0.8, tint: 0x00AAFF }, { duration: 100, onFinish: function onFinish() { tween(startWaveButton, { scaleX: 1.0, scaleY: 1.0, tint: 0xFFFFFF }, { duration: 150 }); } }); startWave(); }; basicTowerButton.down = function () { selectedTowerType = 'basic'; showTowerMenu = false; }; splashTowerButton.down = function () { selectedTowerType = 'splash'; showTowerMenu = false; }; slowTowerButton.down = function () { selectedTowerType = 'slow'; showTowerMenu = false; }; sniperTowerButton.down = function () { selectedTowerType = 'sniper'; showTowerMenu = false; }; game.down = function (x, y, obj) { if (showTowerMenu) { showTowerMenu = false; selectedTower = null; return; } var gridPos = getGridPosition(x, y); if (canPlaceTower(gridPos)) { var tower = new Tower(selectedTowerType); if (coins >= tower.cost) { coins -= tower.cost; tower.x = gridPos.x; tower.y = gridPos.y; gridPos.occupied = true; towers.push(tower); game.addChild(tower); updateUI(); } } }; game.update = function () { // Spawn enemies during wave if (waveInProgress && enemiesSpawned < enemiesPerWave) { spawnTimer++; if (spawnTimer >= 60) { spawnEnemy(); spawnTimer = 0; } } // Reset kill streak if lives were lost if (lives < lastLives) { killStreak = 0; lastLives = lives; } // Check if wave is complete if (waveInProgress && enemiesSpawned >= enemiesPerWave && enemies.length === 0) { waveInProgress = false; wave++; // Wave completion bonus + interest on current money var waveBonus = 50; var interestBonus = Math.floor(coins * 0.1); // 10% interest coins += waveBonus + interestBonus; updateUI(); } // Clean up bullets for (var i = bullets.length - 1; i >= 0; i--) { var bullet = bullets[i]; if (!bullet.parent) { bullets.splice(i, 1); } } // Check win condition if (wave > 20) { LK.showYouWin(); } };
===================================================================
--- original.js
+++ change.js
@@ -521,8 +521,25 @@
selectedTower = null;
}
// Event handlers
startWaveButton.down = function () {
+ // Add touch animation - scale down then back up
+ tween(startWaveButton, {
+ scaleX: 0.8,
+ scaleY: 0.8,
+ tint: 0x00AAFF
+ }, {
+ duration: 100,
+ onFinish: function onFinish() {
+ tween(startWaveButton, {
+ scaleX: 1.0,
+ scaleY: 1.0,
+ tint: 0xFFFFFF
+ }, {
+ duration: 150
+ });
+ }
+ });
startWave();
};
basicTowerButton.down = function () {
selectedTowerType = 'basic';
a tower of a tower defence. In-Game asset. 2d. High contrast. No shadows
slime. In-Game asset. 2d. High contrast. No shadows
splash tower of tower defense. In-Game asset. 2d. High contrast. No shadows
water bullet. In-Game asset. 2d. High contrast. No shadows
puddle of water. In-Game asset. 2d. High contrast. No shadows
slow tower of tower defense. In-Game asset. 2d. High contrast. No shadows
ice ball. In-Game asset. 2d. High contrast. No shadows
the sniper tower of tower defense. In-Game asset. 2d. High contrast. No shadows
sniper bullet. In-Game asset. 2d. High contrast. No shadows
floor. In-Game asset. 2d. High contrast. No shadows
fast enemy. In-Game asset. 2d. High contrast. No shadows
tank. In-Game asset. 2d. High contrast. No shadows