/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); var storage = LK.import("@upit/storage.v1", { highScore: 0 }); /**** * Classes ****/ var GameQuestion = Container.expand(function (character, correctShow, options) { var self = Container.call(this); var questionBox = self.attachAsset('questionBox', { anchorX: 0.5, anchorY: 0.5 }); var questionText = new Text2("Which show is " + character + " from?", { size: 90, fill: 0xFFFFFF }); questionText.anchor.set(0.5, 0.5); self.addChild(questionText); self.character = character; self.correctShow = correctShow; self.options = options; return self; }); var Option = Container.expand(function (optionText, optionId) { var self = Container.call(this); var optionBox = self.attachAsset('optionBox', { anchorX: 0.5, anchorY: 0.5 }); var text = new Text2(optionText, { size: 70, fill: 0xFFFFFF }); text.anchor.set(0.5, 0.5); self.addChild(text); self.optionId = optionId; self.originalColor = 0x6495ED; self.originalScale = { x: 1, y: 1 }; self.width = optionBox.width; self.height = optionBox.height; self.setSelected = function (selected) { if (selected) { tween(optionBox, { tint: 0x3A75C4 }, { duration: 200 }); tween(self, { scaleX: 1.05, scaleY: 1.05 }, { duration: 200 }); } else { tween(optionBox, { tint: self.originalColor }, { duration: 200 }); tween(self, { scaleX: self.originalScale.x, scaleY: self.originalScale.y }, { duration: 200 }); } }; self.setCorrect = function () { tween(optionBox, { tint: 0x32CD32 }, { duration: 300 }); }; self.setWrong = function () { tween(optionBox, { tint: 0xFF6347 }, { duration: 300 }); }; self.reset = function () { tween(optionBox, { tint: self.originalColor }, { duration: 200 }); tween(self, { scaleX: self.originalScale.x, scaleY: self.originalScale.y }, { duration: 200 }); }; self.down = function (x, y, obj) { self.setSelected(true); }; self.up = function (x, y, obj) { self.setSelected(false); if (gameState === "playing") { checkAnswer(self.optionId); } }; return self; }); var ScoreDisplay = Container.expand(function () { var self = Container.call(this); self.scoreText = new Text2("Score: 0", { size: 70, fill: 0xFFFFFF }); self.scoreText.anchor.set(0, 0); self.addChild(self.scoreText); self.updateScore = function (score) { self.scoreText.setText("Score: " + score); }; return self; }); var Timer = Container.expand(function (duration) { var self = Container.call(this); self.timerBar = self.attachAsset('optionBox', { anchorX: 0, anchorY: 0.5, tint: 0x32CD32 }); self.timerText = new Text2("Time: " + duration, { size: 60, fill: 0xFFFFFF }); self.timerText.anchor.set(0.5, 0.5); self.addChild(self.timerText); self.maxDuration = duration; self.currentTime = duration; self.maxWidth = self.timerBar.width; self.updateTimer = function (time) { self.currentTime = time; self.timerText.setText("Time: " + Math.ceil(time)); // Update timer bar width var percentLeft = time / self.maxDuration; self.timerBar.width = self.maxWidth * percentLeft; // Change color based on time left if (percentLeft > 0.6) { self.timerBar.tint = 0x32CD32; // Green } else if (percentLeft > 0.3) { self.timerBar.tint = 0xFFD700; // Yellow } else { self.timerBar.tint = 0xFF6347; // Red } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x87CEEB // Sky blue background }); /**** * Game Code ****/ // Game data var cartoonData = [{ character: "SpongeBob SquarePants", show: "SpongeBob SquarePants" }, { character: "Patrick Star", show: "SpongeBob SquarePants" }, { character: "Squidward Tentacles", show: "SpongeBob SquarePants" }, { character: "Mickey Mouse", show: "Mickey Mouse Clubhouse" }, { character: "Donald Duck", show: "DuckTales" }, { character: "Goofy", show: "Goof Troop" }, { character: "Bugs Bunny", show: "Looney Tunes" }, { character: "Daffy Duck", show: "Looney Tunes" }, { character: "Tweety Bird", show: "Looney Tunes" }, { character: "Scooby-Doo", show: "Scooby-Doo" }, { character: "Shaggy Rogers", show: "Scooby-Doo" }, { character: "Fred Flintstone", show: "The Flintstones" }, { character: "Barney Rubble", show: "The Flintstones" }, { character: "Homer Simpson", show: "The Simpsons" }, { character: "Bart Simpson", show: "The Simpsons" }, { character: "Lisa Simpson", show: "The Simpsons" }, { character: "Peter Griffin", show: "Family Guy" }, { character: "Stewie Griffin", show: "Family Guy" }, { character: "Brian Griffin", show: "Family Guy" }, { character: "Eric Cartman", show: "South Park" }, { character: "Stan Marsh", show: "South Park" }, { character: "Kyle Broflovski", show: "South Park" }, { character: "Finn the Human", show: "Adventure Time" }, { character: "Jake the Dog", show: "Adventure Time" }, { character: "Princess Bubblegum", show: "Adventure Time" }, { character: "Mordecai", show: "Regular Show" }, { character: "Rigby", show: "Regular Show" }, { character: "Tom", show: "Tom and Jerry" }, { character: "Jerry", show: "Tom and Jerry" }, { character: "Rick Sanchez", show: "Rick and Morty" }, { character: "Morty Smith", show: "Rick and Morty" }, { character: "Dexter", show: "Dexter's Laboratory" }, { character: "Johnny Bravo", show: "Johnny Bravo" }, { character: "The Powerpuff Girls", show: "The Powerpuff Girls" }, { character: "Courage", show: "Courage the Cowardly Dog" }, { character: "Ed, Edd n Eddy", show: "Ed, Edd n Eddy" }, { character: "Samurai Jack", show: "Samurai Jack" }, { character: "Ben Tennyson", show: "Ben 10" }, { character: "Gwen Tennyson", show: "Ben 10" }, { character: "Gumball Watterson", show: "The Amazing World of Gumball" }]; // List of shows for generating options var allShows = []; for (var i = 0; i < cartoonData.length; i++) { if (allShows.indexOf(cartoonData[i].show) === -1) { allShows.push(cartoonData[i].show); } } // Game variables var score = 0; var questionIndex = 0; var currentOptions = []; var correctAnswerIndex = 0; var gameState = "ready"; // ready, playing, gameOver var gameTime = 60; // 60 seconds game time var timeLeft = gameTime; var currentQuestion = null; var optionElements = []; var scoreDisplay = null; var timerDisplay = null; var gameTimer = null; var questionTimer = null; var timePerQuestion = 10; // 10 seconds per question var questionTimeLeft = timePerQuestion; var usedCharacters = []; // Initialize game elements function initGame() { // Reset game variables score = 0; questionIndex = 0; timeLeft = gameTime; questionTimeLeft = timePerQuestion; gameState = "ready"; usedCharacters = []; // Create score display scoreDisplay = new ScoreDisplay(); scoreDisplay.x = 100; scoreDisplay.y = 100; game.addChild(scoreDisplay); scoreDisplay.updateScore(score); // Create timer display timerDisplay = new Timer(timePerQuestion); timerDisplay.x = 1024; timerDisplay.y = 100; game.addChild(timerDisplay); timerDisplay.updateTimer(questionTimeLeft); // Create start game prompt var startPrompt = new Text2("Tap anywhere to start!", { size: 120, fill: 0xFFFFFF }); startPrompt.anchor.set(0.5, 0.5); startPrompt.x = 1024; startPrompt.y = 1366; game.addChild(startPrompt); // Animate the start prompt function pulseStartPrompt() { tween(startPrompt, { alpha: 0.5 }, { duration: 800, easing: tween.sinOut, onFinish: function onFinish() { tween(startPrompt, { alpha: 1 }, { duration: 800, easing: tween.sinIn, onFinish: pulseStartPrompt }); } }); } pulseStartPrompt(); // Store reference to start prompt for removal later game.startPrompt = startPrompt; } // Generate a new question function generateQuestion() { // Clean up previous question if exists if (currentQuestion) { game.removeChild(currentQuestion); for (var i = 0; i < optionElements.length; i++) { game.removeChild(optionElements[i]); } } // Reset arrays currentOptions = []; optionElements = []; // Get a random character that hasn't been used yet var availableCharacters = cartoonData.filter(function (character) { return usedCharacters.indexOf(character.character) === -1; }); // If we've used all characters, reset the used characters list if (availableCharacters.length === 0) { usedCharacters = []; availableCharacters = cartoonData; } var randomIndex = Math.floor(Math.random() * availableCharacters.length); var selectedCharacter = availableCharacters[randomIndex]; usedCharacters.push(selectedCharacter.character); var correctShow = selectedCharacter.show; // Generate 3 wrong options var wrongOptions = allShows.filter(function (show) { return show !== correctShow; }); var shuffledWrongOptions = shuffleArray(wrongOptions); var options = [correctShow, shuffledWrongOptions[0], shuffledWrongOptions[1], shuffledWrongOptions[2]]; options = shuffleArray(options); // Find where the correct answer is correctAnswerIndex = options.indexOf(correctShow); // Create question currentQuestion = new GameQuestion(selectedCharacter.character, correctShow, options); currentQuestion.x = 1024; currentQuestion.y = 400; game.addChild(currentQuestion); // Create options for (var i = 0; i < options.length; i++) { var option = new Option(options[i], i); option.x = i < 2 ? 512 : 1536; option.y = i % 2 === 0 ? 1000 : 1300; game.addChild(option); optionElements.push(option); currentOptions.push(options[i]); } // Reset question timer questionTimeLeft = timePerQuestion; timerDisplay.updateTimer(questionTimeLeft); } // Check if the selected answer is correct function checkAnswer(selectedIndex) { // Prevent multiple answers if (gameState !== "playing") return; // Temporarily pause question timer LK.clearInterval(questionTimer); var isCorrect = selectedIndex === correctAnswerIndex; if (isCorrect) { // Correct answer optionElements[selectedIndex].setCorrect(); score += Math.ceil(questionTimeLeft); // Award points based on time left scoreDisplay.updateScore(score); LK.getSound('correct').play(); } else { // Wrong answer optionElements[selectedIndex].setWrong(); optionElements[correctAnswerIndex].setCorrect(); LK.getSound('wrong').play(); } // Wait a moment before showing the next question LK.setTimeout(function () { if (gameState === "playing") { generateQuestion(); // Resume question timer questionTimer = LK.setInterval(updateQuestionTimer, 100); } }, 1500); } // Update the question timer function updateQuestionTimer() { questionTimeLeft -= 0.1; timerDisplay.updateTimer(questionTimeLeft); if (questionTimeLeft <= 0) { // Time's up for this question LK.clearInterval(questionTimer); // Show correct answer for (var i = 0; i < optionElements.length; i++) { if (i === correctAnswerIndex) { optionElements[i].setCorrect(); } else { optionElements[i].setWrong(); } } LK.getSound('wrong').play(); // Wait a moment before showing the next question LK.setTimeout(function () { if (gameState === "playing") { generateQuestion(); // Resume question timer questionTimer = LK.setInterval(updateQuestionTimer, 100); } }, 1500); } } // Start the game function startGame() { if (gameState !== "ready") return; // Remove the start prompt if (game.startPrompt) { game.removeChild(game.startPrompt); game.startPrompt = null; } gameState = "playing"; // Play background music LK.playMusic('gameMusic', { fade: { start: 0, end: 0.5, duration: 1000 } }); // Generate first question generateQuestion(); // Start question timer questionTimer = LK.setInterval(updateQuestionTimer, 100); // Start game timer gameTimer = LK.setInterval(function () { timeLeft -= 0.1; if (timeLeft <= 0) { endGame(); } }, 100); } // End the game function endGame() { // Clear timers LK.clearInterval(questionTimer); LK.clearInterval(gameTimer); gameState = "gameOver"; // Play game over sound LK.getSound('gameover').play(); LK.stopMusic(); // Update high score if needed if (score > storage.highScore) { storage.highScore = score; } // Show game over LK.setScore(score); LK.showGameOver(); } // Helper function to shuffle an array function shuffleArray(array) { var newArray = array.slice(); for (var i = newArray.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = newArray[i]; newArray[i] = newArray[j]; newArray[j] = temp; } return newArray; } // Handle tap to start game game.down = function (x, y, obj) { if (gameState === "ready") { startGame(); } }; // Initialize game initGame();
===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,539 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1", {
+ highScore: 0
+});
+
+/****
+* Classes
+****/
+var GameQuestion = Container.expand(function (character, correctShow, options) {
+ var self = Container.call(this);
+ var questionBox = self.attachAsset('questionBox', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ var questionText = new Text2("Which show is " + character + " from?", {
+ size: 90,
+ fill: 0xFFFFFF
+ });
+ questionText.anchor.set(0.5, 0.5);
+ self.addChild(questionText);
+ self.character = character;
+ self.correctShow = correctShow;
+ self.options = options;
+ return self;
+});
+var Option = Container.expand(function (optionText, optionId) {
+ var self = Container.call(this);
+ var optionBox = self.attachAsset('optionBox', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ var text = new Text2(optionText, {
+ size: 70,
+ fill: 0xFFFFFF
+ });
+ text.anchor.set(0.5, 0.5);
+ self.addChild(text);
+ self.optionId = optionId;
+ self.originalColor = 0x6495ED;
+ self.originalScale = {
+ x: 1,
+ y: 1
+ };
+ self.width = optionBox.width;
+ self.height = optionBox.height;
+ self.setSelected = function (selected) {
+ if (selected) {
+ tween(optionBox, {
+ tint: 0x3A75C4
+ }, {
+ duration: 200
+ });
+ tween(self, {
+ scaleX: 1.05,
+ scaleY: 1.05
+ }, {
+ duration: 200
+ });
+ } else {
+ tween(optionBox, {
+ tint: self.originalColor
+ }, {
+ duration: 200
+ });
+ tween(self, {
+ scaleX: self.originalScale.x,
+ scaleY: self.originalScale.y
+ }, {
+ duration: 200
+ });
+ }
+ };
+ self.setCorrect = function () {
+ tween(optionBox, {
+ tint: 0x32CD32
+ }, {
+ duration: 300
+ });
+ };
+ self.setWrong = function () {
+ tween(optionBox, {
+ tint: 0xFF6347
+ }, {
+ duration: 300
+ });
+ };
+ self.reset = function () {
+ tween(optionBox, {
+ tint: self.originalColor
+ }, {
+ duration: 200
+ });
+ tween(self, {
+ scaleX: self.originalScale.x,
+ scaleY: self.originalScale.y
+ }, {
+ duration: 200
+ });
+ };
+ self.down = function (x, y, obj) {
+ self.setSelected(true);
+ };
+ self.up = function (x, y, obj) {
+ self.setSelected(false);
+ if (gameState === "playing") {
+ checkAnswer(self.optionId);
+ }
+ };
+ return self;
+});
+var ScoreDisplay = Container.expand(function () {
+ var self = Container.call(this);
+ self.scoreText = new Text2("Score: 0", {
+ size: 70,
+ fill: 0xFFFFFF
+ });
+ self.scoreText.anchor.set(0, 0);
+ self.addChild(self.scoreText);
+ self.updateScore = function (score) {
+ self.scoreText.setText("Score: " + score);
+ };
+ return self;
+});
+var Timer = Container.expand(function (duration) {
+ var self = Container.call(this);
+ self.timerBar = self.attachAsset('optionBox', {
+ anchorX: 0,
+ anchorY: 0.5,
+ tint: 0x32CD32
+ });
+ self.timerText = new Text2("Time: " + duration, {
+ size: 60,
+ fill: 0xFFFFFF
+ });
+ self.timerText.anchor.set(0.5, 0.5);
+ self.addChild(self.timerText);
+ self.maxDuration = duration;
+ self.currentTime = duration;
+ self.maxWidth = self.timerBar.width;
+ self.updateTimer = function (time) {
+ self.currentTime = time;
+ self.timerText.setText("Time: " + Math.ceil(time));
+ // Update timer bar width
+ var percentLeft = time / self.maxDuration;
+ self.timerBar.width = self.maxWidth * percentLeft;
+ // Change color based on time left
+ if (percentLeft > 0.6) {
+ self.timerBar.tint = 0x32CD32; // Green
+ } else if (percentLeft > 0.3) {
+ self.timerBar.tint = 0xFFD700; // Yellow
+ } else {
+ self.timerBar.tint = 0xFF6347; // Red
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB // Sky blue background
+});
+
+/****
+* Game Code
+****/
+// Game data
+var cartoonData = [{
+ character: "SpongeBob SquarePants",
+ show: "SpongeBob SquarePants"
+}, {
+ character: "Patrick Star",
+ show: "SpongeBob SquarePants"
+}, {
+ character: "Squidward Tentacles",
+ show: "SpongeBob SquarePants"
+}, {
+ character: "Mickey Mouse",
+ show: "Mickey Mouse Clubhouse"
+}, {
+ character: "Donald Duck",
+ show: "DuckTales"
+}, {
+ character: "Goofy",
+ show: "Goof Troop"
+}, {
+ character: "Bugs Bunny",
+ show: "Looney Tunes"
+}, {
+ character: "Daffy Duck",
+ show: "Looney Tunes"
+}, {
+ character: "Tweety Bird",
+ show: "Looney Tunes"
+}, {
+ character: "Scooby-Doo",
+ show: "Scooby-Doo"
+}, {
+ character: "Shaggy Rogers",
+ show: "Scooby-Doo"
+}, {
+ character: "Fred Flintstone",
+ show: "The Flintstones"
+}, {
+ character: "Barney Rubble",
+ show: "The Flintstones"
+}, {
+ character: "Homer Simpson",
+ show: "The Simpsons"
+}, {
+ character: "Bart Simpson",
+ show: "The Simpsons"
+}, {
+ character: "Lisa Simpson",
+ show: "The Simpsons"
+}, {
+ character: "Peter Griffin",
+ show: "Family Guy"
+}, {
+ character: "Stewie Griffin",
+ show: "Family Guy"
+}, {
+ character: "Brian Griffin",
+ show: "Family Guy"
+}, {
+ character: "Eric Cartman",
+ show: "South Park"
+}, {
+ character: "Stan Marsh",
+ show: "South Park"
+}, {
+ character: "Kyle Broflovski",
+ show: "South Park"
+}, {
+ character: "Finn the Human",
+ show: "Adventure Time"
+}, {
+ character: "Jake the Dog",
+ show: "Adventure Time"
+}, {
+ character: "Princess Bubblegum",
+ show: "Adventure Time"
+}, {
+ character: "Mordecai",
+ show: "Regular Show"
+}, {
+ character: "Rigby",
+ show: "Regular Show"
+}, {
+ character: "Tom",
+ show: "Tom and Jerry"
+}, {
+ character: "Jerry",
+ show: "Tom and Jerry"
+}, {
+ character: "Rick Sanchez",
+ show: "Rick and Morty"
+}, {
+ character: "Morty Smith",
+ show: "Rick and Morty"
+}, {
+ character: "Dexter",
+ show: "Dexter's Laboratory"
+}, {
+ character: "Johnny Bravo",
+ show: "Johnny Bravo"
+}, {
+ character: "The Powerpuff Girls",
+ show: "The Powerpuff Girls"
+}, {
+ character: "Courage",
+ show: "Courage the Cowardly Dog"
+}, {
+ character: "Ed, Edd n Eddy",
+ show: "Ed, Edd n Eddy"
+}, {
+ character: "Samurai Jack",
+ show: "Samurai Jack"
+}, {
+ character: "Ben Tennyson",
+ show: "Ben 10"
+}, {
+ character: "Gwen Tennyson",
+ show: "Ben 10"
+}, {
+ character: "Gumball Watterson",
+ show: "The Amazing World of Gumball"
+}];
+// List of shows for generating options
+var allShows = [];
+for (var i = 0; i < cartoonData.length; i++) {
+ if (allShows.indexOf(cartoonData[i].show) === -1) {
+ allShows.push(cartoonData[i].show);
+ }
+}
+// Game variables
+var score = 0;
+var questionIndex = 0;
+var currentOptions = [];
+var correctAnswerIndex = 0;
+var gameState = "ready"; // ready, playing, gameOver
+var gameTime = 60; // 60 seconds game time
+var timeLeft = gameTime;
+var currentQuestion = null;
+var optionElements = [];
+var scoreDisplay = null;
+var timerDisplay = null;
+var gameTimer = null;
+var questionTimer = null;
+var timePerQuestion = 10; // 10 seconds per question
+var questionTimeLeft = timePerQuestion;
+var usedCharacters = [];
+// Initialize game elements
+function initGame() {
+ // Reset game variables
+ score = 0;
+ questionIndex = 0;
+ timeLeft = gameTime;
+ questionTimeLeft = timePerQuestion;
+ gameState = "ready";
+ usedCharacters = [];
+ // Create score display
+ scoreDisplay = new ScoreDisplay();
+ scoreDisplay.x = 100;
+ scoreDisplay.y = 100;
+ game.addChild(scoreDisplay);
+ scoreDisplay.updateScore(score);
+ // Create timer display
+ timerDisplay = new Timer(timePerQuestion);
+ timerDisplay.x = 1024;
+ timerDisplay.y = 100;
+ game.addChild(timerDisplay);
+ timerDisplay.updateTimer(questionTimeLeft);
+ // Create start game prompt
+ var startPrompt = new Text2("Tap anywhere to start!", {
+ size: 120,
+ fill: 0xFFFFFF
+ });
+ startPrompt.anchor.set(0.5, 0.5);
+ startPrompt.x = 1024;
+ startPrompt.y = 1366;
+ game.addChild(startPrompt);
+ // Animate the start prompt
+ function pulseStartPrompt() {
+ tween(startPrompt, {
+ alpha: 0.5
+ }, {
+ duration: 800,
+ easing: tween.sinOut,
+ onFinish: function onFinish() {
+ tween(startPrompt, {
+ alpha: 1
+ }, {
+ duration: 800,
+ easing: tween.sinIn,
+ onFinish: pulseStartPrompt
+ });
+ }
+ });
+ }
+ pulseStartPrompt();
+ // Store reference to start prompt for removal later
+ game.startPrompt = startPrompt;
+}
+// Generate a new question
+function generateQuestion() {
+ // Clean up previous question if exists
+ if (currentQuestion) {
+ game.removeChild(currentQuestion);
+ for (var i = 0; i < optionElements.length; i++) {
+ game.removeChild(optionElements[i]);
+ }
+ }
+ // Reset arrays
+ currentOptions = [];
+ optionElements = [];
+ // Get a random character that hasn't been used yet
+ var availableCharacters = cartoonData.filter(function (character) {
+ return usedCharacters.indexOf(character.character) === -1;
+ });
+ // If we've used all characters, reset the used characters list
+ if (availableCharacters.length === 0) {
+ usedCharacters = [];
+ availableCharacters = cartoonData;
+ }
+ var randomIndex = Math.floor(Math.random() * availableCharacters.length);
+ var selectedCharacter = availableCharacters[randomIndex];
+ usedCharacters.push(selectedCharacter.character);
+ var correctShow = selectedCharacter.show;
+ // Generate 3 wrong options
+ var wrongOptions = allShows.filter(function (show) {
+ return show !== correctShow;
+ });
+ var shuffledWrongOptions = shuffleArray(wrongOptions);
+ var options = [correctShow, shuffledWrongOptions[0], shuffledWrongOptions[1], shuffledWrongOptions[2]];
+ options = shuffleArray(options);
+ // Find where the correct answer is
+ correctAnswerIndex = options.indexOf(correctShow);
+ // Create question
+ currentQuestion = new GameQuestion(selectedCharacter.character, correctShow, options);
+ currentQuestion.x = 1024;
+ currentQuestion.y = 400;
+ game.addChild(currentQuestion);
+ // Create options
+ for (var i = 0; i < options.length; i++) {
+ var option = new Option(options[i], i);
+ option.x = i < 2 ? 512 : 1536;
+ option.y = i % 2 === 0 ? 1000 : 1300;
+ game.addChild(option);
+ optionElements.push(option);
+ currentOptions.push(options[i]);
+ }
+ // Reset question timer
+ questionTimeLeft = timePerQuestion;
+ timerDisplay.updateTimer(questionTimeLeft);
+}
+// Check if the selected answer is correct
+function checkAnswer(selectedIndex) {
+ // Prevent multiple answers
+ if (gameState !== "playing") return;
+ // Temporarily pause question timer
+ LK.clearInterval(questionTimer);
+ var isCorrect = selectedIndex === correctAnswerIndex;
+ if (isCorrect) {
+ // Correct answer
+ optionElements[selectedIndex].setCorrect();
+ score += Math.ceil(questionTimeLeft); // Award points based on time left
+ scoreDisplay.updateScore(score);
+ LK.getSound('correct').play();
+ } else {
+ // Wrong answer
+ optionElements[selectedIndex].setWrong();
+ optionElements[correctAnswerIndex].setCorrect();
+ LK.getSound('wrong').play();
+ }
+ // Wait a moment before showing the next question
+ LK.setTimeout(function () {
+ if (gameState === "playing") {
+ generateQuestion();
+ // Resume question timer
+ questionTimer = LK.setInterval(updateQuestionTimer, 100);
+ }
+ }, 1500);
+}
+// Update the question timer
+function updateQuestionTimer() {
+ questionTimeLeft -= 0.1;
+ timerDisplay.updateTimer(questionTimeLeft);
+ if (questionTimeLeft <= 0) {
+ // Time's up for this question
+ LK.clearInterval(questionTimer);
+ // Show correct answer
+ for (var i = 0; i < optionElements.length; i++) {
+ if (i === correctAnswerIndex) {
+ optionElements[i].setCorrect();
+ } else {
+ optionElements[i].setWrong();
+ }
+ }
+ LK.getSound('wrong').play();
+ // Wait a moment before showing the next question
+ LK.setTimeout(function () {
+ if (gameState === "playing") {
+ generateQuestion();
+ // Resume question timer
+ questionTimer = LK.setInterval(updateQuestionTimer, 100);
+ }
+ }, 1500);
+ }
+}
+// Start the game
+function startGame() {
+ if (gameState !== "ready") return;
+ // Remove the start prompt
+ if (game.startPrompt) {
+ game.removeChild(game.startPrompt);
+ game.startPrompt = null;
+ }
+ gameState = "playing";
+ // Play background music
+ LK.playMusic('gameMusic', {
+ fade: {
+ start: 0,
+ end: 0.5,
+ duration: 1000
+ }
+ });
+ // Generate first question
+ generateQuestion();
+ // Start question timer
+ questionTimer = LK.setInterval(updateQuestionTimer, 100);
+ // Start game timer
+ gameTimer = LK.setInterval(function () {
+ timeLeft -= 0.1;
+ if (timeLeft <= 0) {
+ endGame();
+ }
+ }, 100);
+}
+// End the game
+function endGame() {
+ // Clear timers
+ LK.clearInterval(questionTimer);
+ LK.clearInterval(gameTimer);
+ gameState = "gameOver";
+ // Play game over sound
+ LK.getSound('gameover').play();
+ LK.stopMusic();
+ // Update high score if needed
+ if (score > storage.highScore) {
+ storage.highScore = score;
+ }
+ // Show game over
+ LK.setScore(score);
+ LK.showGameOver();
+}
+// Helper function to shuffle an array
+function shuffleArray(array) {
+ var newArray = array.slice();
+ for (var i = newArray.length - 1; i > 0; i--) {
+ var j = Math.floor(Math.random() * (i + 1));
+ var temp = newArray[i];
+ newArray[i] = newArray[j];
+ newArray[j] = temp;
+ }
+ return newArray;
+}
+// Handle tap to start game
+game.down = function (x, y, obj) {
+ if (gameState === "ready") {
+ startGame();
+ }
+};
+// Initialize game
+initGame();
\ No newline at end of file