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() {
nextFruitPreview.destroy();
nextFruitPreview = game.addChild(LK.getAsset(nextFruitType, {
anchorX: 0.5,
anchorY: 0.5,
x: dropPosition,
y: 600
}));
}
function getRandomSmallFruit() {
var smallFruits = ['grape', 'cherry', 'strawberry', 'lemon'];
return smallFruits[Math.floor(Math.random() * smallFruits.length)];
}
function checkGameOver() {
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].y - fruits[i].radius < gameOverLine) {
LK.showGameOver();
return true;
}
}
return false;
}
// Initial setup
nextFruitType = getRandomSmallFruit();
updateNextFruitPreview();
game.move = function (x, y, obj) {
if (!canDrop) return;
// Constrain drop position within container bounds
dropPosition = Math.max(containerLeft + 60, Math.min(containerRight - 60, x));
nextFruitPreview.x = dropPosition;
};
game.down = function (x, y, obj) {
if (!canDrop) return;
// Drop the fruit
var newFruit = new Fruit(nextFruitType, dropPosition, 500);
fruits.push(newFruit);
game.addChild(newFruit);
// 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 (LK.ticks % 30 === 0) {
// Check every half second
checkGameOver();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,262 @@
-/****
+/****
+* 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: 0x000000
-});
\ No newline at end of file
+ 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() {
+ nextFruitPreview.destroy();
+ nextFruitPreview = game.addChild(LK.getAsset(nextFruitType, {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: dropPosition,
+ y: 600
+ }));
+}
+function getRandomSmallFruit() {
+ var smallFruits = ['grape', 'cherry', 'strawberry', 'lemon'];
+ return smallFruits[Math.floor(Math.random() * smallFruits.length)];
+}
+function checkGameOver() {
+ for (var i = 0; i < fruits.length; i++) {
+ if (fruits[i].y - fruits[i].radius < gameOverLine) {
+ LK.showGameOver();
+ return true;
+ }
+ }
+ return false;
+}
+// Initial setup
+nextFruitType = getRandomSmallFruit();
+updateNextFruitPreview();
+game.move = function (x, y, obj) {
+ if (!canDrop) return;
+ // Constrain drop position within container bounds
+ dropPosition = Math.max(containerLeft + 60, Math.min(containerRight - 60, x));
+ nextFruitPreview.x = dropPosition;
+};
+game.down = function (x, y, obj) {
+ if (!canDrop) return;
+ // Drop the fruit
+ var newFruit = new Fruit(nextFruitType, dropPosition, 500);
+ fruits.push(newFruit);
+ game.addChild(newFruit);
+ // 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 (LK.ticks % 30 === 0) {
+ // Check every half second
+ checkGameOver();
+ }
+};
\ No newline at end of file