Code edit (3 edits merged)
Please save this source code
User prompt
place the timer under the score
User prompt
add a start text on top of the button, center it on the button
Code edit (1 edits merged)
Please save this source code
User prompt
if count down is NaN display 30
User prompt
if count down is NaN display 0
User prompt
if the count down hits 0 game over
User prompt
make the timer an text object
User prompt
add a count down from 30 seconds that starts on game start
User prompt
add a start game button
User prompt
fix the timer so that it updates
User prompt
the timers beginning value should be 30 seconds
User prompt
the timer should use the duration of the var gameduration
User prompt
set the timer to 30 seconds
User prompt
the timer should be a text element
User prompt
expand the game
Code edit (1 edits merged)
Please save this source code
Initial prompt
aim trainer v2
/**** * 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 }); // 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: 50, fill: "#ffffff" }); self.timerText.anchor.set(0.5, 0.5); self.addChild(self.timerText); // Method to update timer self.update = function (timeLeft) { self.timerText.setText(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 = 2732 / 4; 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: 150, fill: "#ffffff" }); LK.gui.top.addChild(scoreTxt); // Add score display to the top-center of the screen // 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 scoreTxt.setText(LK.getScore().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); });
===================================================================
--- original.js
+++ change.js
@@ -124,10 +124,10 @@
});
// Update timer
var timeLeft = Math.ceil((gameEndTime - Date.now()) / 1000);
timer.update(timeLeft);
- // End game when time is up
- if (Date.now() >= gameEndTime) {
+ // End game when time is up or when timer hits 0
+ if (Date.now() >= gameEndTime || timeLeft <= 0) {
LK.showGameOver();
}
});
// Spawn targets at intervals