/**** * Classes ****/ // Assets will be automatically created and loaded by the LK engine based on usage in the game code. // Question class for displaying trivia questions and handling user interaction var Question = Container.expand(function () { var self = Container.call(this); questionText = new Text2('', { size: 100, fill: "#ffffff" }); questionText.anchor.set(0.5, 0); questionText.x = 1024; // Center horizontally questionText.y = 200; // Position from top self.addChild(questionText); var answerOptions = []; // Array to hold answer option assets // Initialize question and answers self.init = function (question, answers) { questionText.setText(''); // Clear previous question text self.cleanup(); // Clean up previous question and answers // Clear previous question text before setting new question questionText.setText(question); // Set new question text // Create and position answer options answers.forEach(function (answer, index) { var answerHitbox = self.attachAsset('answerOption', { x: 1024, y: 600 + index * 200, anchorX: 0.5, anchorY: 0.5 }); answerOption = new Text2(answer.text, { size: 100, fill: "#ffffff" }); answerOption.anchor.set(0.5, 0.5); answerOption.x = 1024; answerOption.y = 600 + index * 200; self.addChild(answerOption); answerOption.data = answer; // Attach answer data to the option answerOptions.push({ text: answerOption, hitbox: answerHitbox }); // Handle answer selection answerOption.down = function (x, y, obj) { questionText.setText(''); // Clear question text immediately after an answer is selected self.checkAnswer(answerOption.data.isCorrect); }; }); }; // Check if the selected answer is correct self.checkAnswer = function (isCorrect) { if (isCorrect) { LK.effects.flashObject(self, 0xffcccc, 500); // Flash light red for correct answer LK.setScore(LK.getScore() + 1); // Increase score // Moved cleanup to the beginning of displayNextQuestion to ensure the previous question is cleared before displaying the next one displayNextQuestion(); // Proceed to the next question } else { LK.effects.flashObject(self, 0xff0000, 500); // Flash red for incorrect answer // Provide feedback for incorrect answer and wait before moving to the next question LK.setTimeout(function () { self.cleanup(); // Clean up the current question displayNextQuestion(); // Proceed to the next question even if the answer is incorrect }, 1000); // Wait for 1 second to allow the player to see the feedback } // Prepare for next question or end game }; // Clean up for the next question self.cleanup = function () { answerOptions.forEach(function (option) { option.text.destroy(); option.hitbox.destroy(); }); answerOptions = []; }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ var questions = [{ question: "What's Taylor Swift's debut album name?", answers: [{ text: "Taylor Swift", isCorrect: true }, { text: "Fearless", isCorrect: false }, { text: "Speak Now", isCorrect: false }, { text: "Red", isCorrect: false }] }, { question: "What is Taylor Swift's birth date?", answers: [{ text: "December 13, 1989", isCorrect: true }, { text: "January 31, 1989", isCorrect: false }, { text: "November 23, 1989", isCorrect: false }, { text: "October 10, 1989", isCorrect: false }] }, { question: "What is the name of Taylor Swift's first single?", answers: [{ text: "Tim McGraw", isCorrect: true }, { text: "Love Story", isCorrect: false }, { text: "You Belong with Me", isCorrect: false }, { text: "Our Song", isCorrect: false }] }, { question: "Which album includes the song 'Bad Blood'?", answers: [{ text: "1989", isCorrect: true }, { text: "Reputation", isCorrect: false }, { text: "Red", isCorrect: false }, { text: "Lover", isCorrect: false }] }, { question: "What year did Taylor Swift win her first Grammy Award?", answers: [{ text: "2008", isCorrect: false }, { text: "2010", isCorrect: true }, { text: "2012", isCorrect: false }, { text: "2014", isCorrect: false }] }, { question: "What is the name of Taylor Swift's cat?", answers: [{ text: "Meredith", isCorrect: true }, { text: "Olivia", isCorrect: false }, { text: "Benjamin", isCorrect: false }, { text: "All of the above", isCorrect: false }] }, { question: "What is the name of Taylor Swift's 2020 documentary?", answers: [{ text: "Miss Americana", isCorrect: true }, { text: "The Swift Life", isCorrect: false }, { text: "Taylor Swift: Journey to Fearless", isCorrect: false }, { text: "The Taylor Swift Experience", isCorrect: false }] }, { question: "What is the name of Taylor Swift's 2020 album?", answers: [{ text: "Folklore", isCorrect: true }, { text: "Evermore", isCorrect: false }, { text: "Lover", isCorrect: false }, { text: "Reputation", isCorrect: false }] }, { question: "What is the name of Taylor Swift's 2019 album?", answers: [{ text: "Lover", isCorrect: true }, { text: "1989", isCorrect: false }, { text: "Red", isCorrect: false }, { text: "Speak Now", isCorrect: false }] }, { question: "What is the name of Taylor Swift's 2017 album?", answers: [{ text: "Reputation", isCorrect: true }, { text: "1989", isCorrect: false }, { text: "Red", isCorrect: false }, { text: "Speak Now", isCorrect: false }] }, { question: "What is the name of Taylor Swift's 2014 album?", answers: [{ text: "1989", isCorrect: true }, { text: "Red", isCorrect: false }, { text: "Speak Now", isCorrect: false }, { text: "Fearless", isCorrect: false }] }, { question: "What is the name of Taylor Swift's 2012 album?", answers: [{ text: "Red", isCorrect: true }, { text: "1989", isCorrect: false }, { text: "Speak Now", isCorrect: false }, { text: "Fearless", isCorrect: false }] }].sort(function () { return Math.random() - 0.5; }); var currentQuestionIndex = 0; var scoreTxt; function displayNextQuestion() { if (currentQuestionIndex < questions.length) { var questionData = questions[currentQuestionIndex]; var question = game.addChild(new Question()); question.init(questionData.question, questionData.answers); currentQuestionIndex++; } else { // No more questions, show game over LK.showGameOver(); } } function setupGame() { // Display score scoreTxt = new Text2('Score: 0', { size: 100, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); scoreTxt.x = 1024; // Center horizontally scoreTxt.y = 50; // Position from top LK.gui.top.addChild(scoreTxt); // Start with the first question displayNextQuestion(); } // Update score display game.update = function () { scoreTxt.setText('Score: ' + LK.getScore()); }; setupGame();
/****
* Classes
****/
// Assets will be automatically created and loaded by the LK engine based on usage in the game code.
// Question class for displaying trivia questions and handling user interaction
var Question = Container.expand(function () {
var self = Container.call(this);
questionText = new Text2('', {
size: 100,
fill: "#ffffff"
});
questionText.anchor.set(0.5, 0);
questionText.x = 1024; // Center horizontally
questionText.y = 200; // Position from top
self.addChild(questionText);
var answerOptions = []; // Array to hold answer option assets
// Initialize question and answers
self.init = function (question, answers) {
questionText.setText(''); // Clear previous question text
self.cleanup(); // Clean up previous question and answers
// Clear previous question text before setting new question
questionText.setText(question); // Set new question text
// Create and position answer options
answers.forEach(function (answer, index) {
var answerHitbox = self.attachAsset('answerOption', {
x: 1024,
y: 600 + index * 200,
anchorX: 0.5,
anchorY: 0.5
});
answerOption = new Text2(answer.text, {
size: 100,
fill: "#ffffff"
});
answerOption.anchor.set(0.5, 0.5);
answerOption.x = 1024;
answerOption.y = 600 + index * 200;
self.addChild(answerOption);
answerOption.data = answer; // Attach answer data to the option
answerOptions.push({
text: answerOption,
hitbox: answerHitbox
});
// Handle answer selection
answerOption.down = function (x, y, obj) {
questionText.setText(''); // Clear question text immediately after an answer is selected
self.checkAnswer(answerOption.data.isCorrect);
};
});
};
// Check if the selected answer is correct
self.checkAnswer = function (isCorrect) {
if (isCorrect) {
LK.effects.flashObject(self, 0xffcccc, 500); // Flash light red for correct answer
LK.setScore(LK.getScore() + 1); // Increase score
// Moved cleanup to the beginning of displayNextQuestion to ensure the previous question is cleared before displaying the next one
displayNextQuestion(); // Proceed to the next question
} else {
LK.effects.flashObject(self, 0xff0000, 500); // Flash red for incorrect answer
// Provide feedback for incorrect answer and wait before moving to the next question
LK.setTimeout(function () {
self.cleanup(); // Clean up the current question
displayNextQuestion(); // Proceed to the next question even if the answer is incorrect
}, 1000); // Wait for 1 second to allow the player to see the feedback
}
// Prepare for next question or end game
};
// Clean up for the next question
self.cleanup = function () {
answerOptions.forEach(function (option) {
option.text.destroy();
option.hitbox.destroy();
});
answerOptions = [];
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
// Init game with black background
});
/****
* Game Code
****/
var questions = [{
question: "What's Taylor Swift's debut album name?",
answers: [{
text: "Taylor Swift",
isCorrect: true
}, {
text: "Fearless",
isCorrect: false
}, {
text: "Speak Now",
isCorrect: false
}, {
text: "Red",
isCorrect: false
}]
}, {
question: "What is Taylor Swift's birth date?",
answers: [{
text: "December 13, 1989",
isCorrect: true
}, {
text: "January 31, 1989",
isCorrect: false
}, {
text: "November 23, 1989",
isCorrect: false
}, {
text: "October 10, 1989",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's first single?",
answers: [{
text: "Tim McGraw",
isCorrect: true
}, {
text: "Love Story",
isCorrect: false
}, {
text: "You Belong with Me",
isCorrect: false
}, {
text: "Our Song",
isCorrect: false
}]
}, {
question: "Which album includes the song 'Bad Blood'?",
answers: [{
text: "1989",
isCorrect: true
}, {
text: "Reputation",
isCorrect: false
}, {
text: "Red",
isCorrect: false
}, {
text: "Lover",
isCorrect: false
}]
}, {
question: "What year did Taylor Swift win her first Grammy Award?",
answers: [{
text: "2008",
isCorrect: false
}, {
text: "2010",
isCorrect: true
}, {
text: "2012",
isCorrect: false
}, {
text: "2014",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's cat?",
answers: [{
text: "Meredith",
isCorrect: true
}, {
text: "Olivia",
isCorrect: false
}, {
text: "Benjamin",
isCorrect: false
}, {
text: "All of the above",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's 2020 documentary?",
answers: [{
text: "Miss Americana",
isCorrect: true
}, {
text: "The Swift Life",
isCorrect: false
}, {
text: "Taylor Swift: Journey to Fearless",
isCorrect: false
}, {
text: "The Taylor Swift Experience",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's 2020 album?",
answers: [{
text: "Folklore",
isCorrect: true
}, {
text: "Evermore",
isCorrect: false
}, {
text: "Lover",
isCorrect: false
}, {
text: "Reputation",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's 2019 album?",
answers: [{
text: "Lover",
isCorrect: true
}, {
text: "1989",
isCorrect: false
}, {
text: "Red",
isCorrect: false
}, {
text: "Speak Now",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's 2017 album?",
answers: [{
text: "Reputation",
isCorrect: true
}, {
text: "1989",
isCorrect: false
}, {
text: "Red",
isCorrect: false
}, {
text: "Speak Now",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's 2014 album?",
answers: [{
text: "1989",
isCorrect: true
}, {
text: "Red",
isCorrect: false
}, {
text: "Speak Now",
isCorrect: false
}, {
text: "Fearless",
isCorrect: false
}]
}, {
question: "What is the name of Taylor Swift's 2012 album?",
answers: [{
text: "Red",
isCorrect: true
}, {
text: "1989",
isCorrect: false
}, {
text: "Speak Now",
isCorrect: false
}, {
text: "Fearless",
isCorrect: false
}]
}].sort(function () {
return Math.random() - 0.5;
});
var currentQuestionIndex = 0;
var scoreTxt;
function displayNextQuestion() {
if (currentQuestionIndex < questions.length) {
var questionData = questions[currentQuestionIndex];
var question = game.addChild(new Question());
question.init(questionData.question, questionData.answers);
currentQuestionIndex++;
} else {
// No more questions, show game over
LK.showGameOver();
}
}
function setupGame() {
// Display score
scoreTxt = new Text2('Score: 0', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.x = 1024; // Center horizontally
scoreTxt.y = 50; // Position from top
LK.gui.top.addChild(scoreTxt);
// Start with the first question
displayNextQuestion();
}
// Update score display
game.update = function () {
scoreTxt.setText('Score: ' + LK.getScore());
};
setupGame();