/**** * Classes ****/ var Asteroid = Container.expand(function () { var self = Container.call(this); var asteroidGraphics = self.attachAsset('asteroid', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Asteroid movement logic goes here }; }); var Astronaut = Container.expand(function () { var self = Container.call(this); var astronautGraphics = self.attachAsset('astronaut', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Astronaut movement logic goes here }; }); var Stardust = Container.expand(function () { var self = Container.call(this); var stardustGraphics = self.attachAsset('stardust', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Stardust movement logic goes here }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ var astronaut = game.addChild(new Astronaut()); var stardust = game.addChild(new Stardust()); var asteroid = game.addChild(new Asteroid()); game.update = function () { // Game logic goes here };
===================================================================
--- original.js
+++ change.js
@@ -1,690 +1,50 @@
/****
-* 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
});
- // Movement properties
- self.speedX = 0;
- self.speedY = 0;
- self.rotationSpeed = 0;
- self.setup = function (speedX, speedY, rotSpeed) {
- self.speedX = speedX || Math.random() * 4 - 2;
- self.speedY = speedY || Math.random() * 4 - 2;
- self.rotationSpeed = rotSpeed || Math.random() * 0.1 - 0.05;
- };
self.update = function () {
- self.x += self.speedX;
- self.y += self.speedY;
- asteroidGraphics.rotation += self.rotationSpeed;
- // Wrap around screen
- if (self.x < -50) {
- self.x = 2048 + 50;
- }
- if (self.x > 2048 + 50) {
- self.x = -50;
- }
- if (self.y < -50) {
- self.y = 2732 + 50;
- }
- if (self.y > 2732 + 50) {
- self.y = -50;
- }
+ // Asteroid movement logic goes here
};
- return self;
});
var Astronaut = Container.expand(function () {
var self = Container.call(this);
- // Astronaut graphics
var astronautGraphics = self.attachAsset('astronaut', {
anchorX: 0.5,
anchorY: 0.5
});
- // Physics properties
- self.velocityX = 0;
- self.velocityY = 0;
- self.gravity = 0.2;
- self.bounce = 0.8;
- self.isOnPlanet = false;
- self.jumpStrength = -10;
- self.maxVelocity = 15;
- // Jump when touching a planet
- self.jump = function () {
- if (self.isOnPlanet) {
- self.velocityY = self.jumpStrength;
- self.isOnPlanet = false;
- LK.getSound('bounce').play();
- // Visual feedback
- tween(astronautGraphics, {
- scaleX: 1.3,
- scaleY: 0.7
- }, {
- duration: 100,
- onFinish: function onFinish() {
- tween(astronautGraphics, {
- scaleX: 1,
- scaleY: 1
- }, {
- duration: 200,
- easing: tween.easeOut
- });
- }
- });
- }
- };
- // Update physics
self.update = function () {
- // Apply gravity
- if (!self.isOnPlanet) {
- self.velocityY += self.gravity;
- }
- // Limit velocity
- if (self.velocityY > self.maxVelocity) {
- self.velocityY = self.maxVelocity;
- }
- if (self.velocityY < -self.maxVelocity) {
- self.velocityY = -self.maxVelocity;
- }
- if (self.velocityX > self.maxVelocity) {
- self.velocityX = self.maxVelocity;
- }
- if (self.velocityX < -self.maxVelocity) {
- self.velocityX = -self.maxVelocity;
- }
- // Apply movement
- self.x += self.velocityX;
- self.y += self.velocityY;
- // Screen boundaries
- if (self.x < 50) {
- self.x = 50;
- self.velocityX *= -self.bounce;
- } else if (self.x > 2048 - 50) {
- self.x = 2048 - 50;
- self.velocityX *= -self.bounce;
- }
- if (self.y < 50) {
- self.y = 50;
- self.velocityY *= -self.bounce;
- } else if (self.y > 2732 - 50) {
- self.y = 2732 - 50;
- self.velocityY *= -self.bounce;
- self.isOnPlanet = true; // Touching bottom of screen counts as being on a surface
- }
- // Visual rotation based on movement
- astronautGraphics.rotation += self.velocityX * 0.01;
+ // Astronaut movement logic goes here
};
- return self;
});
-var Planet = Container.expand(function () {
- var self = Container.call(this);
- var planetGraphics = self.attachAsset('planet', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.radius = 100; // Default radius
- self.setSize = function (size) {
- self.radius = size / 2;
- planetGraphics.scale.set(size / 200);
- };
- 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 = "jump"; // Default type
- self.setType = function (powerupType) {
- self.type = powerupType;
- // Different colors for different power-ups
- switch (powerupType) {
- case "jump":
- powerupGraphics.tint = 0x00FF00; // Green
- break;
- case "slow":
- powerupGraphics.tint = 0x0000FF; // Blue
- break;
- case "shield":
- powerupGraphics.tint = 0xFF0000; // Red
- break;
- }
- };
- self.update = function () {
- // Float effect
- self.y += Math.sin(LK.ticks * 0.05) * 0.5;
- // Rotate slowly
- powerupGraphics.rotation += 0.01;
- };
- return self;
-});
-var SpaceStation = Container.expand(function () {
- var self = Container.call(this);
- var stationGraphics = self.attachAsset('spaceStation', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.update = function () {
- // Subtle hover effect
- self.y += Math.sin(LK.ticks * 0.02) * 0.3;
- };
- return self;
-});
var Stardust = Container.expand(function () {
var self = Container.call(this);
var stardustGraphics = self.attachAsset('stardust', {
anchorX: 0.5,
anchorY: 0.5
});
- // Animate stardust to make it sparkle
self.update = function () {
- stardustGraphics.rotation += 0.01;
- // Pulsating effect
- if (!self.pulseDirection) {
- self.pulseDirection = 1;
- }
- stardustGraphics.alpha += 0.01 * self.pulseDirection;
- if (stardustGraphics.alpha >= 1) {
- stardustGraphics.alpha = 1;
- self.pulseDirection = -1;
- } else if (stardustGraphics.alpha <= 0.6) {
- stardustGraphics.alpha = 0.6;
- self.pulseDirection = 1;
- }
+ // Stardust movement logic goes here
};
- return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000022 // Dark space background
+ backgroundColor: 0x000000
});
/****
* Game Code
****/
-// Game state variables
-var level = 1;
-var stardustCollected = 0;
-var totalStardustNeeded = 10;
-var gameActive = true;
-var playerHasShield = false;
-// Game objects
-var astronaut;
-var planets = [];
-var stardusts = [];
-var asteroids = [];
-var powerups = [];
-var spaceStation;
-// UI elements
-var scoreTxt;
-var levelTxt;
-var instructionsTxt;
-// Initialize game entities
-function initGame() {
- // Reset game state
- gameActive = true;
- stardustCollected = 0;
- playerHasShield = false;
- // Clear previous objects
- clearGameObjects();
- // Create astronaut
- astronaut = new Astronaut();
- astronaut.x = 1024;
- astronaut.y = 2300;
- game.addChild(astronaut);
- // Create planets based on level
- createPlanets();
- // Create stardust
- createStardust();
- // Create asteroids
- createAsteroids();
- // Create power-ups
- createPowerUps();
- // Create space station (goal)
- spaceStation = new SpaceStation();
- spaceStation.x = 1024;
- spaceStation.y = 200;
- game.addChild(spaceStation);
- // Create UI
- createUI();
- // Play background music
- LK.playMusic('spaceBgm');
-}
-// Clear all game objects
-function clearGameObjects() {
- // Remove all existing objects
- for (var i = game.children.length - 1; i >= 0; i--) {
- game.removeChild(game.children[i]);
- }
- planets = [];
- stardusts = [];
- asteroids = [];
- powerups = [];
-}
-// Create planets based on current level
-function createPlanets() {
- // Basic level layouts
- var layouts = [
- // Level 1: Simple layout
- [{
- x: 500,
- y: 1300,
- size: 300
- }, {
- x: 1500,
- y: 1800,
- size: 250
- }, {
- x: 1200,
- y: 800,
- size: 200
- }],
- // Level 2: More complex
- [{
- x: 400,
- y: 1400,
- size: 200
- }, {
- x: 1600,
- y: 1600,
- size: 200
- }, {
- x: 1000,
- y: 1000,
- size: 180
- }, {
- x: 600,
- y: 600,
- size: 150
- }],
- // Level 3: Advanced
- [{
- x: 300,
- y: 1500,
- size: 150
- }, {
- x: 800,
- y: 1800,
- size: 150
- }, {
- x: 1300,
- y: 1600,
- size: 150
- }, {
- x: 1700,
- y: 1200,
- size: 150
- }, {
- x: 500,
- y: 900,
- size: 150
- }, {
- x: 1200,
- y: 700,
- size: 150
- }]];
- // Use appropriate layout or generate random one for higher levels
- var planetData = level <= layouts.length ? layouts[level - 1] : generateRandomPlanets();
- // Create planets
- for (var i = 0; i < planetData.length; i++) {
- var planet = new Planet();
- planet.x = planetData[i].x;
- planet.y = planetData[i].y;
- planet.setSize(planetData[i].size);
- planets.push(planet);
- game.addChild(planet);
- }
-}
-// Generate random planets for higher levels
-function generateRandomPlanets() {
- var planetData = [];
- var numPlanets = Math.min(5 + level, 10); // More planets for higher levels, max 10
- for (var i = 0; i < numPlanets; i++) {
- planetData.push({
- x: 200 + Math.random() * 1648,
- // Keep away from edges
- y: 400 + Math.random() * 1932,
- // Keep away from top/bottom
- size: 120 + Math.random() * 150
- });
- }
- return planetData;
-}
-// Create stardust items
-function createStardust() {
- // Increase total stardust needed for higher levels
- totalStardustNeeded = 10 + (level - 1) * 5;
- for (var i = 0; i < totalStardustNeeded; i++) {
- var stardust = new Stardust();
- // Place stardust in a random position, but not too close to start or goal
- var validPosition = false;
- while (!validPosition) {
- stardust.x = 100 + Math.random() * 1848;
- stardust.y = 300 + Math.random() * 2132;
- // Make sure it's not too close to astronaut start position or space station
- var distToAstronaut = Math.sqrt(Math.pow(stardust.x - 1024, 2) + Math.pow(stardust.y - 2300, 2));
- var distToStation = Math.sqrt(Math.pow(stardust.x - 1024, 2) + Math.pow(stardust.y - 200, 2));
- if (distToAstronaut > 300 && distToStation > 400) {
- validPosition = true;
- }
- }
- stardusts.push(stardust);
- game.addChild(stardust);
- }
-}
-// Create asteroids based on level
-function createAsteroids() {
- var numAsteroids = Math.min(level * 2, 10); // More asteroids for higher levels, max 10
- for (var i = 0; i < numAsteroids; i++) {
- var asteroid = new Asteroid();
- asteroid.x = Math.random() * 2048;
- asteroid.y = Math.random() * 2732;
- // Make sure asteroids don't start too close to the astronaut
- var distToAstronaut = Math.sqrt(Math.pow(asteroid.x - 1024, 2) + Math.pow(asteroid.y - 2300, 2));
- if (distToAstronaut < 400) {
- asteroid.x = (asteroid.x + 1024) % 2048;
- asteroid.y = (asteroid.y + 1366) % 2732;
- }
- // Faster asteroids for higher levels
- var speedMultiplier = 1 + level * 0.2;
- asteroid.setup((Math.random() * 4 - 2) * speedMultiplier, (Math.random() * 4 - 2) * speedMultiplier, (Math.random() * 0.1 - 0.05) * speedMultiplier);
- asteroids.push(asteroid);
- game.addChild(asteroid);
- }
-}
-// Create power-ups
-function createPowerUps() {
- var powerupTypes = ["jump", "slow", "shield"];
- var numPowerups = Math.min(level, 3); // More power-ups for higher levels, max 3
- for (var i = 0; i < numPowerups; i++) {
- var powerup = new PowerUp();
- powerup.x = 200 + Math.random() * 1648;
- powerup.y = 400 + Math.random() * 1932;
- powerup.setType(powerupTypes[i % powerupTypes.length]);
- powerups.push(powerup);
- game.addChild(powerup);
- }
-}
-// Create UI elements
-function createUI() {
- // Score text
- scoreTxt = new Text2('Stardust: 0/' + totalStardustNeeded, {
- size: 80,
- fill: 0xFFD700
- });
- scoreTxt.anchor.set(0.5, 0);
- LK.gui.top.addChild(scoreTxt);
- // Level text
- levelTxt = new Text2('Level: ' + level, {
- size: 80,
- fill: 0xFFFFFF
- });
- levelTxt.anchor.set(0, 0);
- levelTxt.x = 120; // Keep away from top-left corner
- levelTxt.y = 80;
- LK.gui.topLeft.addChild(levelTxt);
- // Instructions text
- instructionsTxt = new Text2('Tap to make the astronaut jump off planets!', {
- size: 60,
- fill: 0xCCCCCC
- });
- instructionsTxt.anchor.set(0.5, 0);
- instructionsTxt.y = 100;
- LK.gui.top.addChild(instructionsTxt);
- // Hide instructions after 3 seconds
- LK.setTimeout(function () {
- tween(instructionsTxt, {
- alpha: 0
- }, {
- duration: 1000,
- easing: tween.easeOut
- });
- }, 3000);
-}
-// Update score display
-function updateScore() {
- scoreTxt.setText('Stardust: ' + stardustCollected + '/' + totalStardustNeeded);
-}
-// Apply power-up effects
-function applyPowerUp(type) {
- LK.getSound('powerup').play();
- switch (type) {
- case "jump":
- // Temporary boost to jump strength
- var originalJumpStrength = astronaut.jumpStrength;
- astronaut.jumpStrength *= 1.5;
- // Flash effect on astronaut
- LK.effects.flashObject(astronaut, 0x00FF00, 500);
- // Restore after 5 seconds
- LK.setTimeout(function () {
- astronaut.jumpStrength = originalJumpStrength;
- }, 5000);
- break;
- case "slow":
- // Slow down all asteroids
- asteroids.forEach(function (asteroid) {
- asteroid.speedX *= 0.5;
- asteroid.speedY *= 0.5;
- asteroid.rotationSpeed *= 0.5;
- });
- // Visual effect
- LK.effects.flashScreen(0x0000FF, 300);
- // Reset after 8 seconds
- LK.setTimeout(function () {
- asteroids.forEach(function (asteroid) {
- asteroid.speedX *= 2;
- asteroid.speedY *= 2;
- asteroid.rotationSpeed *= 2;
- });
- }, 8000);
- break;
- case "shield":
- // Give player shield for 10 seconds
- playerHasShield = true;
- // Visual effect - surround astronaut with a shield
- var shield = LK.getAsset('astronaut', {
- anchorX: 0.5,
- anchorY: 0.5,
- scaleX: 1.3,
- scaleY: 1.3,
- alpha: 0.5,
- tint: 0xFF0000
- });
- astronaut.addChild(shield);
- // Remove shield after 10 seconds
- LK.setTimeout(function () {
- playerHasShield = false;
- astronaut.removeChild(shield);
- }, 10000);
- break;
- }
-}
-// Check if astronaut can bounce off a planet
-function checkPlanetBounce() {
- astronaut.isOnPlanet = false;
- for (var i = 0; i < planets.length; i++) {
- var planet = planets[i];
- var dx = astronaut.x - planet.x;
- var dy = astronaut.y - planet.y;
- var distance = Math.sqrt(dx * dx + dy * dy);
- // Check if astronaut is touching planet surface
- if (distance <= planet.radius + 50) {
- // 50 is astronaut radius
- // Calculate bounce direction (normal to planet surface)
- var nx = dx / distance;
- var ny = dy / distance;
- // Project velocity onto normal
- var dot = astronaut.velocityX * nx + astronaut.velocityY * ny;
- // Only bounce if moving toward planet
- if (dot < 0) {
- // Reflect velocity around normal with dampening (bounce factor)
- astronaut.velocityX = (astronaut.velocityX - 2 * dot * nx) * astronaut.bounce;
- astronaut.velocityY = (astronaut.velocityY - 2 * dot * ny) * astronaut.bounce;
- // Position astronaut just outside planet to prevent sticking
- astronaut.x = planet.x + nx * (planet.radius + 50);
- astronaut.y = planet.y + ny * (planet.radius + 50);
- // Mark as on planet for jumping
- astronaut.isOnPlanet = true;
- // Play bounce sound
- LK.getSound('bounce').play();
- // Visual feedback
- LK.effects.flashObject(planet, 0xFFFFFF, 200);
- }
- }
- }
-}
-// Check collisions with stardust
-function checkStardustCollision() {
- for (var i = stardusts.length - 1; i >= 0; i--) {
- if (astronaut.intersects(stardusts[i])) {
- // Collect stardust
- stardustCollected++;
- updateScore();
- // Remove stardust
- game.removeChild(stardusts[i]);
- stardusts.splice(i, 1);
- // Play collect sound
- LK.getSound('collect').play();
- // Visual feedback
- LK.effects.flashObject(astronaut, 0xFFD700, 200);
- // Check if all stardust collected
- if (stardustCollected >= totalStardustNeeded) {
- // Make space station flash to show it's active
- tween(spaceStation, {
- alpha: 0.5
- }, {
- duration: 500,
- easing: tween.easeInOut,
- onFinish: function onFinish() {
- tween(spaceStation, {
- alpha: 1
- }, {
- duration: 500,
- easing: tween.easeInOut
- });
- }
- });
- }
- }
- }
-}
-// Check collisions with asteroids
-function checkAsteroidCollision() {
- for (var i = 0; i < asteroids.length; i++) {
- if (astronaut.intersects(asteroids[i])) {
- if (playerHasShield) {
- // Shield absorbs hit
- playerHasShield = false;
- // Remove shield visual
- for (var j = 0; j < astronaut.children.length; j++) {
- if (astronaut.children[j].alpha === 0.5) {
- astronaut.removeChild(astronaut.children[j]);
- break;
- }
- }
- // Visual feedback
- LK.effects.flashObject(astronaut, 0xFF0000, 500);
- // Push astronaut away
- astronaut.velocityX = asteroids[i].speedX * 3;
- astronaut.velocityY = asteroids[i].speedY * 3;
- } else {
- // Hit by asteroid - game over
- LK.getSound('hit').play();
- LK.effects.flashScreen(0xFF0000, 1000);
- gameActive = false;
- // Show game over after a brief delay
- LK.setTimeout(function () {
- LK.showGameOver();
- }, 1000);
- }
- }
- }
-}
-// Check collisions with power-ups
-function checkPowerUpCollision() {
- for (var i = powerups.length - 1; i >= 0; i--) {
- if (astronaut.intersects(powerups[i])) {
- var type = powerups[i].type;
- // Remove power-up
- game.removeChild(powerups[i]);
- powerups.splice(i, 1);
- // Apply power-up effect
- applyPowerUp(type);
- }
- }
-}
-// Check if level completed
-function checkLevelCompletion() {
- if (stardustCollected >= totalStardustNeeded && astronaut.intersects(spaceStation)) {
- // Complete level
- LK.getSound('win').play();
- // Visual feedback
- LK.effects.flashScreen(0x00FF00, 1000);
- // Level up
- level++;
- // Save progress
- storage.level = level;
- // Restart with next level
- LK.setTimeout(function () {
- if (level > 3) {
- // Player completed all 3 levels - win the game
- LK.showYouWin();
- } else {
- // Start next level
- initGame();
- }
- }, 1500);
- }
-}
-// Handle game input
-game.down = function (x, y, obj) {
- if (gameActive) {
- astronaut.jump();
- }
-};
-// Game update loop
+var astronaut = game.addChild(new Astronaut());
+var stardust = game.addChild(new Stardust());
+var asteroid = game.addChild(new Asteroid());
game.update = function () {
- if (!gameActive) {
- return;
- }
- // Update game objects
- if (astronaut) {
- // Check planet collisions and bouncing
- checkPlanetBounce();
- // Update astronaut physics
- astronaut.update();
- // Check collisions
- checkStardustCollision();
- checkAsteroidCollision();
- checkPowerUpCollision();
- // Check level completion
- checkLevelCompletion();
- }
- // Update all objects
- for (var i = 0; i < stardusts.length; i++) {
- stardusts[i].update();
- }
- for (var i = 0; i < asteroids.length; i++) {
- asteroids[i].update();
- }
- for (var i = 0; i < powerups.length; i++) {
- powerups[i].update();
- }
- if (spaceStation) {
- spaceStation.update();
- }
-};
-// Initialize the game
-initGame();
\ No newline at end of file
+ // Game logic goes here
+};
\ No newline at end of file