/**** * Classes ****/ // BirdMother class var BirdMother = Container.expand(function () { var self = Container.call(this); var birdMotherGraphics = self.attachAsset('birdMother', { anchorX: 0.5, anchorY: 0.5 }); self.x = 1024; self.y = 350; }); // Bomb class var Bomb = Container.expand(function () { var self = Container.call(this); var bombGraphics = self.attachAsset('bomb', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 8 + Math.floor(score / 10); self.update = function () { self.y -= self.speed; if (self.y < -bombGraphics.height) { self.destroy(); } }; self.containsPoint = function (point) { var dx = point.x - self.x; var dy = point.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); return distance < bombGraphics.width / 2; }; }); //<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, scaleX: 3, scaleY: 3 }); self.speed = 8 + Math.floor(score / 10); self.update = function () { self.y -= self.speed; if (self.y < -fruitGraphics.height) { self.destroy(); } }; self.containsPoint = function (point) { var dx = point.x - self.x; var dy = point.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); return distance < fruitGraphics.width; }; }); // PauseButton class var PauseButton = Container.expand(function () { var self = Container.call(this); var pauseButtonGraphics = self.attachAsset('pause', { anchorX: 0.5, anchorY: 0.5 }); self.x = 2048 - pauseButtonGraphics.width / 2; self.y = pauseButtonGraphics.height / 2; self.down = function (x, y, obj) { game.paused = !game.paused; if (game.paused) { for (var i = fruits.length - 1; i >= 0; i--) { fruits[i].destroy(); fruits.splice(i, 1); } pauseButtonGraphics = self.attachAsset('continue', { anchorX: 0.5, anchorY: 0.5 }); } else { pauseButtonGraphics = self.attachAsset('pause', { anchorX: 0.5, anchorY: 0.5 }); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x008080 //Change background color to teal }); /**** * Game Code ****/ var truck = game.addChild(LK.getAsset('truck', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 1366 })); var birdMother = game.addChild(new BirdMother()); var pauseButton = game.addChild(new PauseButton()); // Initialize score var score = 0; var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Array to keep track of fruits var fruits = []; // Function to spawn a new fruit function spawnFruit() { var newFruit = new Fruit(); newFruit.x = Math.random() * 2048; newFruit.y = 2732 + newFruit.height; fruits.push(newFruit); game.addChild(newFruit); } // Function to handle slicing fruits function handleSlice(x, y, obj) { for (var i = fruits.length - 1; i >= 0; i--) { if (fruits[i].containsPoint({ x: x, y: y })) { if (fruits[i] instanceof Bomb) { LK.showGameOver(); } else { score += 1; } fruits[i].destroy(); fruits.splice(i, 1); scoreTxt.setText(score); } } } // Set up game event listeners game.down = function (x, y, obj) { handleSlice(x, y, obj); }; game.move = function (x, y, obj) { handleSlice(x, y, obj); }; // Game update function game.update = function () { if (!game.paused) { for (var i = fruits.length - 1; i >= 0; i--) { fruits[i].update(); if (fruits[i].y < 0 && fruits[i] instanceof Fruit) { LK.showGameOver(); } } if (LK.ticks % 60 == 0) { spawnFruit(); if (Math.random() < 0.3) { var newBomb = new Bomb(); newBomb.x = Math.random() * 2048; newBomb.y = 2732 + newBomb.height; fruits.push(newBomb); game.addChild(newBomb); } } } };
/****
* Classes
****/
// BirdMother class
var BirdMother = Container.expand(function () {
var self = Container.call(this);
var birdMotherGraphics = self.attachAsset('birdMother', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = 1024;
self.y = 350;
});
// Bomb class
var Bomb = Container.expand(function () {
var self = Container.call(this);
var bombGraphics = self.attachAsset('bomb', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8 + Math.floor(score / 10);
self.update = function () {
self.y -= self.speed;
if (self.y < -bombGraphics.height) {
self.destroy();
}
};
self.containsPoint = function (point) {
var dx = point.x - self.x;
var dy = point.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
return distance < bombGraphics.width / 2;
};
});
//<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,
scaleX: 3,
scaleY: 3
});
self.speed = 8 + Math.floor(score / 10);
self.update = function () {
self.y -= self.speed;
if (self.y < -fruitGraphics.height) {
self.destroy();
}
};
self.containsPoint = function (point) {
var dx = point.x - self.x;
var dy = point.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
return distance < fruitGraphics.width;
};
});
// PauseButton class
var PauseButton = Container.expand(function () {
var self = Container.call(this);
var pauseButtonGraphics = self.attachAsset('pause', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = 2048 - pauseButtonGraphics.width / 2;
self.y = pauseButtonGraphics.height / 2;
self.down = function (x, y, obj) {
game.paused = !game.paused;
if (game.paused) {
for (var i = fruits.length - 1; i >= 0; i--) {
fruits[i].destroy();
fruits.splice(i, 1);
}
pauseButtonGraphics = self.attachAsset('continue', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
pauseButtonGraphics = self.attachAsset('pause', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x008080 //Change background color to teal
});
/****
* Game Code
****/
var truck = game.addChild(LK.getAsset('truck', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1366
}));
var birdMother = game.addChild(new BirdMother());
var pauseButton = game.addChild(new PauseButton());
// Initialize score
var score = 0;
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Array to keep track of fruits
var fruits = [];
// Function to spawn a new fruit
function spawnFruit() {
var newFruit = new Fruit();
newFruit.x = Math.random() * 2048;
newFruit.y = 2732 + newFruit.height;
fruits.push(newFruit);
game.addChild(newFruit);
}
// Function to handle slicing fruits
function handleSlice(x, y, obj) {
for (var i = fruits.length - 1; i >= 0; i--) {
if (fruits[i].containsPoint({
x: x,
y: y
})) {
if (fruits[i] instanceof Bomb) {
LK.showGameOver();
} else {
score += 1;
}
fruits[i].destroy();
fruits.splice(i, 1);
scoreTxt.setText(score);
}
}
}
// Set up game event listeners
game.down = function (x, y, obj) {
handleSlice(x, y, obj);
};
game.move = function (x, y, obj) {
handleSlice(x, y, obj);
};
// Game update function
game.update = function () {
if (!game.paused) {
for (var i = fruits.length - 1; i >= 0; i--) {
fruits[i].update();
if (fruits[i].y < 0 && fruits[i] instanceof Fruit) {
LK.showGameOver();
}
}
if (LK.ticks % 60 == 0) {
spawnFruit();
if (Math.random() < 0.3) {
var newBomb = new Bomb();
newBomb.x = Math.random() * 2048;
newBomb.y = 2732 + newBomb.height;
fruits.push(newBomb);
game.addChild(newBomb);
}
}
}
};