/****
* Plugins
****/
var facekit = LK.import("@upit/facekit.v1");
/****
* Classes
****/
// Class for Bad Items
var BadItem = Container.expand(function () {
var self = Container.call(this);
var badItemGraphics = self.attachAsset('badItem', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
};
});
// Class for the Bucket (Player)
var Bucket = Container.expand(function () {
var self = Container.call(this);
self.bucketGraphics = self.attachAsset('bucket', {
anchorX: 0.5,
anchorY: 0.5
});
self.bucketGraphics.visible = true;
var closedGraphics = self.attachAsset('closed', {
anchorX: 0.5,
anchorY: 0.5
});
closedGraphics.visible = false;
var bucketGraphics = self.bucketGraphics;
self.update = function () {
self.x = facekit.mouthCenter.x;
self.y = facekit.mouthCenter.y;
if (facekit.mouthOpen) {
bucketGraphics.visible = true;
closedGraphics.visible = false;
} else {
bucketGraphics.visible = false;
closedGraphics.visible = true;
}
};
});
// Class for Falling Objects
var FallingObject = Container.expand(function () {
var self = Container.call(this);
var objectGraphics = self.attachAsset('fallingObject', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
};
});
// Class for Power-ups
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
//<Assets used in the game will automatically appear here>
// Initialize game elements
var bucket = game.addChild(new Bucket());
bucket.x = 2048 / 2;
bucket.y = 2500;
var fallingObjects = [];
var powerUps = [];
var badItems = [];
// Handle game move events
game.move = function (x, y, obj) {
bucket.x = x;
};
// Update game logic
game.update = function () {
// Update falling objects
for (var i = fallingObjects.length - 1; i >= 0; i--) {
var obj = fallingObjects[i];
obj.update();
if (obj.y > 2732) {
obj.destroy();
fallingObjects.splice(i, 1);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
} else if (obj.intersects(bucket)) {
// Handle catching object
obj.destroy();
fallingObjects.splice(i, 1);
// Check if the mouth is open
if (bucket.bucketGraphics.visible) {
// Increment score when mouth touches an item
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
} else {
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
}
}
}
// Update power-ups
for (var j = powerUps.length - 1; j >= 0; j--) {
var powerUp = powerUps[j];
powerUp.update();
if (powerUp.y > 2732) {
powerUp.destroy();
powerUps.splice(j, 1);
} else if (powerUp.intersects(bucket)) {
// Handle catching power-up
powerUp.destroy();
powerUps.splice(j, 1);
// Implement power-up effect
}
}
// Update bad items
for (var k = badItems.length - 1; k >= 0; k--) {
var badItem = badItems[k];
badItem.update();
if (badItem.intersects(bucket)) {
badItem.destroy();
badItems.splice(k, 1);
// Decrease life count
lifeCount--;
// Remove a life icon
var lostLife = lifeIcons.pop();
lostLife.destroy();
// Show game over when life count is 0. The game will be automatically paused while game over is showing.
if (lifeCount === 0) {
LK.showGameOver();
}
}
}
// Spawn new falling objects
if (LK.ticks % 60 == 0) {
var newObject = new FallingObject();
newObject.x = Math.random() * 2048;
newObject.y = 0;
fallingObjects.push(newObject);
game.addChild(newObject);
}
// Spawn bad items every few seconds
if (LK.ticks % (80 + Math.floor(Math.random() * 80)) == 0) {
var newBadItem = new BadItem();
newBadItem.x = Math.random() * 2048;
newBadItem.y = 0;
badItems.push(newBadItem);
game.addChild(newBadItem);
}
};
// Display score
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize life count
var lifeCount = 3;
// Create an array to hold the life icons
var lifeIcons = [];
// Display life
for (var i = 0; i < lifeCount; i++) {
var life = LK.getAsset('life', {
anchorX: 1,
anchorY: 0,
x: -i * 130 // Offset each life icon by 130 pixels
});
lifeIcons.push(life);
LK.gui.topRight.addChild(life);
} /****
* Plugins
****/
var facekit = LK.import("@upit/facekit.v1");
/****
* Classes
****/
// Class for Bad Items
var BadItem = Container.expand(function () {
var self = Container.call(this);
var badItemGraphics = self.attachAsset('badItem', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
};
});
// Class for the Bucket (Player)
var Bucket = Container.expand(function () {
var self = Container.call(this);
self.bucketGraphics = self.attachAsset('bucket', {
anchorX: 0.5,
anchorY: 0.5
});
self.bucketGraphics.visible = true;
var closedGraphics = self.attachAsset('closed', {
anchorX: 0.5,
anchorY: 0.5
});
closedGraphics.visible = false;
var bucketGraphics = self.bucketGraphics;
self.update = function () {
self.x = facekit.mouthCenter.x;
self.y = facekit.mouthCenter.y;
if (facekit.mouthOpen) {
bucketGraphics.visible = true;
closedGraphics.visible = false;
} else {
bucketGraphics.visible = false;
closedGraphics.visible = true;
}
};
});
// Class for Falling Objects
var FallingObject = Container.expand(function () {
var self = Container.call(this);
var objectGraphics = self.attachAsset('fallingObject', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.update = function () {
self.y += self.speed;
};
});
// Class for Power-ups
var PowerUp = Container.expand(function () {
var self = Container.call(this);
var powerUpGraphics = self.attachAsset('powerUp', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 3;
self.update = function () {
self.y += self.speed;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
//<Assets used in the game will automatically appear here>
// Initialize game elements
var bucket = game.addChild(new Bucket());
bucket.x = 2048 / 2;
bucket.y = 2500;
var fallingObjects = [];
var powerUps = [];
var badItems = [];
// Handle game move events
game.move = function (x, y, obj) {
bucket.x = x;
};
// Update game logic
game.update = function () {
// Update falling objects
for (var i = fallingObjects.length - 1; i >= 0; i--) {
var obj = fallingObjects[i];
obj.update();
if (obj.y > 2732) {
obj.destroy();
fallingObjects.splice(i, 1);
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
} else if (obj.intersects(bucket)) {
// Handle catching object
obj.destroy();
fallingObjects.splice(i, 1);
// Check if the mouth is open
if (bucket.bucketGraphics.visible) {
// Increment score when mouth touches an item
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
} else {
// Show game over. The game will be automatically paused while game over is showing.
LK.showGameOver();
}
}
}
// Update power-ups
for (var j = powerUps.length - 1; j >= 0; j--) {
var powerUp = powerUps[j];
powerUp.update();
if (powerUp.y > 2732) {
powerUp.destroy();
powerUps.splice(j, 1);
} else if (powerUp.intersects(bucket)) {
// Handle catching power-up
powerUp.destroy();
powerUps.splice(j, 1);
// Implement power-up effect
}
}
// Update bad items
for (var k = badItems.length - 1; k >= 0; k--) {
var badItem = badItems[k];
badItem.update();
if (badItem.intersects(bucket)) {
badItem.destroy();
badItems.splice(k, 1);
// Decrease life count
lifeCount--;
// Remove a life icon
var lostLife = lifeIcons.pop();
lostLife.destroy();
// Show game over when life count is 0. The game will be automatically paused while game over is showing.
if (lifeCount === 0) {
LK.showGameOver();
}
}
}
// Spawn new falling objects
if (LK.ticks % 60 == 0) {
var newObject = new FallingObject();
newObject.x = Math.random() * 2048;
newObject.y = 0;
fallingObjects.push(newObject);
game.addChild(newObject);
}
// Spawn bad items every few seconds
if (LK.ticks % (80 + Math.floor(Math.random() * 80)) == 0) {
var newBadItem = new BadItem();
newBadItem.x = Math.random() * 2048;
newBadItem.y = 0;
badItems.push(newBadItem);
game.addChild(newBadItem);
}
};
// Display score
var scoreTxt = new Text2('0', {
size: 150,
fill: 0xFFFFFF
});
scoreTxt.setText(LK.getScore());
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize life count
var lifeCount = 3;
// Create an array to hold the life icons
var lifeIcons = [];
// Display life
for (var i = 0; i < lifeCount; i++) {
var life = LK.getAsset('life', {
anchorX: 1,
anchorY: 0,
x: -i * 130 // Offset each life icon by 130 pixels
});
lifeIcons.push(life);
LK.gui.topRight.addChild(life);
}