Code edit (1 edits merged)
Please save this source code
User prompt
add sound for each event.
User prompt
write a small orange “Glaud” in a logical place on the screen. write it as text.
User prompt
write a small orange “Glaud” in a logical place on the screen.
User prompt
add a small orange “Glaud” in a logical place on the screen.
User prompt
add a small orange “Glaud” in a logical place on the screen.
User prompt
Make the text visible on the screen. The design should be good.
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'width')' in or related to this line: 'if (self.x > LK.game.width + padding) {' Line Number: 49
User prompt
Identify and solve some basic logic errors in the game.
User prompt
Identify the features that will make it better and implement them in an optimized way, don't make any mistakes, it's clean and playable. Make it a user-friendly game. ↪💡 Consider importing and using the following plugins: @upit/tween.v1, @upit/storage.v1
Code edit (1 edits merged)
Please save this source code
User prompt
Cosmic Cargo Commander
Initial prompt
game
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.rotationSpeed = (Math.random() - 0.5) * 0.04;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
asteroidGraphics.rotation += self.rotationSpeed;
// Wrap around screen
if (self.x < -100) {
self.x = 2148;
}
if (self.x > 2148) {
self.x = -100;
}
if (self.y < -100) {
self.y = 2832;
}
if (self.y > 2832) {
self.y = -100;
}
};
return self;
});
var CargoContainer = Container.expand(function () {
var self = Container.call(this);
var cargoGraphics = self.attachAsset('cargoContainer', {
anchorX: 0.5,
anchorY: 0.5
});
self.bobSpeed = 0.02;
self.bobAmount = 5;
self.bobPhase = Math.random() * Math.PI * 2;
self.originalY = 0;
self.update = function () {
self.bobPhase += self.bobSpeed;
if (self.originalY) {
self.y = self.originalY + Math.sin(self.bobPhase) * self.bobAmount;
}
};
return self;
});
var CargoShip = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('cargoShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 6;
self.fuel = 100;
self.maxFuel = 100;
self.fuelConsumption = 0.05;
self.cargoCapacity = 3;
self.cargo = 0;
self.shieldActive = false;
self.magnetActive = false;
self.magnetRange = 300;
var shield = self.attachAsset('shield', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.4
});
shield.visible = false;
self.activateShield = function (duration) {
if (!self.shieldActive) {
self.shieldActive = true;
shield.visible = true;
LK.setTimeout(function () {
self.shieldActive = false;
shield.visible = false;
}, duration || 5000);
}
};
self.activateMagnet = function (duration) {
if (!self.magnetActive) {
self.magnetActive = true;
LK.setTimeout(function () {
self.magnetActive = false;
}, duration || 5000);
}
};
self.consumeFuel = function () {
self.fuel -= self.fuelConsumption;
if (self.fuel <= 0) {
self.fuel = 0;
}
if (self.fuel <= 20 && self.fuel > 19.95) {
LK.getSound('lowFuel').play();
}
};
self.refuel = function (amount) {
self.fuel = Math.min(self.fuel + amount, self.maxFuel);
};
self.pickupCargo = function () {
if (self.cargo < self.cargoCapacity) {
self.cargo++;
return true;
}
return false;
};
self.deliverCargo = function () {
var delivered = self.cargo;
self.cargo = 0;
return delivered;
};
return self;
});
var EnemyDrone = Container.expand(function () {
var self = Container.call(this);
var droneGraphics = self.attachAsset('enemyDrone', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.target = null;
self.patrolRadius = 300;
self.patrolCenter = {
x: 0,
y: 0
};
self.patrolAngle = Math.random() * Math.PI * 2;
self.patrolSpeed = 0.01;
self.setPatrolCenter = function (x, y, radius) {
self.patrolCenter.x = x;
self.patrolCenter.y = y;
if (radius) {
self.patrolRadius = radius;
}
};
self.setTarget = function (target) {
self.target = target;
};
self.update = function () {
if (self.target) {
// Move toward target
var dx = self.target.x - self.x;
var dy = self.target.y - self.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist > 5) {
self.x += dx / dist * self.speed;
self.y += dy / dist * self.speed;
}
} else {
// Patrol in a circle
self.patrolAngle += self.patrolSpeed;
self.x = self.patrolCenter.x + Math.cos(self.patrolAngle) * self.patrolRadius;
self.y = self.patrolCenter.y + Math.sin(self.patrolAngle) * self.patrolRadius;
}
};
return self;
});
var Powerup = Container.expand(function (type) {
var self = Container.call(this);
self.type = type || 'fuel';
var assetType;
switch (self.type) {
case 'fuel':
assetType = 'fuelPowerup';
break;
case 'shield':
assetType = 'shieldPowerup';
break;
case 'magnet':
assetType = 'magnet';
break;
default:
assetType = 'fuelPowerup';
}
var powerupGraphics = self.attachAsset(assetType, {
anchorX: 0.5,
anchorY: 0.5
});
self.bobSpeed = 0.03;
self.bobAmount = 5;
self.bobPhase = Math.random() * Math.PI * 2;
self.originalY = 0;
self.rotationSpeed = 0.02;
self.update = function () {
self.bobPhase += self.bobSpeed;
powerupGraphics.rotation += self.rotationSpeed;
if (self.originalY) {
self.y = self.originalY + Math.sin(self.bobPhase) * self.bobAmount;
}
};
return self;
});
var SpaceStation = Container.expand(function () {
var self = Container.call(this);
var stationGraphics = self.attachAsset('spaceStation', {
anchorX: 0.5,
anchorY: 0.5
});
self.rotationSpeed = 0.002;
self.update = function () {
stationGraphics.rotation += self.rotationSpeed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000033
});
/****
* Game Code
****/
// Game state
var ship;
var asteroids = [];
var cargoContainers = [];
var spaceStations = [];
var enemyDrones = [];
var powerups = [];
var fuelBarBg;
var fuelBarFront;
var cargoText;
var scoreText;
var levelText;
var gameActive = true;
var level = 1;
var score = 0;
var destX = 0;
var destY = 0;
var dragNode = null;
var lastSpawnTime = 0;
var difficultyTimer;
// Initialize UI elements
function initUI() {
// Score text
scoreText = new Text2('Score: 0', {
size: 60,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Level text
levelText = new Text2('Level: 1', {
size: 60,
fill: 0xFFFFFF
});
levelText.anchor.set(1, 0);
levelText.x = -50;
LK.gui.topRight.addChild(levelText);
// Cargo text
cargoText = new Text2('Cargo: 0/3', {
size: 60,
fill: 0xFFFFFF
});
cargoText.anchor.set(0, 0);
cargoText.x = 50;
LK.gui.topLeft.addChild(cargoText);
// Fuel bar background
fuelBarBg = LK.getAsset('fuelBarBackground', {
anchorX: 0.5,
anchorY: 0.5
});
fuelBarBg.x = 0;
fuelBarBg.y = 40;
LK.gui.bottom.addChild(fuelBarBg);
// Fuel bar
fuelBarFront = LK.getAsset('fuelBar', {
anchorX: 0,
anchorY: 0.5
});
fuelBarFront.x = -fuelBarBg.width / 2;
fuelBarFront.y = 40;
LK.gui.bottom.addChild(fuelBarFront);
}
// Create cargo ship
function createShip() {
ship = new CargoShip();
ship.x = 2048 / 2;
ship.y = 2732 / 2;
game.addChild(ship);
}
// Create random asteroid
function createAsteroid() {
var asteroid = new Asteroid();
// Random position on the edge of the screen
var side = Math.floor(Math.random() * 4);
switch (side) {
case 0:
// Top
asteroid.x = Math.random() * 2048;
asteroid.y = -100;
break;
case 1:
// Right
asteroid.x = 2148;
asteroid.y = Math.random() * 2732;
break;
case 2:
// Bottom
asteroid.x = Math.random() * 2048;
asteroid.y = 2832;
break;
case 3:
// Left
asteroid.x = -100;
asteroid.y = Math.random() * 2732;
break;
}
// Random speed (higher in higher levels)
var baseSpeed = 2 + level * 0.5;
var speed = baseSpeed * (0.5 + Math.random() * 0.5);
// Direction toward center with some randomness
var targetX = 2048 / 2 + (Math.random() - 0.5) * 1000;
var targetY = 2732 / 2 + (Math.random() - 0.5) * 1000;
var dx = targetX - asteroid.x;
var dy = targetY - asteroid.y;
var dist = Math.sqrt(dx * dx + dy * dy);
asteroid.speedX = dx / dist * speed;
asteroid.speedY = dy / dist * speed;
// Scale randomly
var scale = 0.8 + Math.random() * 0.8;
asteroid.scale.set(scale, scale);
asteroids.push(asteroid);
game.addChild(asteroid);
}
// Create cargo container
function createCargoContainer() {
var container = new CargoContainer();
// Random position (not too close to the edge)
container.x = 200 + Math.random() * (2048 - 400);
container.y = 200 + Math.random() * (2732 - 400);
container.originalY = container.y;
cargoContainers.push(container);
game.addChild(container);
}
// Create space station
function createSpaceStation() {
var station = new SpaceStation();
// Position near an edge but not too close
var side = Math.floor(Math.random() * 4);
switch (side) {
case 0:
// Top
station.x = 400 + Math.random() * (2048 - 800);
station.y = 300;
break;
case 1:
// Right
station.x = 2048 - 300;
station.y = 400 + Math.random() * (2732 - 800);
break;
case 2:
// Bottom
station.x = 400 + Math.random() * (2048 - 800);
station.y = 2732 - 300;
break;
case 3:
// Left
station.x = 300;
station.y = 400 + Math.random() * (2732 - 800);
break;
}
spaceStations.push(station);
game.addChild(station);
}
// Create enemy drone
function createEnemyDrone() {
var drone = new EnemyDrone();
// Random position
drone.x = Math.random() * 2048;
drone.y = Math.random() * 2732;
// Set patrol area
drone.setPatrolCenter(drone.x, drone.y, 200 + Math.random() * 200);
// Set speed based on level
drone.speed = 1 + level * 0.3;
enemyDrones.push(drone);
game.addChild(drone);
}
// Create powerup
function createPowerup(type) {
if (!type) {
var types = ['fuel', 'shield', 'magnet'];
type = types[Math.floor(Math.random() * types.length)];
}
var powerup = new Powerup(type);
// Random position (not too close to the edge)
powerup.x = 300 + Math.random() * (2048 - 600);
powerup.y = 300 + Math.random() * (2732 - 600);
powerup.originalY = powerup.y;
powerups.push(powerup);
game.addChild(powerup);
}
// Spawn game objects based on level
function spawnObjects() {
var currentTime = Date.now();
// Only spawn new objects every 1-3 seconds depending on level
var spawnRate = Math.max(3000 - level * 200, 1000);
if (currentTime - lastSpawnTime > spawnRate) {
// Spawn asteroid
if (asteroids.length < 5 + level * 2) {
createAsteroid();
}
// Spawn cargo
if (cargoContainers.length < 3 + Math.floor(level / 2)) {
createCargoContainer();
}
// Spawn enemy drones in higher levels
if (level >= 2 && enemyDrones.length < level - 1) {
createEnemyDrone();
}
// Spawn powerup (less frequently)
if (Math.random() < 0.2) {
createPowerup();
}
lastSpawnTime = currentTime;
}
}
// Update ship movement based on destination
function updateShipMovement() {
if (destX && destY) {
var dx = destX - ship.x;
var dy = destY - ship.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist > 5) {
// Move toward destination
ship.x += dx / dist * ship.speed;
ship.y += dy / dist * ship.speed;
// Consume fuel while moving
ship.consumeFuel();
}
}
}
// Update enemy drones
function updateEnemyDrones() {
for (var i = 0; i < enemyDrones.length; i++) {
var drone = enemyDrones[i];
// Set ship as target if it's close
var dx = ship.x - drone.x;
var dy = ship.y - drone.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < 500) {
drone.setTarget(ship);
} else {
drone.setTarget(null);
}
}
}
// Update UI elements
function updateUI() {
// Update fuel bar
var fuelPercentage = ship.fuel / ship.maxFuel;
fuelBarFront.width = fuelBarBg.width * fuelPercentage;
// Change fuel bar color based on level
if (fuelPercentage <= 0.2) {
fuelBarFront.tint = 0xe74c3c; // Red
} else if (fuelPercentage <= 0.5) {
fuelBarFront.tint = 0xf39c12; // Orange
} else {
fuelBarFront.tint = 0x27ae60; // Green
}
// Update text displays
cargoText.setText('Cargo: ' + ship.cargo + '/' + ship.cargoCapacity);
scoreText.setText('Score: ' + score);
levelText.setText('Level: ' + level);
}
// Check for ship collisions with game objects
function checkCollisions() {
// Check asteroid collisions
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
if (ship.intersects(asteroid)) {
if (ship.shieldActive) {
// Destroy asteroid if shield is active
asteroid.destroy();
asteroids.splice(i, 1);
// Play collision sound
LK.getSound('collision').play();
// Add points
score += 5;
} else {
// Game over if no shield
gameOver();
return;
}
}
}
// Check enemy drone collisions
for (var i = enemyDrones.length - 1; i >= 0; i--) {
var drone = enemyDrones[i];
if (ship.intersects(drone)) {
if (ship.shieldActive) {
// Destroy drone if shield is active
drone.destroy();
enemyDrones.splice(i, 1);
// Play collision sound
LK.getSound('collision').play();
// Add points
score += 10;
} else {
// Game over if no shield
gameOver();
return;
}
}
}
// Check cargo container pickups
for (var i = cargoContainers.length - 1; i >= 0; i--) {
var container = cargoContainers[i];
// Check direct collision
if (ship.intersects(container)) {
if (ship.pickupCargo()) {
// Remove container and add points
container.destroy();
cargoContainers.splice(i, 1);
// Play pickup sound
LK.getSound('pickup').play();
// Add points
score += 10;
}
}
// Check magnet range
else if (ship.magnetActive) {
var dx = ship.x - container.x;
var dy = ship.y - container.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < ship.magnetRange) {
// Move container toward ship
container.x += dx / dist * 5;
container.y += dy / dist * 5;
container.originalY = container.y;
}
}
}
// Check space station delivery
for (var i = 0; i < spaceStations.length; i++) {
var station = spaceStations[i];
if (ship.intersects(station) && ship.cargo > 0) {
// Deliver cargo
var deliveredCargo = ship.deliverCargo();
// Add points
var deliveryPoints = deliveredCargo * 25;
score += deliveryPoints;
// Play delivery sound
LK.getSound('delivery').play();
// Flash screen green
LK.effects.flashScreen(0x27ae60, 300);
// Check for level up
if (score >= level * 100) {
levelUp();
}
}
}
// Check powerup pickups
for (var i = powerups.length - 1; i >= 0; i--) {
var powerup = powerups[i];
if (ship.intersects(powerup)) {
// Apply powerup effect
switch (powerup.type) {
case 'fuel':
ship.refuel(50);
break;
case 'shield':
ship.activateShield(8000);
break;
case 'magnet':
ship.activateMagnet(10000);
break;
}
// Play powerup sound
LK.getSound('powerup').play();
// Add points
score += 5;
// Remove powerup
powerup.destroy();
powerups.splice(i, 1);
}
}
// Check if out of fuel
if (ship.fuel <= 0 && gameActive) {
gameOver();
}
}
// Level up function
function levelUp() {
level++;
// Update level text
levelText.setText('Level: ' + level);
// Flash screen
LK.effects.flashScreen(0xf1c40f, 500);
// Create new space station if needed
if (spaceStations.length < Math.min(3, Math.floor(level / 2) + 1)) {
createSpaceStation();
}
// Increase ship speed slightly
ship.speed = 6 + level * 0.3;
}
// Game over function
function gameOver() {
gameActive = false;
// Flash screen red
LK.effects.flashScreen(0xe74c3c, 1000);
// Set final score
LK.setScore(score);
// Show game over
LK.showGameOver();
}
// Initialize game
function initGame() {
// Initialize UI
initUI();
// Create ship
createShip();
// Create initial objects
for (var i = 0; i < 5; i++) {
createAsteroid();
}
for (var i = 0; i < 3; i++) {
createCargoContainer();
}
// Create initial space station
createSpaceStation();
// Create initial powerup
createPowerup('fuel');
// Start background music
LK.playMusic('gameMusic');
}
// Handle game taps
function handleDown(x, y, obj) {
destX = x;
destY = y;
}
// Handle game moves
function handleMove(x, y, obj) {
if (dragNode) {
destX = x;
destY = y;
}
}
// Handle game releases
function handleUp(x, y, obj) {
dragNode = null;
}
// Set event handlers
game.down = handleDown;
game.move = handleMove;
game.up = handleUp;
// Initialize the game
initGame();
// Game update loop
game.update = function () {
if (gameActive) {
// Spawn new objects based on level
spawnObjects();
// Update ship movement
updateShipMovement();
// Update enemy drones
updateEnemyDrones();
// Check collisions
checkCollisions();
// Update UI
updateUI();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,674 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var Asteroid = Container.expand(function () {
+ var self = Container.call(this);
+ var asteroidGraphics = self.attachAsset('asteroid', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speedX = 0;
+ self.speedY = 0;
+ self.rotationSpeed = (Math.random() - 0.5) * 0.04;
+ self.update = function () {
+ self.x += self.speedX;
+ self.y += self.speedY;
+ asteroidGraphics.rotation += self.rotationSpeed;
+ // Wrap around screen
+ if (self.x < -100) {
+ self.x = 2148;
+ }
+ if (self.x > 2148) {
+ self.x = -100;
+ }
+ if (self.y < -100) {
+ self.y = 2832;
+ }
+ if (self.y > 2832) {
+ self.y = -100;
+ }
+ };
+ return self;
+});
+var CargoContainer = Container.expand(function () {
+ var self = Container.call(this);
+ var cargoGraphics = self.attachAsset('cargoContainer', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.bobSpeed = 0.02;
+ self.bobAmount = 5;
+ self.bobPhase = Math.random() * Math.PI * 2;
+ self.originalY = 0;
+ self.update = function () {
+ self.bobPhase += self.bobSpeed;
+ if (self.originalY) {
+ self.y = self.originalY + Math.sin(self.bobPhase) * self.bobAmount;
+ }
+ };
+ return self;
+});
+var CargoShip = Container.expand(function () {
+ var self = Container.call(this);
+ var shipGraphics = self.attachAsset('cargoShip', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 6;
+ self.fuel = 100;
+ self.maxFuel = 100;
+ self.fuelConsumption = 0.05;
+ self.cargoCapacity = 3;
+ self.cargo = 0;
+ self.shieldActive = false;
+ self.magnetActive = false;
+ self.magnetRange = 300;
+ var shield = self.attachAsset('shield', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ alpha: 0.4
+ });
+ shield.visible = false;
+ self.activateShield = function (duration) {
+ if (!self.shieldActive) {
+ self.shieldActive = true;
+ shield.visible = true;
+ LK.setTimeout(function () {
+ self.shieldActive = false;
+ shield.visible = false;
+ }, duration || 5000);
+ }
+ };
+ self.activateMagnet = function (duration) {
+ if (!self.magnetActive) {
+ self.magnetActive = true;
+ LK.setTimeout(function () {
+ self.magnetActive = false;
+ }, duration || 5000);
+ }
+ };
+ self.consumeFuel = function () {
+ self.fuel -= self.fuelConsumption;
+ if (self.fuel <= 0) {
+ self.fuel = 0;
+ }
+ if (self.fuel <= 20 && self.fuel > 19.95) {
+ LK.getSound('lowFuel').play();
+ }
+ };
+ self.refuel = function (amount) {
+ self.fuel = Math.min(self.fuel + amount, self.maxFuel);
+ };
+ self.pickupCargo = function () {
+ if (self.cargo < self.cargoCapacity) {
+ self.cargo++;
+ return true;
+ }
+ return false;
+ };
+ self.deliverCargo = function () {
+ var delivered = self.cargo;
+ self.cargo = 0;
+ return delivered;
+ };
+ return self;
+});
+var EnemyDrone = Container.expand(function () {
+ var self = Container.call(this);
+ var droneGraphics = self.attachAsset('enemyDrone', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 2;
+ self.target = null;
+ self.patrolRadius = 300;
+ self.patrolCenter = {
+ x: 0,
+ y: 0
+ };
+ self.patrolAngle = Math.random() * Math.PI * 2;
+ self.patrolSpeed = 0.01;
+ self.setPatrolCenter = function (x, y, radius) {
+ self.patrolCenter.x = x;
+ self.patrolCenter.y = y;
+ if (radius) {
+ self.patrolRadius = radius;
+ }
+ };
+ self.setTarget = function (target) {
+ self.target = target;
+ };
+ self.update = function () {
+ if (self.target) {
+ // Move toward target
+ var dx = self.target.x - self.x;
+ var dy = self.target.y - self.y;
+ var dist = Math.sqrt(dx * dx + dy * dy);
+ if (dist > 5) {
+ self.x += dx / dist * self.speed;
+ self.y += dy / dist * self.speed;
+ }
+ } else {
+ // Patrol in a circle
+ self.patrolAngle += self.patrolSpeed;
+ self.x = self.patrolCenter.x + Math.cos(self.patrolAngle) * self.patrolRadius;
+ self.y = self.patrolCenter.y + Math.sin(self.patrolAngle) * self.patrolRadius;
+ }
+ };
+ return self;
+});
+var Powerup = Container.expand(function (type) {
+ var self = Container.call(this);
+ self.type = type || 'fuel';
+ var assetType;
+ switch (self.type) {
+ case 'fuel':
+ assetType = 'fuelPowerup';
+ break;
+ case 'shield':
+ assetType = 'shieldPowerup';
+ break;
+ case 'magnet':
+ assetType = 'magnet';
+ break;
+ default:
+ assetType = 'fuelPowerup';
+ }
+ var powerupGraphics = self.attachAsset(assetType, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.bobSpeed = 0.03;
+ self.bobAmount = 5;
+ self.bobPhase = Math.random() * Math.PI * 2;
+ self.originalY = 0;
+ self.rotationSpeed = 0.02;
+ self.update = function () {
+ self.bobPhase += self.bobSpeed;
+ powerupGraphics.rotation += self.rotationSpeed;
+ if (self.originalY) {
+ self.y = self.originalY + Math.sin(self.bobPhase) * self.bobAmount;
+ }
+ };
+ return self;
+});
+var SpaceStation = Container.expand(function () {
+ var self = Container.call(this);
+ var stationGraphics = self.attachAsset('spaceStation', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.rotationSpeed = 0.002;
+ self.update = function () {
+ stationGraphics.rotation += self.rotationSpeed;
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x000033
+});
+
+/****
+* Game Code
+****/
+// Game state
+var ship;
+var asteroids = [];
+var cargoContainers = [];
+var spaceStations = [];
+var enemyDrones = [];
+var powerups = [];
+var fuelBarBg;
+var fuelBarFront;
+var cargoText;
+var scoreText;
+var levelText;
+var gameActive = true;
+var level = 1;
+var score = 0;
+var destX = 0;
+var destY = 0;
+var dragNode = null;
+var lastSpawnTime = 0;
+var difficultyTimer;
+// Initialize UI elements
+function initUI() {
+ // Score text
+ scoreText = new Text2('Score: 0', {
+ size: 60,
+ fill: 0xFFFFFF
+ });
+ scoreText.anchor.set(0.5, 0);
+ LK.gui.top.addChild(scoreText);
+ // Level text
+ levelText = new Text2('Level: 1', {
+ size: 60,
+ fill: 0xFFFFFF
+ });
+ levelText.anchor.set(1, 0);
+ levelText.x = -50;
+ LK.gui.topRight.addChild(levelText);
+ // Cargo text
+ cargoText = new Text2('Cargo: 0/3', {
+ size: 60,
+ fill: 0xFFFFFF
+ });
+ cargoText.anchor.set(0, 0);
+ cargoText.x = 50;
+ LK.gui.topLeft.addChild(cargoText);
+ // Fuel bar background
+ fuelBarBg = LK.getAsset('fuelBarBackground', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ fuelBarBg.x = 0;
+ fuelBarBg.y = 40;
+ LK.gui.bottom.addChild(fuelBarBg);
+ // Fuel bar
+ fuelBarFront = LK.getAsset('fuelBar', {
+ anchorX: 0,
+ anchorY: 0.5
+ });
+ fuelBarFront.x = -fuelBarBg.width / 2;
+ fuelBarFront.y = 40;
+ LK.gui.bottom.addChild(fuelBarFront);
+}
+// Create cargo ship
+function createShip() {
+ ship = new CargoShip();
+ ship.x = 2048 / 2;
+ ship.y = 2732 / 2;
+ game.addChild(ship);
+}
+// Create random asteroid
+function createAsteroid() {
+ var asteroid = new Asteroid();
+ // Random position on the edge of the screen
+ var side = Math.floor(Math.random() * 4);
+ switch (side) {
+ case 0:
+ // Top
+ asteroid.x = Math.random() * 2048;
+ asteroid.y = -100;
+ break;
+ case 1:
+ // Right
+ asteroid.x = 2148;
+ asteroid.y = Math.random() * 2732;
+ break;
+ case 2:
+ // Bottom
+ asteroid.x = Math.random() * 2048;
+ asteroid.y = 2832;
+ break;
+ case 3:
+ // Left
+ asteroid.x = -100;
+ asteroid.y = Math.random() * 2732;
+ break;
+ }
+ // Random speed (higher in higher levels)
+ var baseSpeed = 2 + level * 0.5;
+ var speed = baseSpeed * (0.5 + Math.random() * 0.5);
+ // Direction toward center with some randomness
+ var targetX = 2048 / 2 + (Math.random() - 0.5) * 1000;
+ var targetY = 2732 / 2 + (Math.random() - 0.5) * 1000;
+ var dx = targetX - asteroid.x;
+ var dy = targetY - asteroid.y;
+ var dist = Math.sqrt(dx * dx + dy * dy);
+ asteroid.speedX = dx / dist * speed;
+ asteroid.speedY = dy / dist * speed;
+ // Scale randomly
+ var scale = 0.8 + Math.random() * 0.8;
+ asteroid.scale.set(scale, scale);
+ asteroids.push(asteroid);
+ game.addChild(asteroid);
+}
+// Create cargo container
+function createCargoContainer() {
+ var container = new CargoContainer();
+ // Random position (not too close to the edge)
+ container.x = 200 + Math.random() * (2048 - 400);
+ container.y = 200 + Math.random() * (2732 - 400);
+ container.originalY = container.y;
+ cargoContainers.push(container);
+ game.addChild(container);
+}
+// Create space station
+function createSpaceStation() {
+ var station = new SpaceStation();
+ // Position near an edge but not too close
+ var side = Math.floor(Math.random() * 4);
+ switch (side) {
+ case 0:
+ // Top
+ station.x = 400 + Math.random() * (2048 - 800);
+ station.y = 300;
+ break;
+ case 1:
+ // Right
+ station.x = 2048 - 300;
+ station.y = 400 + Math.random() * (2732 - 800);
+ break;
+ case 2:
+ // Bottom
+ station.x = 400 + Math.random() * (2048 - 800);
+ station.y = 2732 - 300;
+ break;
+ case 3:
+ // Left
+ station.x = 300;
+ station.y = 400 + Math.random() * (2732 - 800);
+ break;
+ }
+ spaceStations.push(station);
+ game.addChild(station);
+}
+// Create enemy drone
+function createEnemyDrone() {
+ var drone = new EnemyDrone();
+ // Random position
+ drone.x = Math.random() * 2048;
+ drone.y = Math.random() * 2732;
+ // Set patrol area
+ drone.setPatrolCenter(drone.x, drone.y, 200 + Math.random() * 200);
+ // Set speed based on level
+ drone.speed = 1 + level * 0.3;
+ enemyDrones.push(drone);
+ game.addChild(drone);
+}
+// Create powerup
+function createPowerup(type) {
+ if (!type) {
+ var types = ['fuel', 'shield', 'magnet'];
+ type = types[Math.floor(Math.random() * types.length)];
+ }
+ var powerup = new Powerup(type);
+ // Random position (not too close to the edge)
+ powerup.x = 300 + Math.random() * (2048 - 600);
+ powerup.y = 300 + Math.random() * (2732 - 600);
+ powerup.originalY = powerup.y;
+ powerups.push(powerup);
+ game.addChild(powerup);
+}
+// Spawn game objects based on level
+function spawnObjects() {
+ var currentTime = Date.now();
+ // Only spawn new objects every 1-3 seconds depending on level
+ var spawnRate = Math.max(3000 - level * 200, 1000);
+ if (currentTime - lastSpawnTime > spawnRate) {
+ // Spawn asteroid
+ if (asteroids.length < 5 + level * 2) {
+ createAsteroid();
+ }
+ // Spawn cargo
+ if (cargoContainers.length < 3 + Math.floor(level / 2)) {
+ createCargoContainer();
+ }
+ // Spawn enemy drones in higher levels
+ if (level >= 2 && enemyDrones.length < level - 1) {
+ createEnemyDrone();
+ }
+ // Spawn powerup (less frequently)
+ if (Math.random() < 0.2) {
+ createPowerup();
+ }
+ lastSpawnTime = currentTime;
+ }
+}
+// Update ship movement based on destination
+function updateShipMovement() {
+ if (destX && destY) {
+ var dx = destX - ship.x;
+ var dy = destY - ship.y;
+ var dist = Math.sqrt(dx * dx + dy * dy);
+ if (dist > 5) {
+ // Move toward destination
+ ship.x += dx / dist * ship.speed;
+ ship.y += dy / dist * ship.speed;
+ // Consume fuel while moving
+ ship.consumeFuel();
+ }
+ }
+}
+// Update enemy drones
+function updateEnemyDrones() {
+ for (var i = 0; i < enemyDrones.length; i++) {
+ var drone = enemyDrones[i];
+ // Set ship as target if it's close
+ var dx = ship.x - drone.x;
+ var dy = ship.y - drone.y;
+ var dist = Math.sqrt(dx * dx + dy * dy);
+ if (dist < 500) {
+ drone.setTarget(ship);
+ } else {
+ drone.setTarget(null);
+ }
+ }
+}
+// Update UI elements
+function updateUI() {
+ // Update fuel bar
+ var fuelPercentage = ship.fuel / ship.maxFuel;
+ fuelBarFront.width = fuelBarBg.width * fuelPercentage;
+ // Change fuel bar color based on level
+ if (fuelPercentage <= 0.2) {
+ fuelBarFront.tint = 0xe74c3c; // Red
+ } else if (fuelPercentage <= 0.5) {
+ fuelBarFront.tint = 0xf39c12; // Orange
+ } else {
+ fuelBarFront.tint = 0x27ae60; // Green
+ }
+ // Update text displays
+ cargoText.setText('Cargo: ' + ship.cargo + '/' + ship.cargoCapacity);
+ scoreText.setText('Score: ' + score);
+ levelText.setText('Level: ' + level);
+}
+// Check for ship collisions with game objects
+function checkCollisions() {
+ // Check asteroid collisions
+ for (var i = asteroids.length - 1; i >= 0; i--) {
+ var asteroid = asteroids[i];
+ if (ship.intersects(asteroid)) {
+ if (ship.shieldActive) {
+ // Destroy asteroid if shield is active
+ asteroid.destroy();
+ asteroids.splice(i, 1);
+ // Play collision sound
+ LK.getSound('collision').play();
+ // Add points
+ score += 5;
+ } else {
+ // Game over if no shield
+ gameOver();
+ return;
+ }
+ }
+ }
+ // Check enemy drone collisions
+ for (var i = enemyDrones.length - 1; i >= 0; i--) {
+ var drone = enemyDrones[i];
+ if (ship.intersects(drone)) {
+ if (ship.shieldActive) {
+ // Destroy drone if shield is active
+ drone.destroy();
+ enemyDrones.splice(i, 1);
+ // Play collision sound
+ LK.getSound('collision').play();
+ // Add points
+ score += 10;
+ } else {
+ // Game over if no shield
+ gameOver();
+ return;
+ }
+ }
+ }
+ // Check cargo container pickups
+ for (var i = cargoContainers.length - 1; i >= 0; i--) {
+ var container = cargoContainers[i];
+ // Check direct collision
+ if (ship.intersects(container)) {
+ if (ship.pickupCargo()) {
+ // Remove container and add points
+ container.destroy();
+ cargoContainers.splice(i, 1);
+ // Play pickup sound
+ LK.getSound('pickup').play();
+ // Add points
+ score += 10;
+ }
+ }
+ // Check magnet range
+ else if (ship.magnetActive) {
+ var dx = ship.x - container.x;
+ var dy = ship.y - container.y;
+ var dist = Math.sqrt(dx * dx + dy * dy);
+ if (dist < ship.magnetRange) {
+ // Move container toward ship
+ container.x += dx / dist * 5;
+ container.y += dy / dist * 5;
+ container.originalY = container.y;
+ }
+ }
+ }
+ // Check space station delivery
+ for (var i = 0; i < spaceStations.length; i++) {
+ var station = spaceStations[i];
+ if (ship.intersects(station) && ship.cargo > 0) {
+ // Deliver cargo
+ var deliveredCargo = ship.deliverCargo();
+ // Add points
+ var deliveryPoints = deliveredCargo * 25;
+ score += deliveryPoints;
+ // Play delivery sound
+ LK.getSound('delivery').play();
+ // Flash screen green
+ LK.effects.flashScreen(0x27ae60, 300);
+ // Check for level up
+ if (score >= level * 100) {
+ levelUp();
+ }
+ }
+ }
+ // Check powerup pickups
+ for (var i = powerups.length - 1; i >= 0; i--) {
+ var powerup = powerups[i];
+ if (ship.intersects(powerup)) {
+ // Apply powerup effect
+ switch (powerup.type) {
+ case 'fuel':
+ ship.refuel(50);
+ break;
+ case 'shield':
+ ship.activateShield(8000);
+ break;
+ case 'magnet':
+ ship.activateMagnet(10000);
+ break;
+ }
+ // Play powerup sound
+ LK.getSound('powerup').play();
+ // Add points
+ score += 5;
+ // Remove powerup
+ powerup.destroy();
+ powerups.splice(i, 1);
+ }
+ }
+ // Check if out of fuel
+ if (ship.fuel <= 0 && gameActive) {
+ gameOver();
+ }
+}
+// Level up function
+function levelUp() {
+ level++;
+ // Update level text
+ levelText.setText('Level: ' + level);
+ // Flash screen
+ LK.effects.flashScreen(0xf1c40f, 500);
+ // Create new space station if needed
+ if (spaceStations.length < Math.min(3, Math.floor(level / 2) + 1)) {
+ createSpaceStation();
+ }
+ // Increase ship speed slightly
+ ship.speed = 6 + level * 0.3;
+}
+// Game over function
+function gameOver() {
+ gameActive = false;
+ // Flash screen red
+ LK.effects.flashScreen(0xe74c3c, 1000);
+ // Set final score
+ LK.setScore(score);
+ // Show game over
+ LK.showGameOver();
+}
+// Initialize game
+function initGame() {
+ // Initialize UI
+ initUI();
+ // Create ship
+ createShip();
+ // Create initial objects
+ for (var i = 0; i < 5; i++) {
+ createAsteroid();
+ }
+ for (var i = 0; i < 3; i++) {
+ createCargoContainer();
+ }
+ // Create initial space station
+ createSpaceStation();
+ // Create initial powerup
+ createPowerup('fuel');
+ // Start background music
+ LK.playMusic('gameMusic');
+}
+// Handle game taps
+function handleDown(x, y, obj) {
+ destX = x;
+ destY = y;
+}
+// Handle game moves
+function handleMove(x, y, obj) {
+ if (dragNode) {
+ destX = x;
+ destY = y;
+ }
+}
+// Handle game releases
+function handleUp(x, y, obj) {
+ dragNode = null;
+}
+// Set event handlers
+game.down = handleDown;
+game.move = handleMove;
+game.up = handleUp;
+// Initialize the game
+initGame();
+// Game update loop
+game.update = function () {
+ if (gameActive) {
+ // Spawn new objects based on level
+ spawnObjects();
+ // Update ship movement
+ updateShipMovement();
+ // Update enemy drones
+ updateEnemyDrones();
+ // Check collisions
+ checkCollisions();
+ // Update UI
+ updateUI();
+ }
+};
\ No newline at end of file
asteroid. In-Game asset. 2d. High contrast. No shadows
cargoContainer. In-Game asset. 2d. High contrast. No shadows
cargo spaceShip. In-Game asset. 2d. High contrast. No shadows
enemy space ship. In-Game asset. 2d. High contrast. No shadows
fuelPowerup. In-Game asset. 2d. High contrast. No shadows
spaceStation. In-Game asset. 2d. High contrast. No shadows
magnet. In-Game asset. 2d. High contrast. No shadows
shieldPowerup. In-Game asset. 2d. High contrast. No shadows
shield. In-Game asset. 2d. High contrast. No shadows