/****
* Classes
****/
// Assets are automatically created based on usage in the code.
// Ball class
var Ball = Container.expand(function () {
var self = Container.call(this);
// Attach the white ball asset
var ballGraphics = self.attachAsset('whiteBall', {
anchorX: 0.5,
anchorY: 0.5
});
// Initial position of the ball will be set when it's added to the game
self.x = 0;
self.y = 0;
});
var BlackSquare = Container.expand(function () {
var self = Container.call(this);
var squareGraphics = self.attachAsset('blackSquare', {
anchorX: 0.5,
anchorY: 0.5
});
self.x = 0;
self.y = 0;
});
var TimerBox = Container.expand(function () {
var self = Container.call(this);
var boxGraphics = self.attachAsset('timerBox', {
anchorX: 0.5,
anchorY: 0.5,
color: 0x0000FF
});
self.x = 0;
self.y = 0;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xFFE08C // Set background color to yellow-orange
});
/****
* Game Code
****/
// Removed the reference to the undefined variable 'blueSquares'
LK.on('tick', function () {
// Decrease the timer by 1 every 60 frames (1 second)
if (LK.ticks % 60 == 0) {
timer--;
// If the timer reaches 0, end the game
if (timer <= 0) {
LK.showGameOver();
} else {
// Update the timer display
timerTxt.setText(timer.toString());
}
}
for (var i = blackSquares.length - 1; i >= 0; i--) {
if (whiteBall.intersects(blackSquares[i])) {
blackSquares[i].destroy();
blackSquares.splice(i, 1);
// Update the score
LK.setScore(LK.getScore() + 1);
// Update the score display
scoreTxt.setText(LK.getScore());
// Change the color of the black squares to another color except yellow every hundred points
if (LK.getScore() % 100 == 0) {
var colors = [0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0x00FFFF, 0xFFFFFF];
var randomColor = colors[Math.floor(Math.random() * colors.length)];
for (var i = 0; i < blackSquares.length; i++) {
blackSquares[i].asset.color = randomColor;
}
}
}
}
if (blackSquares.length === 0) {
for (var i = 0; i < 50; i++) {
var blackSquare = game.addChild(new BlackSquare());
blackSquare.x = Math.random() * 2048;
blackSquare.y = Math.random() * 2732;
blackSquares.push(blackSquare);
}
// Reset the timer to 10 seconds
timer = 10;
timerTxt.setText(timer.toString());
}
});
var whiteBall = game.addChild(new Ball());
whiteBall.x = 2048 / 2;
whiteBall.y = 2732 / 2;
// Create an array to hold multiple black squares
var blackSquares = [];
for (var i = 0; i < 50; i++) {
var blackSquare = game.addChild(new BlackSquare());
blackSquare.x = Math.random() * 2048;
blackSquare.y = Math.random() * 2732;
blackSquares.push(blackSquare);
}
// Create an array to hold multiple blue squares
// Create an array to hold multiple red squares
// Function to handle drag movement
function handleDrag(obj) {
var event = obj.event;
var pos = event.getLocalPosition(game);
whiteBall.x = pos.x;
whiteBall.y = pos.y;
}
// Add event listener for drag movement
game.on('down', function (obj) {
handleDrag(obj); // Start dragging on touch down
game.on('move', handleDrag); // Continue dragging on move
});
// Stop dragging when the touch ends
game.on('up', function (obj) {
game.off('move', handleDrag); // Remove move listener to stop dragging
});
// Create a score display
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
// Initialize a timer variable to keep track of the time left
var timer = 10;
var timerBox = game.addChild(new TimerBox());
timerBox.x = 2048 - timerBox.width;
timerBox.y = 50; // Move the timer box a little lower
scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text.
LK.gui.top.addChild(scoreTxt); // Add the score text to the GUI overlay.
// Create a timer display
var timerTxt = new Text2(timer.toString(), {
size: 150,
fill: "#ff0000"
});
timerTxt.anchor.set(0.5, 0.5); // Sets anchor to the center of the text.
timerBox.addChild(timerTxt); // Add the timer text to the timer box.
// No need to handle game tick explicitly as the ball movement is handled via drag events