/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// Define a simple Button class that will be used for the tap target
var Button = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
self.update = function () {
// Add a swinging animation to the button
self.rotation = Math.sin(LK.ticks / 10) / 10;
};
// Add a cursor to the button
self.cursor = 'pointer';
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize score, high score, timer and score text
var score = 0;
var highScore = 0;
var timer = 120 * 60; // 120 seconds * 60 frames per second
var scoreTxt = new Text2('0', {
size: 300,
fill: "#ffffff",
font: "'.VnAvant', serif",
fontWeight: 'bold'
});
scoreTxt.anchor.set(0.5, 1);
LK.gui.bottom.addChild(scoreTxt);
// Create the timer text and position it above the high score text
var timerTxt = new Text2('Time: 0', {
size: 300,
fill: "#ffffff",
font: "'Bahnschrift', serif",
fontWeight: 'bold'
});
timerTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(timerTxt);
timerTxt.y = 0;
// Create the button and position it at the center of the screen
var background = game.addChild(LK.getAsset('background', {
anchorX: 0,
anchorY: 0
}));
var button = game.addChild(new Button());
button.x = 2048 / 2;
button.y = 2732 / 2;
// Update score function
function updateScore() {
score += 1;
scoreTxt.setText(score);
}
// Handle button press
button.down = function (x, y, obj) {
updateScore();
if (score > highScore) {
highScore = score;
highScoreTxt.setText(highScore);
}
// Flash the button to give feedback
LK.effects.flashObject(button, 0xff0000, 100);
// Play a sound when the button is clicked
LK.getSound('clicksound').play();
};
// Game update function
game.update = function () {
// Decrease the timer every game tick
if (timer > 0) {
timer--;
} else {
// End the game when the timer reaches 0
LK.showGameOver();
}
// Update the timer text
var minutes = Math.floor(timer / 3600);
var seconds = Math.floor(timer % 3600 / 60);
timerTxt.setText(minutes + ":" + (seconds < 10 ? "0" : "") + seconds);
// Change the color of the timer text to red when the timer is 15 seconds or lower
if (minutes == 0 && seconds <= 15) {
timerTxt.fill = "#ff0000";
} else {
timerTxt.fill = "#ffffff";
}
};