User prompt
ahora haz una interfaz debajo del cuadrado, en el que indique una barra de vida
User prompt
cambia el color del fondo a negro
User prompt
bien, ahora haz que el jugador este encerrado en un cuadrado, el jugador al chocarse con alguno de los muros del cuadrado, frenar al jugador y evitar que avanze mas alla
Code edit (1 edits merged)
Please save this source code
User prompt
Mouse Follower
Initial prompt
crea a un jugador el cual siga constante y permanentemente el mouse
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 0.5
});
// Target position for smooth movement
self.targetX = 0;
self.targetY = 0;
// Movement speed factor for smooth following
self.followSpeed = 0.15;
// Square boundary limits (centered square area)
var squareSize = 1200;
var centerX = 2048 / 2;
var centerY = 2732 / 2;
self.boundaryLeft = centerX - squareSize / 2;
self.boundaryRight = centerX + squareSize / 2;
self.boundaryTop = centerY - squareSize / 2;
self.boundaryBottom = centerY + squareSize / 2;
var characterRadius = 60;
self.update = function () {
// Calculate next position
var deltaX = self.targetX - self.x;
var deltaY = self.targetY - self.y;
var nextX = self.x + deltaX * self.followSpeed;
var nextY = self.y + deltaY * self.followSpeed;
// Check wall collisions and prevent movement beyond boundaries
if (nextX - characterRadius < self.boundaryLeft) {
nextX = self.boundaryLeft + characterRadius;
}
if (nextX + characterRadius > self.boundaryRight) {
nextX = self.boundaryRight - characterRadius;
}
if (nextY - characterRadius < self.boundaryTop) {
nextY = self.boundaryTop + characterRadius;
}
if (nextY + characterRadius > self.boundaryBottom) {
nextY = self.boundaryBottom - characterRadius;
}
// Apply the constrained position
self.x = nextX;
self.y = nextY;
};
// Method to set target position
self.setTarget = function (x, y) {
self.targetX = x;
self.targetY = y;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Create visual boundary walls
var squareSize = 1200;
var centerX = 2048 / 2;
var centerY = 2732 / 2;
var boundaryLeft = centerX - squareSize / 2;
var boundaryRight = centerX + squareSize / 2;
var boundaryTop = centerY - squareSize / 2;
var boundaryBottom = centerY + squareSize / 2;
// Left wall
var leftWall = game.addChild(LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
}));
leftWall.x = boundaryLeft;
leftWall.y = centerY;
// Right wall
var rightWall = game.addChild(LK.getAsset('wall', {
anchorX: 0.5,
anchorY: 0.5
}));
rightWall.x = boundaryRight;
rightWall.y = centerY;
// Top wall
var topWall = game.addChild(LK.getAsset('wallHorizontal', {
anchorX: 0.5,
anchorY: 0.5
}));
topWall.x = centerX;
topWall.y = boundaryTop;
// Bottom wall
var bottomWall = game.addChild(LK.getAsset('wallHorizontal', {
anchorX: 0.5,
anchorY: 0.5
}));
bottomWall.x = centerX;
bottomWall.y = boundaryBottom;
// Create the character at center of screen
var character = game.addChild(new Character());
character.x = 2048 / 2;
character.y = 2732 / 2;
// Set initial target position to character's starting position
character.setTarget(character.x, character.y);
// Handle mouse/touch movement
game.move = function (x, y, obj) {
// Set character target to mouse position
character.setTarget(x, y);
};
// Handle mouse/touch down - also update position immediately
game.down = function (x, y, obj) {
// Set character target to touch/click position
character.setTarget(x, y);
};
// Create health bar interface below the square
var healthBarY = boundaryBottom + 100;
// Health bar background
var healthBarBackground = game.addChild(LK.getAsset('healthBarBg', {
anchorX: 0.5,
anchorY: 0.5
}));
healthBarBackground.x = centerX;
healthBarBackground.y = healthBarY;
// Health bar fill
var healthBarFill = game.addChild(LK.getAsset('healthBarFill', {
anchorX: 0,
anchorY: 0.5
}));
healthBarFill.x = centerX - 190; // Center minus half of fill width
healthBarFill.y = healthBarY;
// Health bar label
var healthLabel = new Text2('VIDA', {
size: 30,
fill: 0xFFFFFF
});
healthLabel.anchor.set(0.5, 1);
healthLabel.x = centerX;
healthLabel.y = healthBarY - 30;
game.addChild(healthLabel);
// Health variables
var maxHealth = 100;
var currentHealth = 100;
// Function to update health bar
function updateHealthBar() {
var healthPercentage = currentHealth / maxHealth;
healthBarFill.scaleX = healthPercentage;
// Change color based on health level
if (healthPercentage > 0.6) {
healthBarFill.tint = 0x00ff00; // Green
} else if (healthPercentage > 0.3) {
healthBarFill.tint = 0xffff00; // Yellow
} else {
healthBarFill.tint = 0xff0000; // Red
}
}
// Initial health bar update
updateHealthBar();
// Main game update loop
game.update = function () {
// Character update is called automatically by the engine
// No additional logic needed for basic mouse following
}; ===================================================================
--- original.js
+++ change.js
@@ -118,8 +118,51 @@
game.down = function (x, y, obj) {
// Set character target to touch/click position
character.setTarget(x, y);
};
+// Create health bar interface below the square
+var healthBarY = boundaryBottom + 100;
+// Health bar background
+var healthBarBackground = game.addChild(LK.getAsset('healthBarBg', {
+ anchorX: 0.5,
+ anchorY: 0.5
+}));
+healthBarBackground.x = centerX;
+healthBarBackground.y = healthBarY;
+// Health bar fill
+var healthBarFill = game.addChild(LK.getAsset('healthBarFill', {
+ anchorX: 0,
+ anchorY: 0.5
+}));
+healthBarFill.x = centerX - 190; // Center minus half of fill width
+healthBarFill.y = healthBarY;
+// Health bar label
+var healthLabel = new Text2('VIDA', {
+ size: 30,
+ fill: 0xFFFFFF
+});
+healthLabel.anchor.set(0.5, 1);
+healthLabel.x = centerX;
+healthLabel.y = healthBarY - 30;
+game.addChild(healthLabel);
+// Health variables
+var maxHealth = 100;
+var currentHealth = 100;
+// Function to update health bar
+function updateHealthBar() {
+ var healthPercentage = currentHealth / maxHealth;
+ healthBarFill.scaleX = healthPercentage;
+ // Change color based on health level
+ if (healthPercentage > 0.6) {
+ healthBarFill.tint = 0x00ff00; // Green
+ } else if (healthPercentage > 0.3) {
+ healthBarFill.tint = 0xffff00; // Yellow
+ } else {
+ healthBarFill.tint = 0xff0000; // Red
+ }
+}
+// Initial health bar update
+updateHealthBar();
// Main game update loop
game.update = function () {
// Character update is called automatically by the engine
// No additional logic needed for basic mouse following