/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0
});
/****
* Classes
****/
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
// Randomize asteroid appearance
asteroidGraphics.rotation = Math.random() * Math.PI * 2;
var scale = 0.6 + Math.random() * 0.8;
asteroidGraphics.scale.set(scale, scale);
self.speed = 2 + Math.random() * 3;
self.rotationSpeed = (Math.random() - 0.5) * 0.05;
self.width = asteroidGraphics.width * scale;
self.height = asteroidGraphics.height * scale;
self.update = function () {
self.y += self.speed;
asteroidGraphics.rotation += self.rotationSpeed;
};
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = -8;
self.width = bulletGraphics.width;
self.height = bulletGraphics.height;
self.update = function () {
self.y += self.speed;
};
return self;
});
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerupGraphics = self.attachAsset('powerup', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = Math.floor(Math.random() * 3); // 0: shield, 1: slow motion, 2: blaster
// Set color based on powerup type
if (self.type === 0) {
powerupGraphics.tint = 0x3498db; // Blue for shield
} else if (self.type === 1) {
powerupGraphics.tint = 0xf1c40f; // Yellow for slow motion
} else {
powerupGraphics.tint = 0xe74c3c; // Red for blaster
}
self.speed = 1.5 + Math.random() * 1.5;
self.width = powerupGraphics.width;
self.height = powerupGraphics.height;
self.update = function () {
self.y += self.speed;
powerupGraphics.rotation += 0.02;
};
return self;
});
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.shield = self.attachAsset('shield', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0
});
self.hasShield = false;
self.canShoot = false;
self.width = shipGraphics.width;
self.height = shipGraphics.height;
self.activateShield = function (duration) {
self.hasShield = true;
self.shield.alpha = 0.5;
// Deactivate shield after duration
LK.setTimeout(function () {
self.deactivateShield();
}, duration);
};
self.deactivateShield = function () {
self.hasShield = false;
self.shield.alpha = 0;
};
self.activateBlaster = function (duration) {
self.canShoot = true;
// Disable blaster after duration
LK.setTimeout(function () {
self.canShoot = false;
}, duration);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
game.setBackgroundColor(0x000022); // Deep space blue
// Game variables
var spaceship;
var asteroids = [];
var powerups = [];
var bullets = [];
var score = 0;
var gameSpeed = 1; // Used for slow-motion effect
var lastAsteroidTime = 0;
var lastPowerupTime = 0;
var isSlowMotion = false;
var gameStartTime = Date.now();
var dragNode = null;
// Load high score
var highScore = storage.highScore || 0;
// Create score text
var scoreTxt = new Text2('Score: 0', {
size: 50,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
scoreTxt.x = -250;
scoreTxt.y = 50;
// Create high score text
var highScoreTxt = new Text2('High Score: ' + highScore, {
size: 40,
fill: 0xAAAAAA
});
highScoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(highScoreTxt);
highScoreTxt.x = -250;
highScoreTxt.y = 110;
// Initialize game elements
function initGame() {
// Start game music
LK.playMusic('space_music', {
fade: {
start: 0,
end: 0.7,
duration: 1000
}
});
// Reset score
score = 0;
LK.setScore(0);
updateScoreDisplay();
// Reset game speed
gameSpeed = 1;
// Record start time
gameStartTime = Date.now();
// Create spaceship
spaceship = new Spaceship();
spaceship.x = 2048 / 2;
spaceship.y = 2732 - 300;
game.addChild(spaceship);
// Clear existing arrays
clearGameElements();
// Reset timers
lastAsteroidTime = 0;
lastPowerupTime = 0;
}
function clearGameElements() {
// Clear all asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
asteroids[i].destroy();
}
asteroids = [];
// Clear all powerups
for (var i = powerups.length - 1; i >= 0; i--) {
powerups[i].destroy();
}
powerups = [];
// Clear all bullets
for (var i = bullets.length - 1; i >= 0; i--) {
bullets[i].destroy();
}
bullets = [];
}
function updateScoreDisplay() {
scoreTxt.setText('Score: ' + score);
highScoreTxt.setText('High Score: ' + highScore);
}
function createAsteroid() {
var asteroid = new Asteroid();
asteroid.x = Math.random() * 2048;
asteroid.y = -100;
asteroids.push(asteroid);
game.addChild(asteroid);
}
function createPowerup() {
var powerup = new PowerUp();
powerup.x = 100 + Math.random() * (2048 - 200);
powerup.y = -100;
powerups.push(powerup);
game.addChild(powerup);
}
function createBullet() {
var bullet = new Bullet();
bullet.x = spaceship.x;
bullet.y = spaceship.y - 60;
bullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
}
function activateSlowMotion(duration) {
isSlowMotion = true;
gameSpeed = 0.5;
// Return to normal speed after duration
LK.setTimeout(function () {
isSlowMotion = false;
gameSpeed = 1;
}, duration);
}
function handleCollision() {
// Flash the screen red
LK.effects.flashScreen(0xff0000, 500);
// Play explosion sound
LK.getSound('explosion').play();
// Flash the ship
LK.effects.flashObject(spaceship, 0xff0000, 500);
// Store high score
if (score > highScore) {
highScore = score;
storage.highScore = highScore;
updateScoreDisplay();
}
// Show game over
LK.setTimeout(function () {
LK.showGameOver();
}, 500);
}
// Event handlers
function handleMove(x, y, obj) {
if (dragNode) {
// Keep spaceship within game bounds
dragNode.x = Math.max(dragNode.width / 2, Math.min(2048 - dragNode.width / 2, x));
dragNode.y = Math.max(dragNode.height / 2, Math.min(2732 - dragNode.height / 2, y));
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
dragNode = spaceship;
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
};
// Game update function
game.update = function () {
// Increment score based on survival time (10 points per second)
var currentTime = Date.now();
var elapsedSeconds = Math.floor((currentTime - gameStartTime) / 1000);
score = elapsedSeconds * 10;
LK.setScore(score);
updateScoreDisplay();
// Spawn asteroids with increasing frequency
var difficulty = Math.min(10, 1 + Math.floor(elapsedSeconds / 10));
var asteroidInterval = Math.max(30, 60 - difficulty * 5);
if (LK.ticks - lastAsteroidTime > asteroidInterval / gameSpeed) {
createAsteroid();
lastAsteroidTime = LK.ticks;
}
// Occasionally spawn power-ups
if (LK.ticks - lastPowerupTime > 300 / gameSpeed && Math.random() < 0.01) {
createPowerup();
lastPowerupTime = LK.ticks;
}
// Update asteroids
for (var i = asteroids.length - 1; i >= 0; i--) {
var asteroid = asteroids[i];
// Apply game speed to movement
asteroid.speed = asteroid.speed * gameSpeed;
asteroid.update();
asteroid.speed = asteroid.speed / gameSpeed;
// Check if asteroid is off screen
if (asteroid.y > 2732 + 100) {
asteroid.destroy();
asteroids.splice(i, 1);
continue;
}
// Check for collision with spaceship
if (spaceship && asteroid.intersects(spaceship)) {
if (spaceship.hasShield) {
// Shield protects the spaceship
spaceship.deactivateShield();
asteroid.destroy();
asteroids.splice(i, 1);
score += 25; // Bonus for destroying asteroid with shield
continue;
} else {
// Game over on collision
handleCollision();
return;
}
}
// Check for collision with bullets
for (var j = bullets.length - 1; j >= 0; j--) {
if (asteroid.intersects(bullets[j])) {
asteroid.destroy();
asteroids.splice(i, 1);
bullets[j].destroy();
bullets.splice(j, 1);
score += 50; // Bonus for destroying asteroid with bullet
break;
}
}
}
// Update powerups
for (var i = powerups.length - 1; i >= 0; i--) {
var powerup = powerups[i];
// Apply game speed to movement
powerup.speed = powerup.speed * gameSpeed;
powerup.update();
powerup.speed = powerup.speed / gameSpeed;
// Check if powerup is off screen
if (powerup.y > 2732 + 100) {
powerup.destroy();
powerups.splice(i, 1);
continue;
}
// Check for collision with spaceship
if (spaceship && powerup.intersects(spaceship)) {
// Play powerup sound
LK.getSound('powerup_collect').play();
// Apply powerup effect
if (powerup.type === 0) {
// Shield
spaceship.activateShield(10000);
} else if (powerup.type === 1) {
// Slow motion
activateSlowMotion(8000);
} else {
// Blaster
spaceship.activateBlaster(12000);
}
// Remove powerup
powerup.destroy();
powerups.splice(i, 1);
}
}
// Update bullets
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
bullet.update();
// Remove bullets that go off screen
if (bullet.y < -100) {
bullet.destroy();
bullets.splice(i, 1);
}
}
// Fire bullet if spaceship has blaster and space is pressed
if (spaceship && spaceship.canShoot && LK.ticks % 15 === 0) {
createBullet();
}
};
// Initialize the game
initGame(); ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,374 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1", {
+ highScore: 0
+});
+
+/****
+* Classes
+****/
+var Asteroid = Container.expand(function () {
+ var self = Container.call(this);
+ var asteroidGraphics = self.attachAsset('asteroid', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ // Randomize asteroid appearance
+ asteroidGraphics.rotation = Math.random() * Math.PI * 2;
+ var scale = 0.6 + Math.random() * 0.8;
+ asteroidGraphics.scale.set(scale, scale);
+ self.speed = 2 + Math.random() * 3;
+ self.rotationSpeed = (Math.random() - 0.5) * 0.05;
+ self.width = asteroidGraphics.width * scale;
+ self.height = asteroidGraphics.height * scale;
+ self.update = function () {
+ self.y += self.speed;
+ asteroidGraphics.rotation += self.rotationSpeed;
+ };
+ return self;
+});
+var Bullet = Container.expand(function () {
+ var self = Container.call(this);
+ var bulletGraphics = self.attachAsset('bullet', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = -8;
+ self.width = bulletGraphics.width;
+ self.height = bulletGraphics.height;
+ self.update = function () {
+ self.y += self.speed;
+ };
+ return self;
+});
+var PowerUp = Container.expand(function () {
+ var self = Container.call(this);
+ var powerupGraphics = self.attachAsset('powerup', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.type = Math.floor(Math.random() * 3); // 0: shield, 1: slow motion, 2: blaster
+ // Set color based on powerup type
+ if (self.type === 0) {
+ powerupGraphics.tint = 0x3498db; // Blue for shield
+ } else if (self.type === 1) {
+ powerupGraphics.tint = 0xf1c40f; // Yellow for slow motion
+ } else {
+ powerupGraphics.tint = 0xe74c3c; // Red for blaster
+ }
+ self.speed = 1.5 + Math.random() * 1.5;
+ self.width = powerupGraphics.width;
+ self.height = powerupGraphics.height;
+ self.update = function () {
+ self.y += self.speed;
+ powerupGraphics.rotation += 0.02;
+ };
+ return self;
+});
+var Spaceship = Container.expand(function () {
+ var self = Container.call(this);
+ var shipGraphics = self.attachAsset('spaceship', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.shield = self.attachAsset('shield', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ alpha: 0
+ });
+ self.hasShield = false;
+ self.canShoot = false;
+ self.width = shipGraphics.width;
+ self.height = shipGraphics.height;
+ self.activateShield = function (duration) {
+ self.hasShield = true;
+ self.shield.alpha = 0.5;
+ // Deactivate shield after duration
+ LK.setTimeout(function () {
+ self.deactivateShield();
+ }, duration);
+ };
+ self.deactivateShield = function () {
+ self.hasShield = false;
+ self.shield.alpha = 0;
+ };
+ self.activateBlaster = function (duration) {
+ self.canShoot = true;
+ // Disable blaster after duration
+ LK.setTimeout(function () {
+ self.canShoot = false;
+ }, duration);
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
backgroundColor: 0x000000
-});
\ No newline at end of file
+});
+
+/****
+* Game Code
+****/
+game.setBackgroundColor(0x000022); // Deep space blue
+// Game variables
+var spaceship;
+var asteroids = [];
+var powerups = [];
+var bullets = [];
+var score = 0;
+var gameSpeed = 1; // Used for slow-motion effect
+var lastAsteroidTime = 0;
+var lastPowerupTime = 0;
+var isSlowMotion = false;
+var gameStartTime = Date.now();
+var dragNode = null;
+// Load high score
+var highScore = storage.highScore || 0;
+// Create score text
+var scoreTxt = new Text2('Score: 0', {
+ size: 50,
+ fill: 0xFFFFFF
+});
+scoreTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(scoreTxt);
+scoreTxt.x = -250;
+scoreTxt.y = 50;
+// Create high score text
+var highScoreTxt = new Text2('High Score: ' + highScore, {
+ size: 40,
+ fill: 0xAAAAAA
+});
+highScoreTxt.anchor.set(0, 0);
+LK.gui.topRight.addChild(highScoreTxt);
+highScoreTxt.x = -250;
+highScoreTxt.y = 110;
+// Initialize game elements
+function initGame() {
+ // Start game music
+ LK.playMusic('space_music', {
+ fade: {
+ start: 0,
+ end: 0.7,
+ duration: 1000
+ }
+ });
+ // Reset score
+ score = 0;
+ LK.setScore(0);
+ updateScoreDisplay();
+ // Reset game speed
+ gameSpeed = 1;
+ // Record start time
+ gameStartTime = Date.now();
+ // Create spaceship
+ spaceship = new Spaceship();
+ spaceship.x = 2048 / 2;
+ spaceship.y = 2732 - 300;
+ game.addChild(spaceship);
+ // Clear existing arrays
+ clearGameElements();
+ // Reset timers
+ lastAsteroidTime = 0;
+ lastPowerupTime = 0;
+}
+function clearGameElements() {
+ // Clear all asteroids
+ for (var i = asteroids.length - 1; i >= 0; i--) {
+ asteroids[i].destroy();
+ }
+ asteroids = [];
+ // Clear all powerups
+ for (var i = powerups.length - 1; i >= 0; i--) {
+ powerups[i].destroy();
+ }
+ powerups = [];
+ // Clear all bullets
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ bullets[i].destroy();
+ }
+ bullets = [];
+}
+function updateScoreDisplay() {
+ scoreTxt.setText('Score: ' + score);
+ highScoreTxt.setText('High Score: ' + highScore);
+}
+function createAsteroid() {
+ var asteroid = new Asteroid();
+ asteroid.x = Math.random() * 2048;
+ asteroid.y = -100;
+ asteroids.push(asteroid);
+ game.addChild(asteroid);
+}
+function createPowerup() {
+ var powerup = new PowerUp();
+ powerup.x = 100 + Math.random() * (2048 - 200);
+ powerup.y = -100;
+ powerups.push(powerup);
+ game.addChild(powerup);
+}
+function createBullet() {
+ var bullet = new Bullet();
+ bullet.x = spaceship.x;
+ bullet.y = spaceship.y - 60;
+ bullets.push(bullet);
+ game.addChild(bullet);
+ LK.getSound('shoot').play();
+}
+function activateSlowMotion(duration) {
+ isSlowMotion = true;
+ gameSpeed = 0.5;
+ // Return to normal speed after duration
+ LK.setTimeout(function () {
+ isSlowMotion = false;
+ gameSpeed = 1;
+ }, duration);
+}
+function handleCollision() {
+ // Flash the screen red
+ LK.effects.flashScreen(0xff0000, 500);
+ // Play explosion sound
+ LK.getSound('explosion').play();
+ // Flash the ship
+ LK.effects.flashObject(spaceship, 0xff0000, 500);
+ // Store high score
+ if (score > highScore) {
+ highScore = score;
+ storage.highScore = highScore;
+ updateScoreDisplay();
+ }
+ // Show game over
+ LK.setTimeout(function () {
+ LK.showGameOver();
+ }, 500);
+}
+// Event handlers
+function handleMove(x, y, obj) {
+ if (dragNode) {
+ // Keep spaceship within game bounds
+ dragNode.x = Math.max(dragNode.width / 2, Math.min(2048 - dragNode.width / 2, x));
+ dragNode.y = Math.max(dragNode.height / 2, Math.min(2732 - dragNode.height / 2, y));
+ }
+}
+game.move = handleMove;
+game.down = function (x, y, obj) {
+ dragNode = spaceship;
+ handleMove(x, y, obj);
+};
+game.up = function (x, y, obj) {
+ dragNode = null;
+};
+// Game update function
+game.update = function () {
+ // Increment score based on survival time (10 points per second)
+ var currentTime = Date.now();
+ var elapsedSeconds = Math.floor((currentTime - gameStartTime) / 1000);
+ score = elapsedSeconds * 10;
+ LK.setScore(score);
+ updateScoreDisplay();
+ // Spawn asteroids with increasing frequency
+ var difficulty = Math.min(10, 1 + Math.floor(elapsedSeconds / 10));
+ var asteroidInterval = Math.max(30, 60 - difficulty * 5);
+ if (LK.ticks - lastAsteroidTime > asteroidInterval / gameSpeed) {
+ createAsteroid();
+ lastAsteroidTime = LK.ticks;
+ }
+ // Occasionally spawn power-ups
+ if (LK.ticks - lastPowerupTime > 300 / gameSpeed && Math.random() < 0.01) {
+ createPowerup();
+ lastPowerupTime = LK.ticks;
+ }
+ // Update asteroids
+ for (var i = asteroids.length - 1; i >= 0; i--) {
+ var asteroid = asteroids[i];
+ // Apply game speed to movement
+ asteroid.speed = asteroid.speed * gameSpeed;
+ asteroid.update();
+ asteroid.speed = asteroid.speed / gameSpeed;
+ // Check if asteroid is off screen
+ if (asteroid.y > 2732 + 100) {
+ asteroid.destroy();
+ asteroids.splice(i, 1);
+ continue;
+ }
+ // Check for collision with spaceship
+ if (spaceship && asteroid.intersects(spaceship)) {
+ if (spaceship.hasShield) {
+ // Shield protects the spaceship
+ spaceship.deactivateShield();
+ asteroid.destroy();
+ asteroids.splice(i, 1);
+ score += 25; // Bonus for destroying asteroid with shield
+ continue;
+ } else {
+ // Game over on collision
+ handleCollision();
+ return;
+ }
+ }
+ // Check for collision with bullets
+ for (var j = bullets.length - 1; j >= 0; j--) {
+ if (asteroid.intersects(bullets[j])) {
+ asteroid.destroy();
+ asteroids.splice(i, 1);
+ bullets[j].destroy();
+ bullets.splice(j, 1);
+ score += 50; // Bonus for destroying asteroid with bullet
+ break;
+ }
+ }
+ }
+ // Update powerups
+ for (var i = powerups.length - 1; i >= 0; i--) {
+ var powerup = powerups[i];
+ // Apply game speed to movement
+ powerup.speed = powerup.speed * gameSpeed;
+ powerup.update();
+ powerup.speed = powerup.speed / gameSpeed;
+ // Check if powerup is off screen
+ if (powerup.y > 2732 + 100) {
+ powerup.destroy();
+ powerups.splice(i, 1);
+ continue;
+ }
+ // Check for collision with spaceship
+ if (spaceship && powerup.intersects(spaceship)) {
+ // Play powerup sound
+ LK.getSound('powerup_collect').play();
+ // Apply powerup effect
+ if (powerup.type === 0) {
+ // Shield
+ spaceship.activateShield(10000);
+ } else if (powerup.type === 1) {
+ // Slow motion
+ activateSlowMotion(8000);
+ } else {
+ // Blaster
+ spaceship.activateBlaster(12000);
+ }
+ // Remove powerup
+ powerup.destroy();
+ powerups.splice(i, 1);
+ }
+ }
+ // Update bullets
+ for (var i = bullets.length - 1; i >= 0; i--) {
+ var bullet = bullets[i];
+ bullet.update();
+ // Remove bullets that go off screen
+ if (bullet.y < -100) {
+ bullet.destroy();
+ bullets.splice(i, 1);
+ }
+ }
+ // Fire bullet if spaceship has blaster and space is pressed
+ if (spaceship && spaceship.canShoot && LK.ticks % 15 === 0) {
+ createBullet();
+ }
+};
+// Initialize the game
+initGame();
\ No newline at end of file