/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1"); /**** * Classes ****/ var Fruit = Container.expand(function (type) { var self = Container.call(this); self.type = type || 'apple'; self.isBomb = self.type === 'bomb'; // Create fruit graphic var fruitGraphic = self.attachAsset(self.type, { anchorX: 0.5, anchorY: 0.5 }); // Add some visual distinction to different fruits if (self.type === 'banana') { fruitGraphic.rotation = Math.PI / 4; // Rotate bananas by 45 degrees } else if (self.type === 'grapes') { fruitGraphic.scale.set(0.9, 1.1); // Make grapes more oval } else if (self.type === 'bomb') { // Add a small "fuse" on top of the bomb var fuse = LK.getAsset('palmLeaf', { width: 20, height: 40, color: 0x808080, anchorX: 0.5, anchorY: 1 }); fuse.rotation = -Math.PI / 4; // Tilt the fuse fuse.position.set(0, -fruitGraphic.height / 2); self.addChild(fuse); } // Initialize physics properties self.speedY = 0; self.speedX = 0; self.gravity = 0.1; // Reduced gravity for slower falling self.active = true; // Make fruit interactive self.interactive = true; // Handle tap on fruit self.down = function () { if (!self.active) return; self.active = false; if (self.isBomb) { // Hit a bomb LK.getSound('explosion').play(); LK.effects.flashObject(self, 0xFF0000, 300); tween(self, { alpha: 0, scaleX: 2, scaleY: 2 }, { duration: 300, onFinish: function onFinish() { // Game over happens in the game's update method fruits.forEach(function (fruit) { fruit.active = false; }); gameState = 'gameOver'; } }); } else { // Hit a fruit LK.getSound('pop').play(); LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); // Animate the fruit disappearing tween(self, { alpha: 0, scaleX: 1.5, scaleY: 1.5 }, { duration: 300, onFinish: function onFinish() { self.destroy(); var index = fruits.indexOf(self); if (index > -1) { fruits.splice(index, 1); } } }); } }; // Update the fruit's position self.update = function () { if (!self.active) return; self.speedY += self.gravity; self.y += self.speedY; self.x += self.speedX; // Check if the fruit hit the ground if (self.y > groundY - fruitGraphic.height / 2) { if (self.isBomb) { // Bombs can safely hit the ground self.active = false; tween(self, { alpha: 0 }, { duration: 300, onFinish: function onFinish() { self.destroy(); var index = fruits.indexOf(self); if (index > -1) { fruits.splice(index, 1); } } }); } else { // Missed a fruit self.active = false; LK.effects.flashObject(self, 0xFF0000, 300); tween(self, { alpha: 0 }, { duration: 300, onFinish: function onFinish() { // Game over happens in the game's update method fruits.forEach(function (fruit) { fruit.active = false; }); gameState = 'gameOver'; } }); } } // Remove if went off the side of the screen if (self.x < -100 || self.x > 2148) { self.active = false; self.destroy(); var index = fruits.indexOf(self); if (index > -1) { fruits.splice(index, 1); } } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Game state management var gameState = 'playing'; var fruits = []; var spawnTimer = null; var difficulty = 1; var difficultyTimer = null; var groundY = 2732 - 100; // Position of the ground // Create beach scene // Ocean with realistic waves effect var ocean = game.addChild(LK.getAsset('ocean', { anchorX: 0, anchorY: 0, y: 0 })); // Animate ocean waves tween(ocean, { y: -5 }, { duration: 2000, yoyo: true, repeat: -1, easing: tween.easeInOut }); // Bright tropical sun with glow effect var sun = game.addChild(LK.getAsset('sun', { anchorX: 0.5, anchorY: 0.5, x: 300, y: 200 })); // Add sun ray effect tween(sun.scale, { x: 1.05, y: 1.05 }, { duration: 3000, yoyo: true, repeat: -1, easing: tween.easeInOut }); // Sandy beach var sand = game.addChild(LK.getAsset('sand', { anchorX: 0, anchorY: 0, y: groundY - 100 })); // Add some palm trees for decoration function createPalm(x) { var palm = LK.getAsset('palm', { anchorX: 0.5, anchorY: 1, x: x, y: groundY }); // Add palm leaves for (var i = 0; i < 5; i++) { var leaf = LK.getAsset('palmLeaf', { anchorX: 0, anchorY: 0.5, x: 0, y: -palm.height + 50 }); leaf.rotation = i / 5 * Math.PI * 2; palm.addChild(leaf); } return game.addChild(palm); } createPalm(200); createPalm(1800); // Create score display var scoreTxt = new Text2('0', { size: 100, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Function to spawn a new fruit function spawnFruit() { // Determine fruit type with random chance var fruitTypes = ['apple', 'banana', 'orange', 'grapes']; var type; // 15% chance of bomb, increasing with difficulty if (Math.random() < 0.15 + difficulty * 0.01) { type = 'bomb'; } else { type = fruitTypes[Math.floor(Math.random() * fruitTypes.length)]; } var newFruit = new Fruit(type); // Set random position at top of screen newFruit.x = Math.random() * (2048 - 200) + 100; newFruit.y = -100; // Random initial velocities - slower than before newFruit.speedY = Math.random() * 1 + 1 + difficulty * 0.3; // Reduced initial speed newFruit.speedX = (Math.random() - 0.5) * 3; // Slightly reduced horizontal speed fruits.push(newFruit); game.addChild(newFruit); } // Start the game function startGame() { // Reset game state gameState = 'playing'; difficulty = 1; // Clear any existing fruits while (fruits.length > 0) { var fruit = fruits.pop(); if (fruit) { fruit.destroy(); } } // Reset score LK.setScore(0); scoreTxt.setText('0'); // Start fruit spawning with longer intervals to account for slower movement spawnTimer = LK.setInterval(function () { if (gameState === 'playing') { spawnFruit(); // Spawn multiple fruits at higher difficulties if (difficulty >= 3 && Math.random() < 0.3) { LK.setTimeout(spawnFruit, 500); } if (difficulty >= 5 && Math.random() < 0.2) { LK.setTimeout(spawnFruit, 800); } } }, 2000 - difficulty * 100); // Increase difficulty over time difficultyTimer = LK.setInterval(function () { if (gameState === 'playing' && difficulty < 10) { difficulty += 1; // Update spawn rate LK.clearInterval(spawnTimer); spawnTimer = LK.setInterval(function () { if (gameState === 'playing') { spawnFruit(); // Spawn multiple fruits at higher difficulties if (difficulty >= 3 && Math.random() < 0.3) { LK.setTimeout(spawnFruit, 300); } if (difficulty >= 5 && Math.random() < 0.2) { LK.setTimeout(spawnFruit, 600); } } }, 1500 - difficulty * 100); } }, 10000); // Play background music LK.playMusic('beachMusic', { fade: { start: 0, end: 0.5, duration: 1000 } }); } // Start the game right away startGame(); // Update game state game.update = function () { // Update all fruits for (var i = fruits.length - 1; i >= 0; i--) { fruits[i].update(); } // Check for game over condition if (gameState === 'gameOver') { LK.clearInterval(spawnTimer); LK.clearInterval(difficultyTimer); // Show game over screen after a short delay LK.setTimeout(function () { LK.showGameOver(); }, 1000); gameState = 'over'; // Prevent multiple game over calls } }; // Game-wide touch event handling game.down = function (x, y, obj) { // The down events on fruits will be handled by their own down methods }; game.move = function (x, y, obj) { // No special handling needed for movement }; game.up = function (x, y, obj) { // No special handling needed for release };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Fruit = Container.expand(function (type) {
var self = Container.call(this);
self.type = type || 'apple';
self.isBomb = self.type === 'bomb';
// Create fruit graphic
var fruitGraphic = self.attachAsset(self.type, {
anchorX: 0.5,
anchorY: 0.5
});
// Add some visual distinction to different fruits
if (self.type === 'banana') {
fruitGraphic.rotation = Math.PI / 4; // Rotate bananas by 45 degrees
} else if (self.type === 'grapes') {
fruitGraphic.scale.set(0.9, 1.1); // Make grapes more oval
} else if (self.type === 'bomb') {
// Add a small "fuse" on top of the bomb
var fuse = LK.getAsset('palmLeaf', {
width: 20,
height: 40,
color: 0x808080,
anchorX: 0.5,
anchorY: 1
});
fuse.rotation = -Math.PI / 4; // Tilt the fuse
fuse.position.set(0, -fruitGraphic.height / 2);
self.addChild(fuse);
}
// Initialize physics properties
self.speedY = 0;
self.speedX = 0;
self.gravity = 0.1; // Reduced gravity for slower falling
self.active = true;
// Make fruit interactive
self.interactive = true;
// Handle tap on fruit
self.down = function () {
if (!self.active) return;
self.active = false;
if (self.isBomb) {
// Hit a bomb
LK.getSound('explosion').play();
LK.effects.flashObject(self, 0xFF0000, 300);
tween(self, {
alpha: 0,
scaleX: 2,
scaleY: 2
}, {
duration: 300,
onFinish: function onFinish() {
// Game over happens in the game's update method
fruits.forEach(function (fruit) {
fruit.active = false;
});
gameState = 'gameOver';
}
});
} else {
// Hit a fruit
LK.getSound('pop').play();
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
// Animate the fruit disappearing
tween(self, {
alpha: 0,
scaleX: 1.5,
scaleY: 1.5
}, {
duration: 300,
onFinish: function onFinish() {
self.destroy();
var index = fruits.indexOf(self);
if (index > -1) {
fruits.splice(index, 1);
}
}
});
}
};
// Update the fruit's position
self.update = function () {
if (!self.active) return;
self.speedY += self.gravity;
self.y += self.speedY;
self.x += self.speedX;
// Check if the fruit hit the ground
if (self.y > groundY - fruitGraphic.height / 2) {
if (self.isBomb) {
// Bombs can safely hit the ground
self.active = false;
tween(self, {
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
self.destroy();
var index = fruits.indexOf(self);
if (index > -1) {
fruits.splice(index, 1);
}
}
});
} else {
// Missed a fruit
self.active = false;
LK.effects.flashObject(self, 0xFF0000, 300);
tween(self, {
alpha: 0
}, {
duration: 300,
onFinish: function onFinish() {
// Game over happens in the game's update method
fruits.forEach(function (fruit) {
fruit.active = false;
});
gameState = 'gameOver';
}
});
}
}
// Remove if went off the side of the screen
if (self.x < -100 || self.x > 2148) {
self.active = false;
self.destroy();
var index = fruits.indexOf(self);
if (index > -1) {
fruits.splice(index, 1);
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB // Sky blue background
});
/****
* Game Code
****/
// Game state management
var gameState = 'playing';
var fruits = [];
var spawnTimer = null;
var difficulty = 1;
var difficultyTimer = null;
var groundY = 2732 - 100; // Position of the ground
// Create beach scene
// Ocean with realistic waves effect
var ocean = game.addChild(LK.getAsset('ocean', {
anchorX: 0,
anchorY: 0,
y: 0
}));
// Animate ocean waves
tween(ocean, {
y: -5
}, {
duration: 2000,
yoyo: true,
repeat: -1,
easing: tween.easeInOut
});
// Bright tropical sun with glow effect
var sun = game.addChild(LK.getAsset('sun', {
anchorX: 0.5,
anchorY: 0.5,
x: 300,
y: 200
}));
// Add sun ray effect
tween(sun.scale, {
x: 1.05,
y: 1.05
}, {
duration: 3000,
yoyo: true,
repeat: -1,
easing: tween.easeInOut
});
// Sandy beach
var sand = game.addChild(LK.getAsset('sand', {
anchorX: 0,
anchorY: 0,
y: groundY - 100
}));
// Add some palm trees for decoration
function createPalm(x) {
var palm = LK.getAsset('palm', {
anchorX: 0.5,
anchorY: 1,
x: x,
y: groundY
});
// Add palm leaves
for (var i = 0; i < 5; i++) {
var leaf = LK.getAsset('palmLeaf', {
anchorX: 0,
anchorY: 0.5,
x: 0,
y: -palm.height + 50
});
leaf.rotation = i / 5 * Math.PI * 2;
palm.addChild(leaf);
}
return game.addChild(palm);
}
createPalm(200);
createPalm(1800);
// Create score display
var scoreTxt = new Text2('0', {
size: 100,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to spawn a new fruit
function spawnFruit() {
// Determine fruit type with random chance
var fruitTypes = ['apple', 'banana', 'orange', 'grapes'];
var type;
// 15% chance of bomb, increasing with difficulty
if (Math.random() < 0.15 + difficulty * 0.01) {
type = 'bomb';
} else {
type = fruitTypes[Math.floor(Math.random() * fruitTypes.length)];
}
var newFruit = new Fruit(type);
// Set random position at top of screen
newFruit.x = Math.random() * (2048 - 200) + 100;
newFruit.y = -100;
// Random initial velocities - slower than before
newFruit.speedY = Math.random() * 1 + 1 + difficulty * 0.3; // Reduced initial speed
newFruit.speedX = (Math.random() - 0.5) * 3; // Slightly reduced horizontal speed
fruits.push(newFruit);
game.addChild(newFruit);
}
// Start the game
function startGame() {
// Reset game state
gameState = 'playing';
difficulty = 1;
// Clear any existing fruits
while (fruits.length > 0) {
var fruit = fruits.pop();
if (fruit) {
fruit.destroy();
}
}
// Reset score
LK.setScore(0);
scoreTxt.setText('0');
// Start fruit spawning with longer intervals to account for slower movement
spawnTimer = LK.setInterval(function () {
if (gameState === 'playing') {
spawnFruit();
// Spawn multiple fruits at higher difficulties
if (difficulty >= 3 && Math.random() < 0.3) {
LK.setTimeout(spawnFruit, 500);
}
if (difficulty >= 5 && Math.random() < 0.2) {
LK.setTimeout(spawnFruit, 800);
}
}
}, 2000 - difficulty * 100);
// Increase difficulty over time
difficultyTimer = LK.setInterval(function () {
if (gameState === 'playing' && difficulty < 10) {
difficulty += 1;
// Update spawn rate
LK.clearInterval(spawnTimer);
spawnTimer = LK.setInterval(function () {
if (gameState === 'playing') {
spawnFruit();
// Spawn multiple fruits at higher difficulties
if (difficulty >= 3 && Math.random() < 0.3) {
LK.setTimeout(spawnFruit, 300);
}
if (difficulty >= 5 && Math.random() < 0.2) {
LK.setTimeout(spawnFruit, 600);
}
}
}, 1500 - difficulty * 100);
}
}, 10000);
// Play background music
LK.playMusic('beachMusic', {
fade: {
start: 0,
end: 0.5,
duration: 1000
}
});
}
// Start the game right away
startGame();
// Update game state
game.update = function () {
// Update all fruits
for (var i = fruits.length - 1; i >= 0; i--) {
fruits[i].update();
}
// Check for game over condition
if (gameState === 'gameOver') {
LK.clearInterval(spawnTimer);
LK.clearInterval(difficultyTimer);
// Show game over screen after a short delay
LK.setTimeout(function () {
LK.showGameOver();
}, 1000);
gameState = 'over'; // Prevent multiple game over calls
}
};
// Game-wide touch event handling
game.down = function (x, y, obj) {
// The down events on fruits will be handled by their own down methods
};
game.move = function (x, y, obj) {
// No special handling needed for movement
};
game.up = function (x, y, obj) {
// No special handling needed for release
};