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 Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('pillow', { anchorX: 0.5, anchorY: 0.5, scaleX: 0.8, scaleY: 0.8, tint: 0xFF0000 // Red tint for enemy }); // Speed of enemy chasing player self.speed = 2; self.lastPosition = { x: 0, y: 0 }; self.lastIntersecting = false; self.update = function () { // Save last position for intersection detection self.lastPosition.x = self.x; self.lastPosition.y = self.y; if (player) { // Calculate direction to player var dx = player.x - self.x; var dy = player.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); // Normalize and apply speed if (distance > 0) { dx = dx / distance * self.speed; dy = dy / distance * self.speed; // Store potential new position var newX = self.x + dx; var newY = self.y + dy; // Check collision with map boundaries var enemyRadius = self.width * 0.4; var isColliding = false; // Map boundary collision if (newX < enemyRadius || newX > background.width - enemyRadius || newY < enemyRadius || newY > background.height - enemyRadius) { isColliding = true; } // Furniture collision if (!isColliding) { for (var i = 0; i < furniturePieces.length; i++) { var furniture = furniturePieces[i]; // Calculate distance between centers var fdx = newX - furniture.x; var fdy = newY - furniture.y; var minDistX = enemyRadius + furniture.width / 2; var minDistY = enemyRadius + furniture.height / 2; // Check rectangular collision if (Math.abs(fdx) < minDistX && Math.abs(fdy) < minDistY) { isColliding = true; break; } } } // Only move if no collision if (!isColliding) { // Move towards player self.x = newX; self.y = newY; } } } }; 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 = 20; var collectedBananas = 0; var bananas = []; var draggedBanana = null; var player = null; var enemy = 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 = 6144; // Triple the width background.height = 8196; // Triple the height // Add furniture pieces var furniturePieces = []; // Bed var bed = new FurniturePiece('bed', 600, 300); bed.x = 5000; bed.y = 1500; game.addChild(bed); furniturePieces.push(bed); // Table var table = new FurniturePiece('table', 300, 150); table.x = 1500; table.y = 3800; game.addChild(table); furniturePieces.push(table); // Sofa var sofa = new FurniturePiece('sofa', 500, 200); sofa.x = 1800; sofa.y = 5600; game.addChild(sofa); furniturePieces.push(sofa); // Bookshelf var bookshelf = new FurniturePiece('bookshelf', 300, 500); bookshelf.x = 5200; bookshelf.y = 4500; game.addChild(bookshelf); furniturePieces.push(bookshelf); // Plant var plant = new FurniturePiece('plant', 150, 200); plant.x = 4600; plant.y = 6200; game.addChild(plant); furniturePieces.push(plant); // Lamp var lamp = new FurniturePiece('lamp', 100, 250); lamp.x = 1000; lamp.y = 1800; game.addChild(lamp); furniturePieces.push(lamp); // Pillow var pillow = new FurniturePiece('pillow', 150, 150); pillow.x = 5400; pillow.y = 2000; game.addChild(pillow); furniturePieces.push(pillow); // Additional furniture // Second bookshelf var bookshelf2 = new FurniturePiece('bookshelf', 300, 500); bookshelf2.x = 800; bookshelf2.y = 4000; game.addChild(bookshelf2); furniturePieces.push(bookshelf2); // Second desk var desk2 = new FurniturePiece('desk', 400, 200); desk2.x = 3500; desk2.y = 2200; game.addChild(desk2); furniturePieces.push(desk2); // Second plant var plant2 = new FurniturePiece('plant', 150, 200); plant2.x = 2600; plant2.y = 1400; game.addChild(plant2); furniturePieces.push(plant2); // Second lamp var lamp2 = new FurniturePiece('lamp', 100, 250); lamp2.x = 4800; lamp2.y = 3300; game.addChild(lamp2); furniturePieces.push(lamp2); // Second bed var bed2 = new FurniturePiece('bed', 600, 300); bed2.x = 3000; bed2.y = 6000; game.addChild(bed2); furniturePieces.push(bed2); // Armchair (using sofa asset with different size) var armchair = new FurniturePiece('sofa', 300, 200); armchair.x = 4200; armchair.y = 1800; game.addChild(armchair); furniturePieces.push(armchair); // Coffee table (using table with different dimensions) var coffeeTable = new FurniturePiece('table', 250, 120); coffeeTable.x = 2300; coffeeTable.y = 2800; game.addChild(coffeeTable); furniturePieces.push(coffeeTable); // Add collection desk var collectionDesk = new CollectionDesk(); collectionDesk.x = 3072; // Center of larger map collectionDesk.y = 7800; // Bottom of larger 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: 5100, y: 1300 }, // Near the bed { x: 1600, y: 3700 }, // On the table { x: 1400, y: 5500 }, // Behind the sofa { x: 5300, y: 4200 }, // On bookshelf { x: 4700, y: 6100 }, // Behind plant { x: 900, y: 1700 }, // Near lamp { x: 5500, y: 2100 }, // Under pillow // Additional banana positions { x: 3800, y: 1400 }, // Near top center { x: 1200, y: 3200 }, // Left middle area { x: 2300, y: 4700 }, // Middle area { x: 4300, y: 5500 }, // Right middle area { x: 800, y: 7000 }, // Bottom left corner { x: 2700, y: 7500 }, // Near collection desk { x: 4500, y: 7200 }, // Bottom right area { x: 5800, y: 2800 }, // Far right middle { x: 3400, y: 2600 }, // Upper middle { x: 1900, y: 1900 }, // Upper left { x: 4000, y: 4100 }, // Dead center { x: 5700, y: 5000 }, // Lower right { x: 600, y: 5000 } // Lower left ]; 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 = 3072; // Start at center of larger map horizontally player.y = 4098; // Middle of the larger map vertically game.addChild(player); // Create and add enemy character enemy = new Enemy(); enemy.x = 800; // Start at left side of screen enemy.y = 800; // Top portion of screen game.addChild(enemy); // 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) { // Direct movement - same speed as enemy 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 () { // Keep track of player's previous position to restore on collision if (!player.lastX) player.lastX = player.x; if (!player.lastY) player.lastY = player.y; // Check wall boundaries var playerRadius = player.width * 0.4; // Assuming player size var isColliding = false; // Collision with map boundaries if (player.x < playerRadius || player.x > background.width - playerRadius || player.y < playerRadius || player.y > background.height - playerRadius) { isColliding = true; } // Collision with furniture for (var i = 0; i < furniturePieces.length; i++) { var furniture = furniturePieces[i]; // Calculate distance between centers var dx = player.x - furniture.x; var dy = player.y - furniture.y; var minDistX = playerRadius + furniture.width / 2; var minDistY = playerRadius + furniture.height / 2; // Check rectangular collision if (Math.abs(dx) < minDistX && Math.abs(dy) < minDistY) { isColliding = true; break; } } // If collision detected, restore previous position if (isColliding) { player.x = player.lastX; player.y = player.lastY; } else { // Save current valid position player.lastX = player.x; player.lastY = player.y; } // 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); // Update enemy if (enemy) { // Save last intersection status enemy.lastIntersecting = enemy.intersects(player); // Update enemy position (chase player) enemy.update(); // Check if enemy touches player (automatic kill) if (enemy.intersects(player)) { // Flash screen red for game over effect LK.effects.flashScreen(0xFF0000, 1000); // Show game over LK.showGameOver(); } } // 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 20 hidden bananas and\ndrag them to the collection desk!\nAvoid the red enemy!", { 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);
/****
* 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 Enemy = Container.expand(function () {
var self = Container.call(this);
var enemyGraphics = self.attachAsset('pillow', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.8,
scaleY: 0.8,
tint: 0xFF0000 // Red tint for enemy
});
// Speed of enemy chasing player
self.speed = 2;
self.lastPosition = {
x: 0,
y: 0
};
self.lastIntersecting = false;
self.update = function () {
// Save last position for intersection detection
self.lastPosition.x = self.x;
self.lastPosition.y = self.y;
if (player) {
// Calculate direction to player
var dx = player.x - self.x;
var dy = player.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Normalize and apply speed
if (distance > 0) {
dx = dx / distance * self.speed;
dy = dy / distance * self.speed;
// Store potential new position
var newX = self.x + dx;
var newY = self.y + dy;
// Check collision with map boundaries
var enemyRadius = self.width * 0.4;
var isColliding = false;
// Map boundary collision
if (newX < enemyRadius || newX > background.width - enemyRadius || newY < enemyRadius || newY > background.height - enemyRadius) {
isColliding = true;
}
// Furniture collision
if (!isColliding) {
for (var i = 0; i < furniturePieces.length; i++) {
var furniture = furniturePieces[i];
// Calculate distance between centers
var fdx = newX - furniture.x;
var fdy = newY - furniture.y;
var minDistX = enemyRadius + furniture.width / 2;
var minDistY = enemyRadius + furniture.height / 2;
// Check rectangular collision
if (Math.abs(fdx) < minDistX && Math.abs(fdy) < minDistY) {
isColliding = true;
break;
}
}
}
// Only move if no collision
if (!isColliding) {
// Move towards player
self.x = newX;
self.y = newY;
}
}
}
};
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 = 20;
var collectedBananas = 0;
var bananas = [];
var draggedBanana = null;
var player = null;
var enemy = 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 = 6144; // Triple the width
background.height = 8196; // Triple the height
// Add furniture pieces
var furniturePieces = [];
// Bed
var bed = new FurniturePiece('bed', 600, 300);
bed.x = 5000;
bed.y = 1500;
game.addChild(bed);
furniturePieces.push(bed);
// Table
var table = new FurniturePiece('table', 300, 150);
table.x = 1500;
table.y = 3800;
game.addChild(table);
furniturePieces.push(table);
// Sofa
var sofa = new FurniturePiece('sofa', 500, 200);
sofa.x = 1800;
sofa.y = 5600;
game.addChild(sofa);
furniturePieces.push(sofa);
// Bookshelf
var bookshelf = new FurniturePiece('bookshelf', 300, 500);
bookshelf.x = 5200;
bookshelf.y = 4500;
game.addChild(bookshelf);
furniturePieces.push(bookshelf);
// Plant
var plant = new FurniturePiece('plant', 150, 200);
plant.x = 4600;
plant.y = 6200;
game.addChild(plant);
furniturePieces.push(plant);
// Lamp
var lamp = new FurniturePiece('lamp', 100, 250);
lamp.x = 1000;
lamp.y = 1800;
game.addChild(lamp);
furniturePieces.push(lamp);
// Pillow
var pillow = new FurniturePiece('pillow', 150, 150);
pillow.x = 5400;
pillow.y = 2000;
game.addChild(pillow);
furniturePieces.push(pillow);
// Additional furniture
// Second bookshelf
var bookshelf2 = new FurniturePiece('bookshelf', 300, 500);
bookshelf2.x = 800;
bookshelf2.y = 4000;
game.addChild(bookshelf2);
furniturePieces.push(bookshelf2);
// Second desk
var desk2 = new FurniturePiece('desk', 400, 200);
desk2.x = 3500;
desk2.y = 2200;
game.addChild(desk2);
furniturePieces.push(desk2);
// Second plant
var plant2 = new FurniturePiece('plant', 150, 200);
plant2.x = 2600;
plant2.y = 1400;
game.addChild(plant2);
furniturePieces.push(plant2);
// Second lamp
var lamp2 = new FurniturePiece('lamp', 100, 250);
lamp2.x = 4800;
lamp2.y = 3300;
game.addChild(lamp2);
furniturePieces.push(lamp2);
// Second bed
var bed2 = new FurniturePiece('bed', 600, 300);
bed2.x = 3000;
bed2.y = 6000;
game.addChild(bed2);
furniturePieces.push(bed2);
// Armchair (using sofa asset with different size)
var armchair = new FurniturePiece('sofa', 300, 200);
armchair.x = 4200;
armchair.y = 1800;
game.addChild(armchair);
furniturePieces.push(armchair);
// Coffee table (using table with different dimensions)
var coffeeTable = new FurniturePiece('table', 250, 120);
coffeeTable.x = 2300;
coffeeTable.y = 2800;
game.addChild(coffeeTable);
furniturePieces.push(coffeeTable);
// Add collection desk
var collectionDesk = new CollectionDesk();
collectionDesk.x = 3072; // Center of larger map
collectionDesk.y = 7800; // Bottom of larger 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: 5100,
y: 1300
},
// Near the bed
{
x: 1600,
y: 3700
},
// On the table
{
x: 1400,
y: 5500
},
// Behind the sofa
{
x: 5300,
y: 4200
},
// On bookshelf
{
x: 4700,
y: 6100
},
// Behind plant
{
x: 900,
y: 1700
},
// Near lamp
{
x: 5500,
y: 2100
},
// Under pillow
// Additional banana positions
{
x: 3800,
y: 1400
},
// Near top center
{
x: 1200,
y: 3200
},
// Left middle area
{
x: 2300,
y: 4700
},
// Middle area
{
x: 4300,
y: 5500
},
// Right middle area
{
x: 800,
y: 7000
},
// Bottom left corner
{
x: 2700,
y: 7500
},
// Near collection desk
{
x: 4500,
y: 7200
},
// Bottom right area
{
x: 5800,
y: 2800
},
// Far right middle
{
x: 3400,
y: 2600
},
// Upper middle
{
x: 1900,
y: 1900
},
// Upper left
{
x: 4000,
y: 4100
},
// Dead center
{
x: 5700,
y: 5000
},
// Lower right
{
x: 600,
y: 5000
} // Lower left
];
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 = 3072; // Start at center of larger map horizontally
player.y = 4098; // Middle of the larger map vertically
game.addChild(player);
// Create and add enemy character
enemy = new Enemy();
enemy.x = 800; // Start at left side of screen
enemy.y = 800; // Top portion of screen
game.addChild(enemy);
// 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) {
// Direct movement - same speed as enemy
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 () {
// Keep track of player's previous position to restore on collision
if (!player.lastX) player.lastX = player.x;
if (!player.lastY) player.lastY = player.y;
// Check wall boundaries
var playerRadius = player.width * 0.4; // Assuming player size
var isColliding = false;
// Collision with map boundaries
if (player.x < playerRadius || player.x > background.width - playerRadius || player.y < playerRadius || player.y > background.height - playerRadius) {
isColliding = true;
}
// Collision with furniture
for (var i = 0; i < furniturePieces.length; i++) {
var furniture = furniturePieces[i];
// Calculate distance between centers
var dx = player.x - furniture.x;
var dy = player.y - furniture.y;
var minDistX = playerRadius + furniture.width / 2;
var minDistY = playerRadius + furniture.height / 2;
// Check rectangular collision
if (Math.abs(dx) < minDistX && Math.abs(dy) < minDistY) {
isColliding = true;
break;
}
}
// If collision detected, restore previous position
if (isColliding) {
player.x = player.lastX;
player.y = player.lastY;
} else {
// Save current valid position
player.lastX = player.x;
player.lastY = player.y;
}
// 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);
// Update enemy
if (enemy) {
// Save last intersection status
enemy.lastIntersecting = enemy.intersects(player);
// Update enemy position (chase player)
enemy.update();
// Check if enemy touches player (automatic kill)
if (enemy.intersects(player)) {
// Flash screen red for game over effect
LK.effects.flashScreen(0xFF0000, 1000);
// Show game over
LK.showGameOver();
}
}
// 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 20 hidden bananas and\ndrag them to the collection desk!\nAvoid the red enemy!", {
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);