User prompt
make smooth swipe control
User prompt
add background asset
User prompt
make clouds random sizes moving from upper screen to bottom screen ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
delete text weapon level
User prompt
add boss fight
User prompt
delete text upgrade weapon
User prompt
delete shield text
User prompt
delete upgrade text on screen
User prompt
delete all upgrade logic
User prompt
fix
User prompt
erase upgrade shield
User prompt
fix upgrade shield
User prompt
Please fix the bug: 'Uncaught ReferenceError: specialWeaponBtn is not defined' in or related to this line: 'if (obj === specialWeaponBtn && player.weaponLevel >= 3) {' Line Number: 932
User prompt
fox upgrade weapon
User prompt
reduce enemy bullet speed
User prompt
make smooth control
User prompt
smooth control player ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Player energy decreases quickly when attacked by enemies
User prompt
add moving white cloud to the bottom ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
more fast get double coins
User prompt
fast get much coin
User prompt
enemy one hit get dead
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'setText')' in or related to this line: 'scoreTxt.setText('Score: ' + score);' Line Number: 638
Code edit (1 edits merged)
Please save this source code
User prompt
Cargo Ship Survival: Pirate Defense
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0,
money: 0,
weaponLevel: 1,
shieldLevel: 0
});
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.9
});
self.speed = -15;
self.damage = 1;
self.active = true;
self.update = function () {
if (!self.active) return;
self.y += self.speed;
};
self.hit = function () {
self.active = false;
LK.getSound('explosion').play();
// Flash effect
tween(bulletGraphics, {
alpha: 0
}, {
duration: 200,
onFinish: function onFinish() {
self.destroy();
}
});
};
return self;
});
var CargoShip = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('cargoShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.maxHealth = 100;
self.shootCooldown = 0;
self.weaponLevel = storage.weaponLevel;
self.shieldLevel = storage.shieldLevel;
self.shield = null;
self.shieldHealth = 0;
self.maxShieldHealth = 0;
self.active = true;
if (self.shieldLevel > 0) {
self.shield = self.attachAsset('shield', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.4
});
self.shieldHealth = self.shieldLevel * 20;
self.maxShieldHealth = self.shieldHealth;
}
self.shoot = function () {
if (self.shootCooldown <= 0 && self.active) {
LK.getSound('playerShoot').play();
// Create main bullet
var bullet = new Bullet();
bullet.x = self.x;
bullet.y = self.y - 80;
bullet.damage = self.weaponLevel;
bullets.push(bullet);
game.addChild(bullet);
// Add side bullets based on weapon level
if (self.weaponLevel >= 2) {
var bulletLeft = new Bullet();
bulletLeft.x = self.x - 100;
bulletLeft.y = self.y - 50;
bulletLeft.damage = Math.ceil(self.weaponLevel / 2);
bullets.push(bulletLeft);
game.addChild(bulletLeft);
var bulletRight = new Bullet();
bulletRight.x = self.x + 100;
bulletRight.y = self.y - 50;
bulletRight.damage = Math.ceil(self.weaponLevel / 2);
bullets.push(bulletRight);
game.addChild(bulletRight);
}
// Add more bullets for higher weapon levels
if (self.weaponLevel >= 4) {
var bulletFarLeft = new Bullet();
bulletFarLeft.x = self.x - 180;
bulletFarLeft.y = self.y - 20;
bulletFarLeft.damage = Math.ceil(self.weaponLevel / 3);
bullets.push(bulletFarLeft);
game.addChild(bulletFarLeft);
var bulletFarRight = new Bullet();
bulletFarRight.x = self.x + 180;
bulletFarRight.y = self.y - 20;
bulletFarRight.damage = Math.ceil(self.weaponLevel / 3);
bullets.push(bulletFarRight);
game.addChild(bulletFarRight);
}
// Set cooldown based on weapon level (higher level = faster shooting)
self.shootCooldown = 30 - self.weaponLevel * 2;
if (self.shootCooldown < 10) self.shootCooldown = 10; // Minimum cooldown
}
};
self.takeDamage = function (amount) {
if (!self.active) return;
// Shield absorbs damage first
if (self.shield && self.shieldHealth > 0) {
self.shieldHealth -= amount;
// Flash shield
tween(self.shield, {
alpha: 0.8
}, {
duration: 100,
onFinish: function onFinish() {
tween(self.shield, {
alpha: 0.4
}, {
duration: 200
});
}
});
if (self.shieldHealth <= 0) {
self.shield.visible = false;
}
LK.getSound('damage').play();
return;
}
self.health -= amount;
LK.getSound('damage').play();
// Flash ship red
tween(shipGraphics, {
tint: 0xff0000
}, {
duration: 100,
onFinish: function onFinish() {
tween(shipGraphics, {
tint: 0xffffff
}, {
duration: 200
});
}
});
// Shake effect
var originalX = self.x;
var originalY = self.y;
tween(self, {
x: originalX + 20
}, {
duration: 50,
onFinish: function onFinish() {
tween(self, {
x: originalX - 20
}, {
duration: 100,
onFinish: function onFinish() {
tween(self, {
x: originalX
}, {
duration: 50
});
}
});
}
});
if (self.health <= 0) {
self.destroy();
self.active = false;
gameOver = true;
// Save high score
if (score > storage.highScore) {
storage.highScore = score;
}
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
}
};
self.update = function () {
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
// Auto-shoot
if (autoShoot) {
self.shoot();
}
};
return self;
});
var PirateBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('pirateBullet', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.9
});
self.speed = 8;
self.damage = 1;
self.active = true;
self.update = function () {
if (!self.active) return;
self.y += self.speed;
};
self.hit = function () {
self.active = false;
// Flash effect
tween(bulletGraphics, {
alpha: 0
}, {
duration: 200,
onFinish: function onFinish() {
self.destroy();
}
});
};
return self;
});
var PirateShip = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('pirateShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 3;
self.speed = 3;
self.shootCooldown = Math.floor(Math.random() * 60) + 30;
self.moveDirection = Math.random() > 0.5 ? 1 : -1;
self.movementChangeTimer = Math.floor(Math.random() * 60) + 60;
self.active = true;
self.update = function () {
if (!self.active) return;
// Move downward
self.y += self.speed;
// Horizontal movement
self.x += self.moveDirection * 2;
// Change direction occasionally
self.movementChangeTimer--;
if (self.movementChangeTimer <= 0) {
self.moveDirection *= -1;
self.movementChangeTimer = Math.floor(Math.random() * 60) + 60;
}
// Keep within screen bounds
if (self.x < 150) {
self.x = 150;
self.moveDirection = 1;
} else if (self.x > 2048 - 150) {
self.x = 2048 - 150;
self.moveDirection = -1;
}
// Shooting logic
self.shootCooldown--;
if (self.shootCooldown <= 0 && self.y > 0 && self.y < 2000) {
self.shoot();
self.shootCooldown = Math.floor(Math.random() * 120) + 60;
}
// Destroy if off screen
if (self.y > 2732 + 100) {
self.destroy();
// Find and remove from pirates array
for (var i = 0; i < pirates.length; i++) {
if (pirates[i] === self) {
pirates.splice(i, 1);
break;
}
}
}
};
self.shoot = function () {
if (!self.active) return;
LK.getSound('pirateShoot').play();
var bullet = new PirateBullet();
bullet.x = self.x;
bullet.y = self.y + 50;
pirateBullets.push(bullet);
game.addChild(bullet);
};
self.takeDamage = function (amount) {
if (!self.active) return;
self.health = 0; // Set health to 0 immediately for one-hit kill
// Flash red
tween(shipGraphics, {
tint: 0xff0000
}, {
duration: 100,
onFinish: function onFinish() {
tween(shipGraphics, {
tint: 0xffffff
}, {
duration: 200
});
}
});
if (self.health <= 0) {
self.active = false;
// Create explosion effect
var explosion = LK.getAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x,
y: self.y,
alpha: 0.9,
scaleX: 1.5,
scaleY: 1.5
});
game.addChild(explosion);
// Animate explosion
tween(explosion, {
alpha: 0,
scaleX: 3,
scaleY: 3
}, {
duration: 500,
onFinish: function onFinish() {
explosion.destroy();
}
});
LK.getSound('explosion').play();
// Drop coin with 30% chance or if it's a larger pirate
if (Math.random() < 0.3 || self.health > 3) {
var coin = LK.getAsset('coin', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x,
y: self.y
});
coins.push({
sprite: coin,
x: self.x,
y: self.y,
vx: (Math.random() - 0.5) * 3,
vy: Math.random() * 2 + 3
});
game.addChild(coin);
}
// Remove from array
for (var i = 0; i < pirates.length; i++) {
if (pirates[i] === self) {
pirates.splice(i, 1);
break;
}
}
// Increase score
score += 10;
updateScore();
self.destroy();
}
};
return self;
});
var LargePirateShip = PirateShip.expand(function () {
var self = PirateShip.call(this);
// Get the pirateShip asset that was attached in the parent class
var shipGraphics = self.children[0];
shipGraphics.scaleX = 1.8;
shipGraphics.scaleY = 1.5;
self.health = 8;
self.speed = 2;
self.shoot = function () {
if (!self.active) return;
LK.getSound('pirateShoot').play();
// Shoot 3 bullets in a spread pattern
for (var i = -1; i <= 1; i++) {
var bullet = new PirateBullet();
bullet.x = self.x + i * 60;
bullet.y = self.y + 60;
pirateBullets.push(bullet);
game.addChild(bullet);
}
};
self.takeDamage = function (amount) {
if (!self.active) return;
self.health -= amount;
// Flash red
tween(shipGraphics, {
tint: 0xff0000
}, {
duration: 100,
onFinish: function onFinish() {
tween(shipGraphics, {
tint: 0xffffff
}, {
duration: 200
});
}
});
if (self.health <= 0) {
self.active = false;
// Create explosion effect
var explosion = LK.getAsset('explosion', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x,
y: self.y,
alpha: 0.9,
scaleX: 2.5,
scaleY: 2.5
});
game.addChild(explosion);
// Animate explosion
tween(explosion, {
alpha: 0,
scaleX: 4,
scaleY: 4
}, {
duration: 700,
onFinish: function onFinish() {
explosion.destroy();
}
});
LK.getSound('explosion').play();
// Always drop a coin
var coin = LK.getAsset('coin', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x,
y: self.y
});
coins.push({
sprite: coin,
x: self.x,
y: self.y,
vx: (Math.random() - 0.5) * 3,
vy: Math.random() * 2 + 3
});
game.addChild(coin);
// Drop another coin
var coin2 = LK.getAsset('coin', {
anchorX: 0.5,
anchorY: 0.5,
x: self.x + 30,
y: self.y - 20
});
coins.push({
sprite: coin2,
x: self.x + 30,
y: self.y - 20,
vx: (Math.random() - 0.5) * 3,
vy: Math.random() * 2 + 3
});
game.addChild(coin2);
// Remove from array
for (var i = 0; i < pirates.length; i++) {
if (pirates[i] === self) {
pirates.splice(i, 1);
break;
}
}
// Increase score
score += 30;
updateScore();
self.destroy();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x0a2550 // Deep blue ocean color
});
/****
* Game Code
****/
// Game variables
var player;
var bullets = [];
var pirates = [];
var pirateBullets = [];
var coins = [];
var score = 0;
var money = storage.money;
var gameOver = false;
var spawnTimer = 0;
var waveTimer = 0;
var currentWave = 1;
var enemiesThisWave = 5;
var enemiesSpawned = 0;
var healthBar;
var healthBarBg;
var shieldBar;
var autoShoot = true;
// UI elements
var scoreTxt;
var waveTxt;
var moneyTxt;
var weaponUpgradeBtn;
var shieldUpgradeBtn;
var weaponLevelTxt;
var shieldLevelTxt;
// Initialize player
function initPlayer() {
player = new CargoShip();
player.x = 2048 / 2;
player.y = 2732 - 200;
game.addChild(player);
// Create health bar background
healthBarBg = LK.getAsset('healthBarBg', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 50
});
game.addChild(healthBarBg);
// Create health bar
healthBar = LK.getAsset('healthBar', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 50
});
game.addChild(healthBar);
// Create shield bar if player has shields
if (player.shieldLevel > 0) {
shieldBar = LK.getAsset('shield', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 90,
height: 20,
width: 500,
alpha: 0.6
});
game.addChild(shieldBar);
}
}
// Initialize UI
function initUI() {
// Score text
scoreTxt = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreTxt);
// Wave text
waveTxt = new Text2('Wave: 1', {
size: 60,
fill: 0xFFFFFF
});
waveTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(waveTxt);
// Money text
moneyTxt = new Text2('Coins: ' + money, {
size: 60,
fill: 0xFFCC00
});
moneyTxt.anchor.set(0, 0);
moneyTxt.x = 120;
LK.gui.topLeft.addChild(moneyTxt);
// Weapon upgrade button
weaponUpgradeBtn = LK.getAsset('upgradeButton', {
anchorX: 0.5,
anchorY: 0.5
});
weaponUpgradeBtn.x = 2048 / 2 - 350;
weaponUpgradeBtn.y = 2732 - 80;
game.addChild(weaponUpgradeBtn);
// Weapon level text
weaponLevelTxt = new Text2('Upgrade Weapon\n' + getUpgradeCost(player.weaponLevel) + ' coins', {
size: 40,
fill: 0xFFFFFF
});
weaponLevelTxt.anchor.set(0.5, 0.5);
weaponLevelTxt.x = weaponUpgradeBtn.x;
weaponLevelTxt.y = weaponUpgradeBtn.y;
game.addChild(weaponLevelTxt);
// Shield upgrade button
shieldUpgradeBtn = LK.getAsset('repairButton', {
anchorX: 0.5,
anchorY: 0.5
});
shieldUpgradeBtn.x = 2048 / 2 + 350;
shieldUpgradeBtn.y = 2732 - 80;
game.addChild(shieldUpgradeBtn);
// Shield level text
shieldLevelTxt = new Text2('Upgrade Shield\n' + getShieldCost(player.shieldLevel) + ' coins', {
size: 40,
fill: 0xFFFFFF
});
shieldLevelTxt.anchor.set(0.5, 0.5);
shieldLevelTxt.x = shieldUpgradeBtn.x;
shieldLevelTxt.y = shieldUpgradeBtn.y;
game.addChild(shieldLevelTxt);
}
// Update score display
function updateScore() {
scoreTxt.setText('Score: ' + score);
LK.setScore(score);
}
// Update money display
function updateMoney() {
moneyTxt.setText('Coins: ' + money);
storage.money = money;
}
// Update wave display
function updateWave() {
waveTxt.setText('Wave: ' + currentWave);
}
// Get upgrade cost based on current level
function getUpgradeCost(level) {
return level * 50;
}
// Get shield upgrade cost
function getShieldCost(level) {
return (level + 1) * 80;
}
// Spawn a pirate ship
function spawnPirate() {
var pirateX = Math.random() * (2048 - 300) + 150;
var pirateY = -100;
var pirate;
// Spawn large pirates occasionally, more often in higher waves
if (Math.random() < 0.1 + currentWave * 0.02) {
pirate = new LargePirateShip();
pirate.speed += Math.min(currentWave * 0.1, 1); // Increase speed with wave
} else {
pirate = new PirateShip();
pirate.speed += Math.min(currentWave * 0.2, 2); // Increase speed with wave
pirate.health += Math.floor(currentWave / 3); // Increase health with wave
}
pirate.x = pirateX;
pirate.y = pirateY;
pirates.push(pirate);
game.addChild(pirate);
enemiesSpawned++;
}
// Start a new wave
function startNewWave() {
currentWave++;
enemiesThisWave = 5 + currentWave * 2;
enemiesSpawned = 0;
updateWave();
// Flash wave text
tween(waveTxt, {
scale: 1.5
}, {
duration: 300,
onFinish: function onFinish() {
tween(waveTxt, {
scale: 1.0
}, {
duration: 300
});
}
});
}
// Upgrade weapon
function upgradeWeapon() {
var cost = getUpgradeCost(player.weaponLevel);
if (money >= cost) {
money -= cost;
player.weaponLevel++;
storage.weaponLevel = player.weaponLevel;
updateMoney();
// Update button text
weaponLevelTxt.setText('Upgrade Weapon\n' + getUpgradeCost(player.weaponLevel) + ' coins');
LK.getSound('upgrade').play();
// Show effect
tween(weaponUpgradeBtn, {
alpha: 0.5
}, {
duration: 200,
onFinish: function onFinish() {
tween(weaponUpgradeBtn, {
alpha: 1.0
}, {
duration: 200
});
}
});
}
}
// Upgrade shield
function upgradeShield() {
var cost = getShieldCost(player.shieldLevel);
if (money >= cost) {
money -= cost;
player.shieldLevel++;
storage.shieldLevel = player.shieldLevel;
updateMoney();
// Update button text
shieldLevelTxt.setText('Upgrade Shield\n' + getShieldCost(player.shieldLevel) + ' coins');
// Add shield if first level
if (player.shieldLevel === 1) {
player.shield = player.attachAsset('shield', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.4
});
// Create shield bar
shieldBar = LK.getAsset('shield', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 90,
height: 20,
width: 500,
alpha: 0.6
});
game.addChild(shieldBar);
}
// Restore and increase shield
player.shieldHealth = player.shieldLevel * 20;
player.maxShieldHealth = player.shieldHealth;
if (player.shield) {
player.shield.visible = true;
}
LK.getSound('upgrade').play();
// Show effect
tween(shieldUpgradeBtn, {
alpha: 0.5
}, {
duration: 200,
onFinish: function onFinish() {
tween(shieldUpgradeBtn, {
alpha: 1.0
}, {
duration: 200
});
}
});
}
}
// Handle touch/mouse events
function handleTouchMove(x, y) {
if (gameOver) return;
// Move player
if (player && player.active) {
player.x = x;
// Constrain to screen
if (player.x < 250) player.x = 250;
if (player.x > 2048 - 250) player.x = 2048 - 250;
}
}
// Game initialization
function initGame() {
// Initialize game state
score = 0;
money = storage.money || 0;
gameOver = false;
spawnTimer = 0;
waveTimer = 0;
currentWave = 1;
enemiesThisWave = 5;
enemiesSpawned = 0;
bullets = [];
pirates = [];
pirateBullets = [];
coins = [];
// Initialize player and UI first
initPlayer();
initUI();
// Now update displays after UI is initialized
updateScore();
updateMoney();
// Start background music
LK.playMusic('bgMusic');
}
// Touch/mouse events
game.move = function (x, y) {
handleTouchMove(x, y);
};
game.down = function (x, y, obj) {
// Check if clicked on upgrade buttons
if (obj === weaponUpgradeBtn) {
upgradeWeapon();
return;
}
if (obj === shieldUpgradeBtn) {
upgradeShield();
return;
}
// Otherwise move the player
handleTouchMove(x, y);
// Manual shoot on tap
if (player && player.active) {
player.shoot();
}
};
// Main game update loop
game.update = function () {
if (gameOver) return;
// Update player
if (player && player.active) {
player.update();
// Update health bar
var healthRatio = player.health / player.maxHealth;
healthBar.width = 500 * healthRatio;
// Update shield bar
if (shieldBar && player.shieldLevel > 0) {
var shieldRatio = player.shieldHealth / player.maxShieldHealth;
shieldBar.width = 500 * shieldRatio;
}
}
// Spawn enemies
spawnTimer--;
if (spawnTimer <= 0 && enemiesSpawned < enemiesThisWave) {
spawnPirate();
spawnTimer = Math.max(30, 90 - currentWave * 5); // Spawn faster in higher waves
}
// Check if wave is complete
if (enemiesSpawned >= enemiesThisWave && pirates.length === 0) {
waveTimer--;
if (waveTimer <= 0) {
startNewWave();
waveTimer = 180; // Delay before next wave
}
}
// Update pirates
for (var i = 0; i < pirates.length; i++) {
pirates[i].update();
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
bullet.update();
// Check for off-screen
if (bullet.y < -50) {
bullet.destroy();
bullets.splice(i, 1);
continue;
}
// Check for collisions with pirates
for (var j = 0; j < pirates.length; j++) {
var pirate = pirates[j];
if (bullet.active && pirate.active && bullet.intersects(pirate)) {
pirate.takeDamage(bullet.damage);
bullet.hit();
break;
}
}
}
// Update pirate bullets
for (var i = pirateBullets.length - 1; i >= 0; i--) {
var bullet = pirateBullets[i];
bullet.update();
// Check for off-screen
if (bullet.y > 2732 + 50) {
bullet.destroy();
pirateBullets.splice(i, 1);
continue;
}
// Check for collision with player
if (bullet.active && player && player.active && bullet.intersects(player)) {
player.takeDamage(bullet.damage);
bullet.hit();
pirateBullets.splice(i, 1);
}
}
// Update coins
for (var i = coins.length - 1; i >= 0; i--) {
var coin = coins[i];
// Apply physics
coin.y += coin.vy;
coin.x += coin.vx;
coin.vy += 0.1; // Gravity
// Update sprite position
coin.sprite.x = coin.x;
coin.sprite.y = coin.y;
// Check if coin is collected
if (player && player.active && Math.abs(coin.x - player.x) < 300 && Math.abs(coin.y - player.y) < 150) {
coin.sprite.destroy();
coins.splice(i, 1);
money++;
updateMoney();
LK.getSound('coinPickup').play();
continue;
}
// Remove if off screen
if (coin.y > 2732 + 50) {
coin.sprite.destroy();
coins.splice(i, 1);
}
}
};
// Initialize the game
initGame(); ===================================================================
--- original.js
+++ change.js
@@ -283,9 +283,9 @@
game.addChild(bullet);
};
self.takeDamage = function (amount) {
if (!self.active) return;
- self.health -= amount;
+ self.health = 0; // Set health to 0 immediately for one-hit kill
// Flash red
tween(shipGraphics, {
tint: 0xff0000
}, {