/**** * Classes ****/ // StartButton class var StartButton = Container.expand(function () { var self = Container.call(this); // Initialize start button with a shape asset var startButtonGraphics = self.attachAsset('startButton', { anchorX: 0.5, anchorY: 0.5, shape: 'box', width: 400, height: 200, color: 0x0d6eaf // Blue color }); // Add start text on top of the button var startText = new Text2('START', { size: 80, fill: "#ffffff" }); startText.anchor.set(0.5, 0.5); // Center the text self.addChild(startText); // Add the text to the button // Method to handle start button click self.on('down', function () { self.destroy(); // Destroy start button after click startGame(); // Start the game }); }); // Assets will be automatically created based on usage in the code. // Target class var Target = Container.expand(function () { var self = Container.call(this); // Initialize target with a shape asset var targetGraphics = self.attachAsset('target', { anchorX: 0.5, anchorY: 0.5, shape: 'ellipse', width: 200, // Initial width height: 200, // Initial height color: 0xff0000 // Red color }); // Method to decrease the size of the target self.shrink = function () { if (targetGraphics.width > 20 && targetGraphics.height > 20) { // Minimum size check targetGraphics.width -= 1; targetGraphics.height -= 1; } else { self.destroy(); // Destroy target if it's too small } }; // Method to handle target click self.on('down', function () { LK.setScore(LK.getScore() + 1); // Increase score self.destroy(); // Destroy target after click }); }); // Timer class var Timer = Container.expand(function () { var self = Container.call(this); // Create timer text self.timerText = new Text2('30', { size: 80, fill: "#ffffff" }); self.timerText.anchor.set(0.5, 0.5); self.addChild(self.timerText); // Method to update timer self.update = function (timeLeft) { if (isNaN(timeLeft)) { self.timerText.setText('30'); } else { self.timerText.setText(timeLeft < 10 ? '0' + timeLeft.toString() : timeLeft.toString()); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ // Initialize score and targets array var targets = []; var gameDuration = 30000; // 30 seconds in milliseconds var targetSpawnInterval = 500; // 0.5 seconds in milliseconds var gameEndTime; // Game end time will be set when the game starts var timer = new Timer(); timer.x = 2048 / 2; // Center the timer timer.y = 300; game.addChild(timer); var startButton = new StartButton(); startButton.x = 2048 / 2; // Center the start button startButton.y = 2732 / 2; game.addChild(startButton); // Create score display var scoreTxt = new Text2('0', { size: 180, fill: "#ffffff" }); scoreTxt.x = 2048 / 2 - 75; // Center the score text horizontally scoreTxt.y = 0; // Position the score text 100 pixels from the top game.addChild(scoreTxt); // Add score display to the game // Function to spawn targets function startGame() { gameEndTime = Date.now() + gameDuration; // Calculate game end time } function spawnTarget() { if (Date.now() < gameEndTime) { // Check if game time is not over var target = new Target(); // Random position within game area target.x = Math.random() * (2048 - 100) + 50; // Avoid spawning on the edges target.y = Math.random() * (2732 - 100) + 50; // Avoid spawning on the edges game.addChild(target); targets.push(target); } } // Game tick event LK.on('tick', function () { // Update score display var score = LK.getScore(); scoreTxt.setText(score < 10 ? '0' + score.toString() : score.toString()); // Shrink targets targets.forEach(function (target, index) { target.shrink(); if (!target.parent) { // Check if target has been destroyed targets.splice(index, 1); // Remove target from array } }); // Update timer var timeLeft = Math.ceil((gameEndTime - Date.now()) / 1000); timer.update(timeLeft); // End game when time is up or when timer hits 0 if (Date.now() >= gameEndTime || timeLeft <= 0) { LK.showGameOver(); } }); // Spawn targets at intervals var spawnTimer = LK.setInterval(spawnTarget, targetSpawnInterval); // Clear interval on game over to stop spawning targets LK.on('gameOver', function () { LK.clearInterval(spawnTimer); });
/****
* Classes
****/
// StartButton class
var StartButton = Container.expand(function () {
var self = Container.call(this);
// Initialize start button with a shape asset
var startButtonGraphics = self.attachAsset('startButton', {
anchorX: 0.5,
anchorY: 0.5,
shape: 'box',
width: 400,
height: 200,
color: 0x0d6eaf // Blue color
});
// Add start text on top of the button
var startText = new Text2('START', {
size: 80,
fill: "#ffffff"
});
startText.anchor.set(0.5, 0.5); // Center the text
self.addChild(startText); // Add the text to the button
// Method to handle start button click
self.on('down', function () {
self.destroy(); // Destroy start button after click
startGame(); // Start the game
});
});
// Assets will be automatically created based on usage in the code.
// Target class
var Target = Container.expand(function () {
var self = Container.call(this);
// Initialize target with a shape asset
var targetGraphics = self.attachAsset('target', {
anchorX: 0.5,
anchorY: 0.5,
shape: 'ellipse',
width: 200,
// Initial width
height: 200,
// Initial height
color: 0xff0000 // Red color
});
// Method to decrease the size of the target
self.shrink = function () {
if (targetGraphics.width > 20 && targetGraphics.height > 20) {
// Minimum size check
targetGraphics.width -= 1;
targetGraphics.height -= 1;
} else {
self.destroy(); // Destroy target if it's too small
}
};
// Method to handle target click
self.on('down', function () {
LK.setScore(LK.getScore() + 1); // Increase score
self.destroy(); // Destroy target after click
});
});
// Timer class
var Timer = Container.expand(function () {
var self = Container.call(this);
// Create timer text
self.timerText = new Text2('30', {
size: 80,
fill: "#ffffff"
});
self.timerText.anchor.set(0.5, 0.5);
self.addChild(self.timerText);
// Method to update timer
self.update = function (timeLeft) {
if (isNaN(timeLeft)) {
self.timerText.setText('30');
} else {
self.timerText.setText(timeLeft < 10 ? '0' + timeLeft.toString() : timeLeft.toString());
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Initialize score and targets array
var targets = [];
var gameDuration = 30000; // 30 seconds in milliseconds
var targetSpawnInterval = 500; // 0.5 seconds in milliseconds
var gameEndTime; // Game end time will be set when the game starts
var timer = new Timer();
timer.x = 2048 / 2; // Center the timer
timer.y = 300;
game.addChild(timer);
var startButton = new StartButton();
startButton.x = 2048 / 2; // Center the start button
startButton.y = 2732 / 2;
game.addChild(startButton);
// Create score display
var scoreTxt = new Text2('0', {
size: 180,
fill: "#ffffff"
});
scoreTxt.x = 2048 / 2 - 75; // Center the score text horizontally
scoreTxt.y = 0; // Position the score text 100 pixels from the top
game.addChild(scoreTxt); // Add score display to the game
// Function to spawn targets
function startGame() {
gameEndTime = Date.now() + gameDuration; // Calculate game end time
}
function spawnTarget() {
if (Date.now() < gameEndTime) {
// Check if game time is not over
var target = new Target();
// Random position within game area
target.x = Math.random() * (2048 - 100) + 50; // Avoid spawning on the edges
target.y = Math.random() * (2732 - 100) + 50; // Avoid spawning on the edges
game.addChild(target);
targets.push(target);
}
}
// Game tick event
LK.on('tick', function () {
// Update score display
var score = LK.getScore();
scoreTxt.setText(score < 10 ? '0' + score.toString() : score.toString());
// Shrink targets
targets.forEach(function (target, index) {
target.shrink();
if (!target.parent) {
// Check if target has been destroyed
targets.splice(index, 1); // Remove target from array
}
});
// Update timer
var timeLeft = Math.ceil((gameEndTime - Date.now()) / 1000);
timer.update(timeLeft);
// End game when time is up or when timer hits 0
if (Date.now() >= gameEndTime || timeLeft <= 0) {
LK.showGameOver();
}
});
// Spawn targets at intervals
var spawnTimer = LK.setInterval(spawnTarget, targetSpawnInterval);
// Clear interval on game over to stop spawning targets
LK.on('gameOver', function () {
LK.clearInterval(spawnTimer);
});