User prompt
When the fruit hit the ground a sound happen
User prompt
Play the music and make it loop
User prompt
Put the fruit in the container
User prompt
No no don't put the fruit up the top it will be in the container
User prompt
If the fruit reach the top is game over
User prompt
You can move fruit and you can place it ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please remove the game over
Code edit (1 edits merged)
Please save this source code
User prompt
Watermelon Merge
Initial prompt
Watermelon
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Fruit = Container.expand(function (type, x, y) {
var self = Container.call(this);
self.fruitType = type;
self.fruitTypes = ['grape', 'cherry', 'strawberry', 'lemon', 'orange', 'apple', 'pear', 'peach', 'pineapple', 'melon', 'watermelon'];
self.fruitSizes = [80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 300];
self.fruitPoints = [10, 20, 40, 80, 160, 320, 640, 1280, 2560, 5120, 10240];
self.graphics = self.attachAsset(type, {
anchorX: 0.5,
anchorY: 0.5
});
self.x = x;
self.y = y;
self.vx = 0;
self.vy = 0;
self.radius = self.fruitSizes[self.fruitTypes.indexOf(type)] / 2;
self.gravity = 0.8;
self.bounce = 0.3;
self.friction = 0.99;
self.hasMerged = false;
self.update = function () {
if (self.hasMerged) return;
// Apply gravity
self.vy += self.gravity;
// Apply velocity
self.x += self.vx;
self.y += self.vy;
// Apply friction
self.vx *= self.friction;
self.vy *= self.friction;
// Boundary collision with container walls
if (self.x - self.radius < containerLeft + 40) {
self.x = containerLeft + 40 + self.radius;
self.vx = -self.vx * self.bounce;
}
if (self.x + self.radius > containerRight - 40) {
self.x = containerRight - 40 - self.radius;
self.vx = -self.vx * self.bounce;
}
// Boundary collision with container bottom
if (self.y + self.radius > containerBottom - 20) {
self.y = containerBottom - 20 - self.radius;
self.vy = -self.vy * self.bounce;
if (Math.abs(self.vy) < 1) {
self.vy = 0;
}
}
// Check for merging with other fruits
for (var i = 0; i < fruits.length; i++) {
var other = fruits[i];
if (other === self || other.hasMerged || self.hasMerged) continue;
if (other.fruitType !== self.fruitType) continue;
var dx = other.x - self.x;
var dy = other.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var minDistance = self.radius + other.radius;
if (distance < minDistance) {
// Merge fruits
self.merge(other);
break;
}
}
// Check for collision with other fruits
for (var i = 0; i < fruits.length; i++) {
var other = fruits[i];
if (other === self || other.hasMerged || self.hasMerged) continue;
var dx = other.x - self.x;
var dy = other.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
var minDistance = self.radius + other.radius;
if (distance < minDistance && distance > 0) {
// Separate fruits
var overlap = minDistance - distance;
var separationX = dx / distance * overlap * 0.5;
var separationY = dy / distance * overlap * 0.5;
self.x -= separationX;
self.y -= separationY;
other.x += separationX;
other.y += separationY;
// Exchange velocities
var tempVx = self.vx;
var tempVy = self.vy;
self.vx = other.vx * self.bounce;
self.vy = other.vy * self.bounce;
other.vx = tempVx * other.bounce;
other.vy = tempVy * other.bounce;
}
}
};
self.merge = function (other) {
if (self.hasMerged || other.hasMerged) return;
var typeIndex = self.fruitTypes.indexOf(self.fruitType);
if (typeIndex >= self.fruitTypes.length - 1) return; // Already watermelon
var newType = self.fruitTypes[typeIndex + 1];
var points = self.fruitPoints[typeIndex + 1];
// Mark both fruits as merged
self.hasMerged = true;
other.hasMerged = true;
// Create new fruit at average position
var newX = (self.x + other.x) / 2;
var newY = (self.y + other.y) / 2;
// Add score
LK.setScore(LK.getScore() + points);
scoreTxt.setText(LK.getScore());
// Play merge sound
LK.getSound('merge').play();
// Flash effect
LK.effects.flashObject(self, 0xffffff, 200);
LK.effects.flashObject(other, 0xffffff, 200);
// Remove old fruits after a short delay
LK.setTimeout(function () {
// Remove from fruits array
var selfIndex = fruits.indexOf(self);
if (selfIndex >= 0) {
fruits.splice(selfIndex, 1);
}
var otherIndex = fruits.indexOf(other);
if (otherIndex >= 0) {
fruits.splice(otherIndex, 1);
}
// Destroy old fruits
self.destroy();
other.destroy();
// Create new fruit
var newFruit = new Fruit(newType, newX, newY);
fruits.push(newFruit);
game.addChild(newFruit);
}, 200);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
var fruits = [];
var nextFruitType = 'grape';
var containerLeft = 224;
var containerRight = 1824;
var containerBottom = 2200;
var containerTop = 800;
var dropPosition = 1024;
var canDrop = true;
var gameOverLine = containerTop - 100;
// Create container
var container = game.addChild(LK.getAsset('container', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1500
}));
// Create container walls
var leftWall = game.addChild(LK.getAsset('containerWall', {
anchorX: 0.5,
anchorY: 0.5,
x: containerLeft,
y: 1500
}));
var rightWall = game.addChild(LK.getAsset('containerWall', {
anchorX: 0.5,
anchorY: 0.5,
x: containerRight,
y: 1500
}));
var bottomWall = game.addChild(LK.getAsset('containerBottom', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: containerBottom
}));
// Create score display
var scoreTxt = new Text2('0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create next fruit preview
var nextFruitPreview = game.addChild(LK.getAsset('nextFruit', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 600
}));
function updateNextFruitPreview() {
// Fade out old preview
tween(nextFruitPreview, {
alpha: 0,
scaleX: 0.5,
scaleY: 0.5
}, {
duration: 150,
easing: tween.easeIn,
onFinish: function onFinish() {
nextFruitPreview.destroy();
nextFruitPreview = game.addChild(LK.getAsset(nextFruitType, {
anchorX: 0.5,
anchorY: 0.5,
x: dropPosition,
y: 600
}));
// Fade in new preview
nextFruitPreview.alpha = 0;
nextFruitPreview.scaleX = 1.2;
nextFruitPreview.scaleY = 1.2;
tween(nextFruitPreview, {
alpha: 1,
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.bounceOut
});
}
});
}
function getRandomSmallFruit() {
var smallFruits = ['grape', 'cherry', 'strawberry', 'lemon'];
return smallFruits[Math.floor(Math.random() * smallFruits.length)];
}
// checkGameOver function removed
// Initial setup
nextFruitType = getRandomSmallFruit();
updateNextFruitPreview();
game.move = function (x, y, obj) {
if (!canDrop) return;
// Constrain drop position within container bounds
var newDropPosition = Math.max(containerLeft + 60, Math.min(containerRight - 60, x));
// Only update if position actually changed
if (Math.abs(newDropPosition - dropPosition) > 5) {
dropPosition = newDropPosition;
// Smooth tween animation for fruit preview movement
tween.stop(nextFruitPreview, {
x: true
}); // Stop any existing x movement
tween(nextFruitPreview, {
x: dropPosition
}, {
duration: 150,
easing: tween.easeOut
});
}
};
game.down = function (x, y, obj) {
if (!canDrop) return;
// Add scale down animation to preview before dropping
tween(nextFruitPreview, {
scaleX: 0.8,
scaleY: 0.8
}, {
duration: 100,
easing: tween.easeIn,
onFinish: function onFinish() {
// Drop the fruit
var newFruit = new Fruit(nextFruitType, dropPosition, 500);
fruits.push(newFruit);
game.addChild(newFruit);
// Add entry animation to the new fruit
newFruit.scaleX = 0.5;
newFruit.scaleY = 0.5;
tween(newFruit, {
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.bounceOut
});
// Play drop sound
LK.getSound('drop').play();
// Prevent dropping for a short time
canDrop = false;
LK.setTimeout(function () {
canDrop = true;
}, 500);
// Generate next fruit
nextFruitType = getRandomSmallFruit();
updateNextFruitPreview();
}
});
};
game.update = function () {
// Update all fruits
for (var i = fruits.length - 1; i >= 0; i--) {
if (fruits[i] && !fruits[i].hasMerged) {
// Check if fruit still exists (might have been destroyed in merge)
if (fruits[i].parent) {
fruits[i].update();
}
}
}
// Check for game over - if any fruit reaches the top
for (var i = 0; i < fruits.length; i++) {
if (fruits[i] && !fruits[i].hasMerged) {
if (fruits[i].y - fruits[i].radius <= gameOverLine) {
LK.showGameOver();
return;
}
}
}
}; ===================================================================
--- original.js
+++ change.js
@@ -154,9 +154,9 @@
var containerBottom = 2200;
var containerTop = 800;
var dropPosition = 1024;
var canDrop = true;
-var gameOverLine = containerTop + 100;
+var gameOverLine = containerTop - 100;
// Create container
var container = game.addChild(LK.getAsset('container', {
anchorX: 0.5,
anchorY: 0.5,