User prompt
Add collision to the walls and furniture.
User prompt
Add more furniture for the bananas to hide.
User prompt
When the enemy touches you, make it an automatic kill.
User prompt
Make the map bigger.
User prompt
Make the player's speed the same speed as the enemy's.
User prompt
Make the players slower.
User prompt
Enemy chase you. When it touches you, make a game over scene.
User prompt
the number of bananas that you have to collect from 7 to 20.
User prompt
Let the camera move with the player.
User prompt
Make the map bigger.
User prompt
When the player touches the banana, collect it and add it to the server bananas.
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'if (obj.event.target === player) {' Line Number: 260
User prompt
Make the player move with your finger.
Code edit (1 edits merged)
Please save this source code
User prompt
Banana Hunt: Find & Collect
Initial prompt
Make a game that you have to find seven bananas and collect them and put them on a desk.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Banana = Container.expand(function () {
var self = Container.call(this);
var bananaGraphics = self.attachAsset('banana', {
anchorX: 0.5,
anchorY: 0.5,
rotation: Math.PI / 4
});
self.isCollected = false;
self.isDragging = false;
self.down = function (x, y, obj) {
if (!self.isCollected) {
self.isDragging = true;
LK.getSound('pickup').play();
}
};
self.up = function (x, y, obj) {
self.isDragging = false;
if (self.intersects(collectionDesk) && !self.isCollected) {
self.isCollected = true;
// Animate banana to center of desk
var deskGlobal = collectionDesk.toGlobal({
x: 0,
y: 0
});
var deskLocal = game.toLocal(deskGlobal);
tween(self, {
x: deskLocal.x + collectionDesk.width / 2,
y: deskLocal.y + 50 + collectedBananas * 30
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
collectedBananas++;
updateCollectionCounter();
LK.getSound('drop').play();
if (collectedBananas === totalBananas) {
LK.setTimeout(function () {
LK.getSound('success').play();
LK.showYouWin();
}, 1000);
}
}
});
}
};
return self;
});
var CollectionDesk = Container.expand(function () {
var self = Container.call(this);
var deskGraphics = self.attachAsset('desk', {
anchorX: 0.5,
anchorY: 0.5
});
var labelText = new Text2('Collection Desk', {
size: 40,
fill: 0xFFFFFF
});
labelText.anchor.set(0.5, 0.5);
labelText.position.set(0, -60);
self.addChild(labelText);
return self;
});
var FurniturePiece = Container.expand(function (assetName, width, height) {
var self = Container.call(this);
var furnitureGraphics = self.attachAsset(assetName, {
anchorX: 0.5,
anchorY: 0.5
});
self.width = width;
self.height = height;
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
// Create a simple player character using an existing shape
var playerGraphics = self.attachAsset('pillow', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.8,
scaleY: 0.8,
tint: 0x00AAFF // Blue tint to distinguish from other pillows
});
// Track if player is being dragged
self.isDragging = false;
self.down = function (x, y, obj) {
self.isDragging = true;
};
self.up = function (x, y, obj) {
self.isDragging = false;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xF5F5DC
});
/****
* Game Code
****/
// Game constants
var totalBananas = 7;
var collectedBananas = 0;
var bananas = [];
var draggedBanana = null;
var player = null;
// Initialize game position for camera system
game.x = 0;
game.y = 0;
// Add room background
var background = game.addChild(LK.getAsset('roomBackground', {
anchorX: 0,
anchorY: 0
}));
background.width = 4096; // Double the width
background.height = 5464; // Double the height
// Add furniture pieces
var furniturePieces = [];
// Bed
var bed = new FurniturePiece('bed', 600, 300);
bed.x = 3200;
bed.y = 1000;
game.addChild(bed);
furniturePieces.push(bed);
// Table
var table = new FurniturePiece('table', 300, 150);
table.x = 1000;
table.y = 2400;
game.addChild(table);
furniturePieces.push(table);
// Sofa
var sofa = new FurniturePiece('sofa', 500, 200);
sofa.x = 1000;
sofa.y = 3600;
game.addChild(sofa);
furniturePieces.push(sofa);
// Bookshelf
var bookshelf = new FurniturePiece('bookshelf', 300, 500);
bookshelf.x = 3400;
bookshelf.y = 2800;
game.addChild(bookshelf);
furniturePieces.push(bookshelf);
// Plant
var plant = new FurniturePiece('plant', 150, 200);
plant.x = 3000;
plant.y = 4000;
game.addChild(plant);
furniturePieces.push(plant);
// Lamp
var lamp = new FurniturePiece('lamp', 100, 250);
lamp.x = 600;
lamp.y = 1000;
game.addChild(lamp);
furniturePieces.push(lamp);
// Pillow
var pillow = new FurniturePiece('pillow', 150, 150);
pillow.x = 3400;
pillow.y = 1200;
game.addChild(pillow);
furniturePieces.push(pillow);
// Add collection desk
var collectionDesk = new CollectionDesk();
collectionDesk.x = 2048; // Center of screen
collectionDesk.y = 5000; // Bottom of screen
game.addChild(collectionDesk);
// Add collection counter
var collectionCounter = new Text2('0/' + totalBananas + ' bananas', {
size: 60,
fill: 0x000000
});
collectionCounter.anchor.set(0.5, 0);
LK.gui.top.addChild(collectionCounter);
function updateCollectionCounter() {
collectionCounter.setText(collectedBananas + "/" + totalBananas + " bananas");
}
updateCollectionCounter();
// Create and hide bananas around the room
function createBananas() {
var bananaPositions = [{
x: 3300,
y: 800
},
// Near the bed
{
x: 1100,
y: 2300
},
// On the table
{
x: 700,
y: 3600
},
// Behind the sofa
{
x: 3400,
y: 2400
},
// On bookshelf
{
x: 3100,
y: 4100
},
// Behind plant
{
x: 600,
y: 700
},
// Near lamp
{
x: 3500,
y: 1300
} // Under pillow
];
for (var i = 0; i < totalBananas; i++) {
var banana = new Banana();
banana.x = bananaPositions[i].x;
banana.y = bananaPositions[i].y;
// Add slight random rotation
banana.rotation = (Math.random() - 0.5) * 0.5;
bananas.push(banana);
game.addChild(banana);
}
}
createBananas();
// Add Math.lerp utility function for smooth interpolation
Math.lerp = function (start, end, amt) {
return (1 - amt) * start + amt * end;
};
// Create and add player character
player = new Player();
player.x = 2048; // Start at center of screen horizontally
player.y = 2732; // Middle of the screen vertically
game.addChild(player);
// Game event handlers
game.down = function (x, y, obj) {
// Check if player was clicked
if (obj.target === player) {
player.isDragging = true;
}
};
game.move = function (x, y, obj) {
// Move any dragged banana
for (var i = 0; i < bananas.length; i++) {
if (bananas[i].isDragging) {
bananas[i].x = x;
bananas[i].y = y;
}
}
// Move player if being dragged
if (player && player.isDragging) {
player.x = x;
player.y = y;
}
};
game.up = function (x, y, obj) {
// Handled by individual banana objects
if (player) {
player.isDragging = false;
}
};
// Update function to check for player-banana collisions and handle camera
game.update = function () {
// Update camera position to follow player
if (player) {
// Calculate camera target position
var cameraX = Math.max(1024, Math.min(background.width - 1024, player.x));
var cameraY = Math.max(1366, Math.min(background.height - 1366, player.y));
// Smoothly move the camera (game position) to follow the player
game.x = Math.lerp(game.x, -cameraX + 1024, 0.1);
game.y = Math.lerp(game.y, -cameraY + 1366, 0.1);
// Check if player touches any banana
for (var i = 0; i < bananas.length; i++) {
if (!bananas[i].isCollected && !bananas[i].isDragging && player.intersects(bananas[i])) {
// Collect the banana
bananas[i].isCollected = true;
// Animate banana to center of desk
var deskGlobal = collectionDesk.toGlobal({
x: 0,
y: 0
});
var deskLocal = game.toLocal(deskGlobal);
// Play pickup sound
LK.getSound('pickup').play();
tween(bananas[i], {
x: deskLocal.x + collectionDesk.width / 2,
y: deskLocal.y + 50 + collectedBananas * 30
}, {
duration: 500,
easing: tween.easeOut,
onFinish: function onFinish() {
collectedBananas++;
updateCollectionCounter();
LK.getSound('drop').play();
if (collectedBananas === totalBananas) {
LK.setTimeout(function () {
LK.getSound('success').play();
LK.showYouWin();
}, 1000);
}
}
});
}
}
}
};
// Play background music
LK.playMusic('bgmusic');
// Instructions
var instructionsText = new Text2("Find 7 hidden bananas and\ndrag them to the collection desk!", {
size: 50,
fill: 0x000000
});
instructionsText.anchor.set(0.5, 0);
instructionsText.y = 100;
LK.gui.top.addChild(instructionsText);
// Auto-hide instructions after 5 seconds
LK.setTimeout(function () {
tween(instructionsText, {
alpha: 0
}, {
duration: 1000,
easing: tween.easeOut
});
}, 5000); ===================================================================
--- original.js
+++ change.js
@@ -115,8 +115,11 @@
var collectedBananas = 0;
var bananas = [];
var draggedBanana = null;
var player = null;
+// Initialize game position for camera system
+game.x = 0;
+game.y = 0;
// Add room background
var background = game.addChild(LK.getAsset('roomBackground', {
anchorX: 0,
anchorY: 0
@@ -230,8 +233,12 @@
game.addChild(banana);
}
}
createBananas();
+// Add Math.lerp utility function for smooth interpolation
+Math.lerp = function (start, end, amt) {
+ return (1 - amt) * start + amt * end;
+};
// Create and add player character
player = new Player();
player.x = 2048; // Start at center of screen horizontally
player.y = 2732; // Middle of the screen vertically
@@ -262,12 +269,19 @@
if (player) {
player.isDragging = false;
}
};
-// Update function to check for player-banana collisions
+// Update function to check for player-banana collisions and handle camera
game.update = function () {
- // Check if player touches any banana
+ // Update camera position to follow player
if (player) {
+ // Calculate camera target position
+ var cameraX = Math.max(1024, Math.min(background.width - 1024, player.x));
+ var cameraY = Math.max(1366, Math.min(background.height - 1366, player.y));
+ // Smoothly move the camera (game position) to follow the player
+ game.x = Math.lerp(game.x, -cameraX + 1024, 0.1);
+ game.y = Math.lerp(game.y, -cameraY + 1366, 0.1);
+ // Check if player touches any banana
for (var i = 0; i < bananas.length; i++) {
if (!bananas[i].isCollected && !bananas[i].isDragging && player.intersects(bananas[i])) {
// Collect the banana
bananas[i].isCollected = true;