User prompt
quiero que la barra este ubicada a la derecha de forma vertical
User prompt
ahora quiero que tambien hayan objetos sean como la gasolina por asi decirlo, que cuando lo agarre se incremente su barra de gasolina o energia, y si la barra se acaba, pierde el juego
User prompt
entonces que la nave siga el cursor del mouse
Code edit (1 edits merged)
Please save this source code
User prompt
Space Ascender
Initial prompt
quiero hacer como que una nave va acendiendo por el espacio mientras esquiva rocas
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Asteroid = Container.expand(function () {
var self = Container.call(this);
var asteroidGraphics = self.attachAsset('asteroid', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3 + Math.random() * 4;
self.rotationSpeed = (Math.random() - 0.5) * 0.1;
self.update = function () {
self.y += self.speed;
asteroidGraphics.rotation += self.rotationSpeed;
};
return self;
});
var Debris = Container.expand(function () {
var self = Container.call(this);
var debrisGraphics = self.attachAsset('debris', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2 + Math.random() * 3;
self.horizontalSpeed = (Math.random() - 0.5) * 2;
self.update = function () {
self.y += self.speed;
self.x += self.horizontalSpeed;
};
return self;
});
var FuelPickup = Container.expand(function () {
var self = Container.call(this);
var fuelGraphics = self.attachAsset('fuel', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2 + Math.random() * 2;
self.rotationSpeed = 0.05;
self.update = function () {
self.y += self.speed;
fuelGraphics.rotation += self.rotationSpeed;
};
return self;
});
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 0;
self.maxSpeed = 8;
self.acceleration = 0.3;
self.friction = 0.85;
self.targetX = 1024; // Default center position
self.update = function () {
// Smoothly move towards target position
var deltaX = self.targetX - self.x;
self.x += deltaX * 0.1; // Smooth interpolation factor
// Keep ship within bounds
if (self.x < 40) {
self.x = 40;
}
if (self.x > 2008) {
self.x = 2008;
}
};
self.moveLeft = function () {
self.speed = Math.max(self.speed - self.acceleration, -self.maxSpeed);
};
self.moveRight = function () {
self.speed = Math.min(self.speed + self.acceleration, self.maxSpeed);
};
self.setTargetX = function (x) {
self.targetX = x;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000011
});
/****
* Game Code
****/
var spaceship = new Spaceship();
var obstacles = [];
var fuelPickups = [];
var scrollSpeed = 2;
var spawnTimer = 0;
var spawnRate = 120; // frames between spawns
var fuelSpawnTimer = 0;
var fuelSpawnRate = 300; // frames between fuel spawns
var gameSpeed = 1;
var altitude = 0;
var lastTouchX = null;
var isDragging = false;
var maxFuel = 100;
var currentFuel = 100;
var fuelConsumptionRate = 0.2; // fuel consumed per frame
// Position spaceship
spaceship.x = 1024;
spaceship.y = 2400;
game.addChild(spaceship);
// Create score display
var scoreText = new Text2('Altitude: 0m', {
size: 60,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Create altitude display
var altitudeText = new Text2('Score: 0', {
size: 50,
fill: 0xFFFFFF
});
altitudeText.anchor.set(0.5, 0);
altitudeText.y = 80;
LK.gui.top.addChild(altitudeText);
// Create fuel bar background
var fuelBarBg = LK.getAsset('fuel', {
width: 300,
height: 20,
scaleX: 6,
scaleY: 0.4,
anchorX: 0,
anchorY: 0
});
fuelBarBg.tint = 0x333333;
fuelBarBg.x = 100;
fuelBarBg.y = 140;
LK.gui.top.addChild(fuelBarBg);
// Create fuel bar
var fuelBar = LK.getAsset('fuel', {
width: 300,
height: 20,
scaleX: 6,
scaleY: 0.4,
anchorX: 0,
anchorY: 0
});
fuelBar.x = 100;
fuelBar.y = 140;
LK.gui.top.addChild(fuelBar);
// Create fuel text
var fuelText = new Text2('Fuel: 100%', {
size: 40,
fill: 0xFFFFFF
});
fuelText.anchor.set(0, 0);
fuelText.x = 100;
fuelText.y = 170;
LK.gui.top.addChild(fuelText);
function createObstacle() {
var obstacle;
if (Math.random() < 0.7) {
obstacle = new Asteroid();
} else {
obstacle = new Debris();
}
obstacle.x = Math.random() * (2048 - 100) + 50;
obstacle.y = -50;
obstacles.push(obstacle);
game.addChild(obstacle);
}
function createFuelPickup() {
var fuel = new FuelPickup();
fuel.x = Math.random() * (2048 - 100) + 50;
fuel.y = -50;
fuelPickups.push(fuel);
game.addChild(fuel);
}
function handleTouch(x, y) {
// Directly set spaceship target to cursor position
spaceship.setTargetX(x);
}
game.down = function (x, y, obj) {
isDragging = true;
lastTouchX = x;
handleTouch(x, y);
};
game.move = function (x, y, obj) {
// Always follow cursor, not just when dragging
handleTouch(x, y);
};
game.up = function (x, y, obj) {
isDragging = false;
lastTouchX = null;
};
game.update = function () {
// Consume fuel
currentFuel -= fuelConsumptionRate;
currentFuel = Math.max(0, currentFuel);
// Check if fuel is depleted
if (currentFuel <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
return;
}
// Update fuel bar
var fuelPercentage = currentFuel / maxFuel;
fuelBar.scaleX = 6 * fuelPercentage;
fuelText.setText('Fuel: ' + Math.floor(fuelPercentage * 100) + '%');
// Update altitude and score
altitude += scrollSpeed;
LK.setScore(Math.floor(altitude / 10));
// Update displays
scoreText.setText('Altitude: ' + Math.floor(altitude) + 'm');
altitudeText.setText('Score: ' + LK.getScore());
// Increase difficulty over time
if (LK.ticks % 600 === 0) {
// Every 10 seconds
gameSpeed += 0.1;
scrollSpeed = Math.min(scrollSpeed + 0.2, 6);
spawnRate = Math.max(spawnRate - 5, 30);
}
// Spawn obstacles
spawnTimer++;
if (spawnTimer >= spawnRate) {
createObstacle();
spawnTimer = 0;
}
// Spawn fuel pickups
fuelSpawnTimer++;
if (fuelSpawnTimer >= fuelSpawnRate) {
createFuelPickup();
fuelSpawnTimer = 0;
}
// Update fuel pickups and check collection
for (var j = fuelPickups.length - 1; j >= 0; j--) {
var fuelPickup = fuelPickups[j];
// Track last position for cleanup
if (fuelPickup.lastY === undefined) fuelPickup.lastY = fuelPickup.y;
// Remove fuel pickups that are off screen
if (fuelPickup.lastY < 2800 && fuelPickup.y >= 2800) {
fuelPickup.destroy();
fuelPickups.splice(j, 1);
continue;
}
// Check collection with spaceship
if (fuelPickup.intersects(spaceship)) {
// Add fuel
currentFuel = Math.min(currentFuel + 25, maxFuel);
LK.getSound('fuel_pickup').play();
fuelPickup.destroy();
fuelPickups.splice(j, 1);
continue;
}
fuelPickup.lastY = fuelPickup.y;
}
// Update obstacles and check collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
// Track last position for cleanup
if (obstacle.lastY === undefined) obstacle.lastY = obstacle.y;
// Remove obstacles that are off screen
if (obstacle.lastY < 2800 && obstacle.y >= 2800) {
obstacle.destroy();
obstacles.splice(i, 1);
continue;
}
// Check collision with spaceship
if (obstacle.intersects(spaceship)) {
// Flash screen red and show game over
LK.effects.flashScreen(0xff0000, 1000);
LK.getSound('explosion').play();
LK.showGameOver();
return;
}
obstacle.lastY = obstacle.y;
}
// Create star field effect by occasionally spawning small debris
if (LK.ticks % 30 === 0) {
var star = new Debris();
star.x = Math.random() * 2048;
star.y = -10;
star.speed = 1 + Math.random() * 2;
star.horizontalSpeed = 0;
// Make it smaller and whiter
star.scaleX = 0.3;
star.scaleY = 0.3;
star.alpha = 0.6;
obstacles.push(star);
game.addChild(star);
}
};
// Start background music
LK.playMusic('space_music'); ===================================================================
--- original.js
+++ change.js
@@ -33,8 +33,22 @@
self.x += self.horizontalSpeed;
};
return self;
});
+var FuelPickup = Container.expand(function () {
+ var self = Container.call(this);
+ var fuelGraphics = self.attachAsset('fuel', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 2 + Math.random() * 2;
+ self.rotationSpeed = 0.05;
+ self.update = function () {
+ self.y += self.speed;
+ fuelGraphics.rotation += self.rotationSpeed;
+ };
+ return self;
+});
var Spaceship = Container.expand(function () {
var self = Container.call(this);
var shipGraphics = self.attachAsset('spaceship', {
anchorX: 0.5,
@@ -80,15 +94,21 @@
* Game Code
****/
var spaceship = new Spaceship();
var obstacles = [];
+var fuelPickups = [];
var scrollSpeed = 2;
var spawnTimer = 0;
var spawnRate = 120; // frames between spawns
+var fuelSpawnTimer = 0;
+var fuelSpawnRate = 300; // frames between fuel spawns
var gameSpeed = 1;
var altitude = 0;
var lastTouchX = null;
var isDragging = false;
+var maxFuel = 100;
+var currentFuel = 100;
+var fuelConsumptionRate = 0.2; // fuel consumed per frame
// Position spaceship
spaceship.x = 1024;
spaceship.y = 2400;
game.addChild(spaceship);
@@ -106,8 +126,42 @@
});
altitudeText.anchor.set(0.5, 0);
altitudeText.y = 80;
LK.gui.top.addChild(altitudeText);
+// Create fuel bar background
+var fuelBarBg = LK.getAsset('fuel', {
+ width: 300,
+ height: 20,
+ scaleX: 6,
+ scaleY: 0.4,
+ anchorX: 0,
+ anchorY: 0
+});
+fuelBarBg.tint = 0x333333;
+fuelBarBg.x = 100;
+fuelBarBg.y = 140;
+LK.gui.top.addChild(fuelBarBg);
+// Create fuel bar
+var fuelBar = LK.getAsset('fuel', {
+ width: 300,
+ height: 20,
+ scaleX: 6,
+ scaleY: 0.4,
+ anchorX: 0,
+ anchorY: 0
+});
+fuelBar.x = 100;
+fuelBar.y = 140;
+LK.gui.top.addChild(fuelBar);
+// Create fuel text
+var fuelText = new Text2('Fuel: 100%', {
+ size: 40,
+ fill: 0xFFFFFF
+});
+fuelText.anchor.set(0, 0);
+fuelText.x = 100;
+fuelText.y = 170;
+LK.gui.top.addChild(fuelText);
function createObstacle() {
var obstacle;
if (Math.random() < 0.7) {
obstacle = new Asteroid();
@@ -118,8 +172,15 @@
obstacle.y = -50;
obstacles.push(obstacle);
game.addChild(obstacle);
}
+function createFuelPickup() {
+ var fuel = new FuelPickup();
+ fuel.x = Math.random() * (2048 - 100) + 50;
+ fuel.y = -50;
+ fuelPickups.push(fuel);
+ game.addChild(fuel);
+}
function handleTouch(x, y) {
// Directly set spaceship target to cursor position
spaceship.setTargetX(x);
}
@@ -136,8 +197,21 @@
isDragging = false;
lastTouchX = null;
};
game.update = function () {
+ // Consume fuel
+ currentFuel -= fuelConsumptionRate;
+ currentFuel = Math.max(0, currentFuel);
+ // Check if fuel is depleted
+ if (currentFuel <= 0) {
+ LK.effects.flashScreen(0xff0000, 1000);
+ LK.showGameOver();
+ return;
+ }
+ // Update fuel bar
+ var fuelPercentage = currentFuel / maxFuel;
+ fuelBar.scaleX = 6 * fuelPercentage;
+ fuelText.setText('Fuel: ' + Math.floor(fuelPercentage * 100) + '%');
// Update altitude and score
altitude += scrollSpeed;
LK.setScore(Math.floor(altitude / 10));
// Update displays
@@ -155,8 +229,36 @@
if (spawnTimer >= spawnRate) {
createObstacle();
spawnTimer = 0;
}
+ // Spawn fuel pickups
+ fuelSpawnTimer++;
+ if (fuelSpawnTimer >= fuelSpawnRate) {
+ createFuelPickup();
+ fuelSpawnTimer = 0;
+ }
+ // Update fuel pickups and check collection
+ for (var j = fuelPickups.length - 1; j >= 0; j--) {
+ var fuelPickup = fuelPickups[j];
+ // Track last position for cleanup
+ if (fuelPickup.lastY === undefined) fuelPickup.lastY = fuelPickup.y;
+ // Remove fuel pickups that are off screen
+ if (fuelPickup.lastY < 2800 && fuelPickup.y >= 2800) {
+ fuelPickup.destroy();
+ fuelPickups.splice(j, 1);
+ continue;
+ }
+ // Check collection with spaceship
+ if (fuelPickup.intersects(spaceship)) {
+ // Add fuel
+ currentFuel = Math.min(currentFuel + 25, maxFuel);
+ LK.getSound('fuel_pickup').play();
+ fuelPickup.destroy();
+ fuelPickups.splice(j, 1);
+ continue;
+ }
+ fuelPickup.lastY = fuelPickup.y;
+ }
// Update obstacles and check collisions
for (var i = obstacles.length - 1; i >= 0; i--) {
var obstacle = obstacles[i];
// Track last position for cleanup