/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Fruit class
var Fruit = Container.expand(function () {
var self = Container.call(this);
var fruitGraphics = self.attachAsset('fruit', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.speed += 0.1; // Increase speed due to gravity
self.y += self.speed;
if (self.y > 2732) {
self.destroy();
// Check if the fruit is already missed
if (!self.missed) {
// Mark the fruit as missed
self.missed = true;
// Reduce lives by 1
lives--;
// Update lives text
livesTxt.setText('Lives: ' + lives);
// Flash screen red
LK.effects.flashScreen(0xff0000, 1000);
// If no lives left, end the game
if (lives <= 0) {
LK.showGameOver();
}
}
}
};
self.slice = function () {
// Create two fruit parts
var fruitPart1 = new FruitPart();
fruitPart1.x = self.x;
fruitPart1.y = self.y;
fruitPart1.speedX = -2; // Add horizontal speed to the left
game.addChild(fruitPart1);
var fruitPart2 = new FruitPart();
fruitPart2.x = self.x;
fruitPart2.y = self.y;
fruitPart2.speedX = 2; // Add horizontal speed to the right
game.addChild(fruitPart2);
// Destroy the fruit
self.destroy();
// Increase score only if it's not a bomb
if (!(self instanceof Bomb)) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
// Play sound when fruit is chopped
LK.getSound('chop').play();
}
};
self.containsPoint = function (point) {
return point.x >= self.x - self.width / 2 && point.x <= self.x + self.width / 2 && point.y >= self.y - self.height / 2 && point.y <= self.y + self.height / 2;
};
self.containsPoint = function (point) {
return point.x >= self.x - self.width / 2 && point.x <= self.x + self.width / 2 && point.y >= self.y - self.height / 2 && point.y <= self.y + self.height / 2;
};
});
// Bomb class
var Bomb = Fruit.expand(function () {
var self = Fruit.call(this);
self.attachAsset('bomb', {
anchorX: 0.5,
anchorY: 0.5
});
// Change color to black
self.attachAsset('bomb', {
anchorX: 0.5,
anchorY: 0.5
});
if (bombGraphics) {
bombGraphics.color = 0x000000;
}
self.slice = function () {
// Trigger game over when bomb is sliced
LK.showGameOver();
// Destroy the bomb
self.destroy();
};
});
// FruitPart class
var FruitPart = Container.expand(function () {
var self = Container.call(this);
var fruitPartGraphics = self.attachAsset('fruitPart', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.rotationSpeed = Math.random() * 0.1 - 0.05;
self.update = function () {
self.speed += 0.1; // Increase speed due to gravity
self.x += self.speedX; // Add horizontal movement
self.y += self.speed;
self.rotation += self.rotationSpeed;
if (self.y > 2732) {
self.destroy();
}
};
});
// Slash class
var Slash = Container.expand(function () {
var self = Container.call(this);
self.points = [];
self.graphics = self.attachAsset('line', {
anchorX: 0.5,
anchorY: 0.5
});
self.addPoint = function (x, y) {
// Maximum number of points in a Slash
var maxPoints = 10;
// If the maximum number of points has been reached, remove the oldest point
if (self.points.length >= maxPoints) {
self.points.shift();
}
// Add the new point
self.points.push({
x: x,
y: y
});
self.redraw();
};
self.redraw = function () {
self.graphics.removeChildren();
for (var i = 1; i < self.points.length; i++) {
var line = LK.getAsset('line', {
x: self.points[i - 1].x,
y: self.points[i - 1].y,
width: Math.hypot(self.points[i].x - self.points[i - 1].x, self.points[i].y - self.points[i - 1].y),
rotation: Math.atan2(self.points[i].y - self.points[i - 1].y, self.points[i].x - self.points[i - 1].x)
});
self.graphics.addChild(line);
}
// Set a timer to destroy the slash after 0.5 seconds
LK.setTimeout(function () {
var index = slashes.indexOf(self);
if (index > -1) {
slashes.splice(index, 1);
}
self.destroy();
}, 100);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
width: 2048,
height: 2732
});
// Initialize score text
var scoreTxt = new Text2('0', {
size: 150,
fill: "#000000"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize lives
var lives = 3;
var livesTxt = new Text2('Lives: ' + lives, {
size: 150,
fill: "#000000"
});
livesTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(livesTxt);
// Array to keep track of fruits
var fruits = [];
// Function to spawn a new fruit
function spawnFruit() {
var newFruit;
// 20% chance to spawn a bomb instead of a fruit after score reaches 50
if (LK.getScore() >= 50 && Math.random() <= 0.2) {
newFruit = new Bomb();
} else {
newFruit = new Fruit();
}
newFruit.x = Math.random() * (2048 - 400) + 200; // Start from a random position along the bottom edge, but within a margin of 200 from the left and right edges
newFruit.y = 2732; // Start from the bottom
newFruit.speed = -Math.random() * 5 - 15; // Set speed to throw them at different heights
fruits.push(newFruit);
game.addChild(newFruit);
}
// Handle slicing fruits
var dragNode = null;
var slashes = [];
game.down = function (x, y, obj) {
var slash = new Slash();
slash.addPoint(x, y);
game.addChild(slash);
slashes.push(slash);
dragNode = slash;
};
// Update game every tick
game.update = function () {
for (var i = fruits.length - 1; i >= 0; i--) {
fruits[i].update();
// Check if the fruit intersects with the slash
if (dragNode) {
for (var j = 0; j < dragNode.points.length; j++) {
if (fruits[i].containsPoint(dragNode.points[j])) {
// If the sliced fruit is a bomb, trigger game over
if (fruits[i] instanceof Bomb) {
LK.showGameOver();
} else {
// Slice the fruit
fruits[i].slice();
}
// Remove the fruit from the array
fruits.splice(i, 1);
break;
}
}
}
}
// Spawn a chunk of fruits every 60 ticks (1 second) only if all fruits have come down
if (LK.ticks % 60 == 0) {
var allFruitsDown = true;
for (var i = 0; i < fruits.length; i++) {
if (fruits[i].y < 2732 / 2) {
allFruitsDown = false;
break;
}
}
if (allFruitsDown) {
// Spawn a chunk of fruits based on score
var chunkSize = Math.floor(LK.getScore() / 50) * 2 + Math.floor(Math.random() * 3) + 3;
for (var i = 0; i < chunkSize; i++) {
spawnFruit();
}
}
}
};
game.move = function (x, y, obj) {
if (dragNode) {
dragNode.addPoint(x, y);
dragNode.redraw();
}
};
game.up = function (x, y, obj) {
if (dragNode) {
dragNode.redraw();
dragNode = null;
}
};