User prompt
Add a visible hunger bar ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
When the player enters the house, disable pig spawning and make the ground light brown, like house planks.
User prompt
Make it so that I can move the player in the house too
User prompt
Make it so I need to hold to move, not tap to move, and make the player faster ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Make it so that the player needs to be close to the house to enter it
User prompt
Add a player u can move by clicking the left right forward and backwards squares, the player is blue
Code edit (1 edits merged)
Please save this source code
User prompt
Add a player which you can move by clicking the left, right, forward, backwards arrows that show up
Code edit (1 edits merged)
Please save this source code
User prompt
Forest Survivor
Initial prompt
Make a 2d survival game about survving in a forest, you can shoot animals (pink pigs) by clicking them, add trees, when yoh double click a tree it builds a house, when u click on the house it gets u în it, double click on the house floor to get out, when you kill a pink pig he becomes smaller, when he îs dead u can click on him to eat him
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Bullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speedX = 0;
self.speedY = 0;
self.lifetime = 0;
self.update = function () {
self.x += self.speedX;
self.y += self.speedY;
self.lifetime++;
// Remove bullet after 2 seconds
if (self.lifetime > 120) {
self.destroy();
var index = bullets.indexOf(self);
if (index > -1) {
bullets.splice(index, 1);
}
}
};
return self;
});
var ControlButton = Container.expand(function (direction) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('controlButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.direction = direction;
// Add directional indicator
var arrow = new Text2(direction === 'left' ? '←' : direction === 'right' ? '→' : direction === 'up' ? '↑' : '↓', {
size: 60,
fill: 0xFFFFFF
});
arrow.anchor.set(0.5, 0.5);
self.addChild(arrow);
self.down = function (x, y, obj) {
// Set button state to held
if (direction === 'left') {
leftButtonHeld = true;
} else if (direction === 'right') {
rightButtonHeld = true;
} else if (direction === 'up') {
upButtonHeld = true;
} else if (direction === 'down') {
downButtonHeld = true;
}
// Visual feedback - button pressed
tween.stop(self, {
scaleX: true,
scaleY: true
});
tween(self, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
};
self.up = function (x, y, obj) {
// Set button state to not held
if (direction === 'left') {
leftButtonHeld = false;
} else if (direction === 'right') {
rightButtonHeld = false;
} else if (direction === 'up') {
upButtonHeld = false;
} else if (direction === 'down') {
downButtonHeld = false;
}
// Visual feedback - button released
tween.stop(self, {
scaleX: true,
scaleY: true
});
tween(self, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 100
});
};
return self;
});
var House = Container.expand(function () {
var self = Container.call(this);
var houseGraphics = self.attachAsset('house', {
anchorX: 0.5,
anchorY: 1.0
});
var floor = self.addChild(LK.getAsset('houseFloor', {
anchorX: 0.5,
anchorY: 0.5
}));
floor.x = 0;
floor.y = -10;
self.down = function (x, y, obj) {
if (!playerInHouse) {
// Check if player is close enough to the house
var dx = player.x - self.x;
var dy = player.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var maxDistance = 150; // Player must be within 150 pixels of the house
if (distance <= maxDistance) {
enterHouse();
}
}
};
return self;
});
var Pig = Container.expand(function () {
var self = Container.call(this);
var pigGraphics = self.attachAsset('pig', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 3;
self.isDead = false;
self.isEaten = false;
self.originalScale = 1.0;
self.takeDamage = function () {
if (self.isDead) {
return;
}
self.health--;
LK.getSound('pigHit').play();
// Shrink the pig
var newScale = self.originalScale * (self.health / 3);
tween(self, {
scaleX: newScale,
scaleY: newScale
}, {
duration: 200
});
// Flash effect
LK.effects.flashObject(self, 0xff0000, 300);
if (self.health <= 0) {
self.die();
}
};
self.die = function () {
self.isDead = true;
LK.getSound('pigDie').play();
tween(pigGraphics, {
tint: 0x666666
}, {
duration: 500
});
};
self.eat = function () {
if (!self.isDead || self.isEaten) {
return false;
}
self.isEaten = true;
playerFood += 20;
foodText.setText('Food: ' + playerFood);
tween(self, {
alpha: 0,
scaleX: 0.1,
scaleY: 0.1
}, {
duration: 500,
onFinish: function onFinish() {
self.destroy();
var index = pigs.indexOf(self);
if (index > -1) {
pigs.splice(index, 1);
}
}
});
return true;
};
self.down = function (x, y, obj) {
if (self.isDead && !self.isEaten) {
self.eat();
} else if (!self.isDead) {
shootAt(self.x, self.y);
self.takeDamage();
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 15;
self.moveLeft = function () {
self.x -= self.speed;
if (self.x < 30) self.x = 30;
};
self.moveRight = function () {
self.x += self.speed;
if (self.x > 2018) self.x = 2018;
};
self.moveUp = function () {
self.y -= self.speed;
if (self.y < 30) self.y = 30;
};
self.moveDown = function () {
self.y += self.speed;
if (self.y > 2702) self.y = 2702;
};
return self;
});
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.attachAsset('tree', {
anchorX: 0.5,
anchorY: 1.0
});
self.isBuilt = false;
self.lastClickTime = 0;
self.buildHouse = function () {
if (self.isBuilt) {
return;
}
self.isBuilt = true;
LK.getSound('buildHouse').play();
// Remove tree and add house
var house = new House();
house.x = self.x;
house.y = self.y;
game.addChild(house);
houses.push(house);
self.destroy();
var index = trees.indexOf(self);
if (index > -1) {
trees.splice(index, 1);
}
};
self.down = function (x, y, obj) {
var currentTime = Date.now();
if (currentTime - self.lastClickTime < 500) {
// Double click detected
self.buildHouse();
}
self.lastClickTime = currentTime;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2d5a2d
});
/****
* Game Code
****/
// Game variables
var pigs = [];
var trees = [];
var houses = [];
var bullets = [];
var playerFood = 100;
var playerInHouse = false;
var houseFloorClickTime = 0;
var player;
var controlButtons = [];
// Button state tracking for hold-to-move
var leftButtonHeld = false;
var rightButtonHeld = false;
var upButtonHeld = false;
var downButtonHeld = false;
// UI Elements
var foodText = new Text2('Food: ' + playerFood, {
size: 60,
fill: 0xFFFFFF
});
foodText.anchor.set(0, 0);
LK.gui.topRight.addChild(foodText);
var instructionText = new Text2('Click pigs to hunt\nDouble-click trees to build', {
size: 40,
fill: 0xFFFF00
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 2048 / 2;
instructionText.y = 50;
game.addChild(instructionText);
// Initialize forest
function spawnPig() {
var pig = new Pig();
pig.x = Math.random() * 1800 + 124;
pig.y = Math.random() * 2400 + 200;
game.addChild(pig);
pigs.push(pig);
}
function spawnTree() {
var tree = new Tree();
tree.x = Math.random() * 1800 + 124;
tree.y = Math.random() * 2400 + 300;
game.addChild(tree);
trees.push(tree);
}
// Spawn initial entities
for (var i = 0; i < 8; i++) {
spawnPig();
}
for (var i = 0; i < 15; i++) {
spawnTree();
}
// Initialize player
player = new Player();
player.x = 1024;
player.y = 1366;
game.addChild(player);
// Create control buttons
var leftButton = new ControlButton('left');
leftButton.x = 200;
leftButton.y = 2500;
game.addChild(leftButton);
controlButtons.push(leftButton);
var rightButton = new ControlButton('right');
rightButton.x = 400;
rightButton.y = 2500;
game.addChild(rightButton);
controlButtons.push(rightButton);
var upButton = new ControlButton('up');
upButton.x = 300;
upButton.y = 2400;
game.addChild(upButton);
controlButtons.push(upButton);
var downButton = new ControlButton('down');
downButton.x = 300;
downButton.y = 2600;
game.addChild(downButton);
controlButtons.push(downButton);
// Shooting system
function shootAt(targetX, targetY) {
var bullet = new Bullet();
bullet.x = player.x;
bullet.y = player.y;
var dx = targetX - bullet.x;
var dy = targetY - bullet.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var speed = 15;
bullet.speedX = dx / distance * speed;
bullet.speedY = dy / distance * speed;
game.addChild(bullet);
bullets.push(bullet);
LK.getSound('shoot').play();
}
// House system
function enterHouse() {
if (playerInHouse) {
return;
}
playerInHouse = true;
// Hide all game elements except houses
for (var i = 0; i < pigs.length; i++) {
pigs[i].visible = false;
}
for (var i = 0; i < trees.length; i++) {
trees[i].visible = false;
}
for (var i = 0; i < bullets.length; i++) {
bullets[i].visible = false;
}
player.visible = false;
for (var i = 0; i < controlButtons.length; i++) {
controlButtons[i].visible = false;
}
instructionText.setText('Double-click floor to exit house');
// Show house interior (just the floor for now)
var houseFloor = LK.getAsset('houseFloor', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 8,
scaleY: 20,
x: 1024,
y: 1366
});
game.addChild(houseFloor);
houseFloor.name = 'houseFloor';
}
function exitHouse() {
if (!playerInHouse) {
return;
}
playerInHouse = false;
// Show all game elements
for (var i = 0; i < pigs.length; i++) {
pigs[i].visible = true;
}
for (var i = 0; i < trees.length; i++) {
trees[i].visible = true;
}
for (var i = 0; i < bullets.length; i++) {
bullets[i].visible = true;
}
player.visible = true;
for (var i = 0; i < controlButtons.length; i++) {
controlButtons[i].visible = true;
}
instructionText.setText('Click pigs to hunt\nDouble-click trees to build');
// Remove house floor
var floor = game.children.find(function (child) {
return child.name === 'houseFloor';
});
if (floor) {
game.removeChild(floor);
}
}
// Game event handlers
game.down = function (x, y, obj) {
if (playerInHouse) {
var currentTime = Date.now();
if (currentTime - houseFloorClickTime < 500) {
exitHouse();
}
houseFloorClickTime = currentTime;
}
};
// Spawn timer
var spawnTimer = 0;
// Food depletion timer
var foodTimer = 0;
game.update = function () {
spawnTimer++;
foodTimer++;
// Handle continuous movement based on held buttons
if (!playerInHouse) {
if (leftButtonHeld) {
player.moveLeft();
}
if (rightButtonHeld) {
player.moveRight();
}
if (upButtonHeld) {
player.moveUp();
}
if (downButtonHeld) {
player.moveDown();
}
}
// Spawn new pigs occasionally
if (spawnTimer % 600 === 0 && pigs.length < 12) {
spawnPig();
}
// Deplete food over time
if (foodTimer % 180 === 0) {
playerFood--;
if (playerFood < 0) {
playerFood = 0;
}
foodText.setText('Food: ' + playerFood);
if (playerFood <= 0) {
LK.showGameOver();
}
}
// Clean up destroyed bullets
for (var i = bullets.length - 1; i >= 0; i--) {
if (!bullets[i].parent) {
bullets.splice(i, 1);
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -45,33 +45,53 @@
});
arrow.anchor.set(0.5, 0.5);
self.addChild(arrow);
self.down = function (x, y, obj) {
+ // Set button state to held
if (direction === 'left') {
- player.moveLeft();
+ leftButtonHeld = true;
} else if (direction === 'right') {
- player.moveRight();
+ rightButtonHeld = true;
} else if (direction === 'up') {
- player.moveUp();
+ upButtonHeld = true;
} else if (direction === 'down') {
- player.moveDown();
+ downButtonHeld = true;
}
- // Visual feedback
+ // Visual feedback - button pressed
+ tween.stop(self, {
+ scaleX: true,
+ scaleY: true
+ });
tween(self, {
scaleX: 0.9,
scaleY: 0.9
}, {
- duration: 100,
- onFinish: function onFinish() {
- tween(self, {
- scaleX: 1.0,
- scaleY: 1.0
- }, {
- duration: 100
- });
- }
+ duration: 100
});
};
+ self.up = function (x, y, obj) {
+ // Set button state to not held
+ if (direction === 'left') {
+ leftButtonHeld = false;
+ } else if (direction === 'right') {
+ rightButtonHeld = false;
+ } else if (direction === 'up') {
+ upButtonHeld = false;
+ } else if (direction === 'down') {
+ downButtonHeld = false;
+ }
+ // Visual feedback - button released
+ tween.stop(self, {
+ scaleX: true,
+ scaleY: true
+ });
+ tween(self, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 100
+ });
+ };
return self;
});
var House = Container.expand(function () {
var self = Container.call(this);
@@ -176,9 +196,9 @@
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
- self.speed = 8;
+ self.speed = 15;
self.moveLeft = function () {
self.x -= self.speed;
if (self.x < 30) self.x = 30;
};
@@ -252,8 +272,13 @@
var playerInHouse = false;
var houseFloorClickTime = 0;
var player;
var controlButtons = [];
+// Button state tracking for hold-to-move
+var leftButtonHeld = false;
+var rightButtonHeld = false;
+var upButtonHeld = false;
+var downButtonHeld = false;
// UI Elements
var foodText = new Text2('Food: ' + playerFood, {
size: 60,
fill: 0xFFFFFF
@@ -408,8 +433,23 @@
var foodTimer = 0;
game.update = function () {
spawnTimer++;
foodTimer++;
+ // Handle continuous movement based on held buttons
+ if (!playerInHouse) {
+ if (leftButtonHeld) {
+ player.moveLeft();
+ }
+ if (rightButtonHeld) {
+ player.moveRight();
+ }
+ if (upButtonHeld) {
+ player.moveUp();
+ }
+ if (downButtonHeld) {
+ player.moveDown();
+ }
+ }
// Spawn new pigs occasionally
if (spawnTimer % 600 === 0 && pigs.length < 12) {
spawnPig();
}
Make a house, with a wood door, the house is made of red wood and the door brown wood, the roof is made of wheat. 2d. High contrast. No shadows
Make a pig. In-Game asset. 2d. High contrast. No shadows
Make a lumberjack. In-Game asset. 2d. High contrast. No shadows
Make a tree. In-Game asset. 2d. High contrast. No shadows