/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var AnswerButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphic = self.attachAsset('answerButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.answerText = new Text2('', {
size: 36,
fill: 0xFFFFFF
});
self.answerText.anchor.set(0.5, 0.5);
self.answerText.x = 0;
self.answerText.y = 0;
self.addChild(self.answerText);
self.isCorrect = false;
self.answered = false;
self.setText = function (text) {
self.answerText.setText(text);
};
self.setCorrect = function (correct) {
self.isCorrect = correct;
};
self.showResult = function () {
self.answered = true;
self.removeChild(self.buttonGraphic);
if (self.isCorrect) {
self.buttonGraphic = self.attachAsset('correctButton', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
self.buttonGraphic = self.attachAsset('incorrectButton', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
self.down = function (x, y, obj) {
if (!self.answered && gameState === 'playing') {
handleAnswerClick(self);
}
};
return self;
});
var DifficultyButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphic = self.attachAsset('difficultyButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.difficultyText = new Text2('', {
size: 28,
fill: 0xFFFFFF
});
self.difficultyText.anchor.set(0.5, 0.5);
self.difficultyText.x = 0;
self.difficultyText.y = 0;
self.addChild(self.difficultyText);
self.difficulty = '';
self.setText = function (text) {
self.difficultyText.setText(text);
self.difficulty = text;
};
self.down = function (x, y, obj) {
if (gameState === 'difficultySelection') {
selectedDifficulty = self.difficulty;
startQuiz();
}
};
return self;
});
var QuestionPanel = Container.expand(function () {
var self = Container.call(this);
var panel = self.attachAsset('questionPanel', {
anchorX: 0.5,
anchorY: 0.5
});
self.questionText = new Text2('', {
size: 48,
fill: 0xFFFFFF
});
self.questionText.anchor.set(0.5, 0.5);
self.questionText.x = 0;
self.questionText.y = 0;
self.addChild(self.questionText);
self.setQuestion = function (text) {
self.questionText.setText(text);
};
return self;
});
var TopicButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphic = self.attachAsset('topicButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.topicText = new Text2('', {
size: 32,
fill: 0xFFFFFF
});
self.topicText.anchor.set(0.5, 0.5);
self.topicText.x = 0;
self.topicText.y = 0;
self.addChild(self.topicText);
self.topic = '';
self.setText = function (text) {
self.topicText.setText(text);
self.topic = text;
};
self.down = function (x, y, obj) {
if (gameState === 'topicSelection') {
selectedTopic = self.topic;
showDifficultySelection();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x34495e
});
/****
* Game Code
****/
// Game state variables
var gameState = 'topicSelection'; // 'topicSelection', 'difficultySelection', 'playing', 'results'
var selectedTopic = '';
var selectedDifficulty = '';
var currentQuestionIndex = 0;
var totalQuestions = 10;
var score = 0;
var questions = [];
// UI Elements
var questionPanel;
var answerButtons = [];
var topicButtons = [];
var difficultyButtons = [];
var progressBarBackground;
var progressBar;
var titleText;
var scoreText;
var questionCountText;
// Quiz data
var quizTopics = ['Science', 'History', 'Geography', 'Literature', 'Mathematics', 'Sports', 'Movies', 'Music'];
var difficulties = ['Easy', 'Medium', 'Hard'];
// Sample questions for different topics and difficulties
var questionBank = {
'Science': {
'Easy': [{
q: 'What is the chemical symbol for water?',
a: ['H2O', 'CO2', 'NaCl', 'O2'],
c: 0
}, {
q: 'How many planets are in our solar system?',
a: ['7', '8', '9', '10'],
c: 1
}, {
q: 'What gas do plants absorb from the atmosphere?',
a: ['Oxygen', 'Carbon Dioxide', 'Nitrogen', 'Hydrogen'],
c: 1
}, {
q: 'What is the center of an atom called?',
a: ['Electron', 'Proton', 'Nucleus', 'Neutron'],
c: 2
}, {
q: 'What is the hardest natural substance?',
a: ['Gold', 'Iron', 'Diamond', 'Silver'],
c: 2
}],
'Medium': [{
q: 'What is the speed of light in vacuum?',
a: ['300,000 km/s', '150,000 km/s', '450,000 km/s', '600,000 km/s'],
c: 0
}, {
q: 'Which element has the atomic number 1?',
a: ['Helium', 'Hydrogen', 'Lithium', 'Carbon'],
c: 1
}, {
q: 'What type of bond holds water molecules together?',
a: ['Ionic', 'Covalent', 'Hydrogen', 'Metallic'],
c: 2
}, {
q: 'What is the most abundant gas in Earth atmosphere?',
a: ['Oxygen', 'Carbon Dioxide', 'Nitrogen', 'Argon'],
c: 2
}, {
q: 'How many chambers does a human heart have?',
a: ['2', '3', '4', '5'],
c: 2
}],
'Hard': [{
q: 'What is the Heisenberg Uncertainty Principle about?',
a: ['Energy conservation', 'Position and momentum', 'Time dilation', 'Mass equivalence'],
c: 1
}, {
q: 'What is the name of the theoretical boundary around a black hole?',
a: ['Photon sphere', 'Event horizon', 'Ergosphere', 'Singularity'],
c: 1
}, {
q: 'Which subatomic particle was discovered by J.J. Thomson?',
a: ['Proton', 'Neutron', 'Electron', 'Positron'],
c: 2
}, {
q: 'What is the pH of pure water at 25°C?',
a: ['6', '7', '8', '9'],
c: 1
}, {
q: 'What is the first law of thermodynamics?',
a: ['Energy cannot be created or destroyed', 'Entropy always increases', 'Absolute zero is unattainable', 'Heat flows from hot to cold'],
c: 0
}]
},
'History': {
'Easy': [{
q: 'In which year did World War II end?',
a: ['1944', '1945', '1946', '1947'],
c: 1
}, {
q: 'Who was the first President of the United States?',
a: ['Thomas Jefferson', 'George Washington', 'John Adams', 'Benjamin Franklin'],
c: 1
}, {
q: 'Which ancient wonder was located in Egypt?',
a: ['Hanging Gardens', 'Colossus of Rhodes', 'Great Pyramid of Giza', 'Lighthouse of Alexandria'],
c: 2
}, {
q: 'What year did the Titanic sink?',
a: ['1910', '1911', '1912', '1913'],
c: 2
}, {
q: 'Who painted the Mona Lisa?',
a: ['Michelangelo', 'Leonardo da Vinci', 'Raphael', 'Donatello'],
c: 1
}],
'Medium': [{
q: 'Which empire was ruled by Julius Caesar?',
a: ['Greek Empire', 'Roman Empire', 'Persian Empire', 'Byzantine Empire'],
c: 1
}, {
q: 'In what year did the Berlin Wall fall?',
a: ['1987', '1988', '1989', '1990'],
c: 2
}, {
q: 'Who was the first person to walk on the moon?',
a: ['Buzz Aldrin', 'Neil Armstrong', 'John Glenn', 'Alan Shepard'],
c: 1
}, {
q: 'Which war was fought between the North and South in America?',
a: ['Revolutionary War', 'War of 1812', 'Civil War', 'Spanish-American War'],
c: 2
}, {
q: 'What ancient civilization built Machu Picchu?',
a: ['Maya', 'Aztec', 'Inca', 'Olmec'],
c: 2
}],
'Hard': [{
q: 'Which treaty ended World War I?',
a: ['Treaty of Versailles', 'Treaty of Paris', 'Treaty of Vienna', 'Treaty of Westphalia'],
c: 0
}, {
q: 'Who was the last Tsar of Russia?',
a: ['Alexander III', 'Nicholas II', 'Alexander II', 'Nicholas I'],
c: 1
}, {
q: 'What year did the Byzantine Empire fall?',
a: ['1453', '1492', '1517', '1571'],
c: 0
}, {
q: 'Which battle is considered the turning point of World War II in the Pacific?',
a: ['Pearl Harbor', 'Midway', 'Guadalcanal', 'Iwo Jima'],
c: 1
}, {
q: 'Who wrote "The Communist Manifesto"?',
a: ['Vladimir Lenin', 'Karl Marx and Friedrich Engels', 'Leon Trotsky', 'Joseph Stalin'],
c: 1
}]
}
};
// Initialize UI
function initializeUI() {
// Title text
titleText = new Text2('Quiz Master Challenge', {
size: 72,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 300;
game.addChild(titleText);
// Score display
scoreText = new Text2('Score: 0', {
size: 48,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Question count
questionCountText = new Text2('', {
size: 36,
fill: 0xFFFFFF
});
questionCountText.anchor.set(1, 0);
LK.gui.topRight.addChild(questionCountText);
showTopicSelection();
}
function showTopicSelection() {
gameState = 'topicSelection';
clearGameElements();
titleText.setText('Choose Your Topic');
titleText.y = 400;
// Create topic buttons
var cols = 4;
var rows = Math.ceil(quizTopics.length / cols);
var startX = 1024 - (cols - 1) * 450 / 2;
var startY = 800;
for (var i = 0; i < quizTopics.length; i++) {
var col = i % cols;
var row = Math.floor(i / cols);
var topicButton = new TopicButton();
topicButton.setText(quizTopics[i]);
topicButton.x = startX + col * 450;
topicButton.y = startY + row * 150;
topicButtons.push(topicButton);
game.addChild(topicButton);
}
}
function showDifficultySelection() {
gameState = 'difficultySelection';
clearGameElements();
titleText.setText('Choose Difficulty');
titleText.y = 500;
var startX = 1024;
var startY = 800;
for (var i = 0; i < difficulties.length; i++) {
var difficultyButton = new DifficultyButton();
difficultyButton.setText(difficulties[i]);
difficultyButton.x = startX;
difficultyButton.y = startY + i * 150;
difficultyButtons.push(difficultyButton);
game.addChild(difficultyButton);
}
}
function startQuiz() {
gameState = 'playing';
clearGameElements();
// Generate questions
generateQuestions();
currentQuestionIndex = 0;
score = 0;
// Create progress bar
progressBarBackground = game.addChild(LK.getAsset('progressBackground', {
anchorX: 0.5,
anchorY: 0.5
}));
progressBarBackground.x = 1024;
progressBarBackground.y = 200;
progressBar = game.addChild(LK.getAsset('progressBar', {
anchorX: 0,
anchorY: 0.5
}));
progressBar.x = progressBarBackground.x - progressBarBackground.width / 2;
progressBar.y = 200;
progressBar.width = 0;
// Create question panel
questionPanel = new QuestionPanel();
questionPanel.x = 1024;
questionPanel.y = 600;
game.addChild(questionPanel);
// Create answer buttons
for (var i = 0; i < 4; i++) {
var answerButton = new AnswerButton();
var col = i % 2;
var row = Math.floor(i / 2);
answerButton.x = 524 + col * 1000;
answerButton.y = 1000 + row * 200;
answerButtons.push(answerButton);
game.addChild(answerButton);
}
showQuestion();
}
function generateQuestions() {
questions = [];
var topicQuestions = questionBank[selectedTopic] && questionBank[selectedTopic][selectedDifficulty];
if (topicQuestions) {
// Use existing questions
for (var i = 0; i < Math.min(totalQuestions, topicQuestions.length); i++) {
questions.push(topicQuestions[i]);
}
} else {
// Generate placeholder questions
for (var i = 0; i < totalQuestions; i++) {
questions.push({
q: 'Sample question ' + (i + 1) + ' about ' + selectedTopic + ' (' + selectedDifficulty + ')',
a: ['Option A', 'Option B', 'Option C', 'Option D'],
c: Math.floor(Math.random() * 4)
});
}
}
}
function showQuestion() {
if (currentQuestionIndex >= questions.length) {
showResults();
return;
}
var question = questions[currentQuestionIndex];
// Update question panel
questionPanel.setQuestion(question.q);
// Update answer buttons
for (var i = 0; i < 4; i++) {
answerButtons[i].setText(question.a[i]);
answerButtons[i].setCorrect(i === question.c);
answerButtons[i].answered = false;
// Reset button graphics
answerButtons[i].removeChild(answerButtons[i].buttonGraphic);
answerButtons[i].buttonGraphic = answerButtons[i].attachAsset('answerButton', {
anchorX: 0.5,
anchorY: 0.5
});
}
// Update progress
updateProgress();
updateScoreDisplay();
}
function handleAnswerClick(button) {
// Show results for all buttons
for (var i = 0; i < answerButtons.length; i++) {
answerButtons[i].showResult();
}
// Update score
if (button.isCorrect) {
var points = getDifficultyPoints();
score += points;
LK.getSound('correct').play();
LK.effects.flashObject(button, 0x27ae60, 500);
} else {
LK.getSound('incorrect').play();
LK.effects.flashObject(button, 0xe74c3c, 500);
}
// Move to next question after delay
LK.setTimeout(function () {
currentQuestionIndex++;
showQuestion();
}, 2000);
}
function getDifficultyPoints() {
switch (selectedDifficulty) {
case 'Easy':
return 10;
case 'Medium':
return 20;
case 'Hard':
return 30;
default:
return 10;
}
}
function updateProgress() {
var progress = (currentQuestionIndex + 1) / totalQuestions;
progressBar.width = progressBarBackground.width * progress;
questionCountText.setText('Question ' + (currentQuestionIndex + 1) + '/' + totalQuestions);
}
function updateScoreDisplay() {
scoreText.setText('Score: ' + score);
}
function showResults() {
gameState = 'results';
clearGameElements();
var percentage = Math.round(score / (totalQuestions * getDifficultyPoints()) * 100);
var grade = getGrade(percentage);
titleText.setText('Quiz Complete!');
titleText.y = 500;
var resultText = new Text2('Final Score: ' + score + '\nAccuracy: ' + percentage + '%\nGrade: ' + grade, {
size: 48,
fill: 0xFFFFFF
});
resultText.anchor.set(0.5, 0.5);
resultText.x = 1024;
resultText.y = 900;
game.addChild(resultText);
var playAgainText = new Text2('Tap anywhere to play again', {
size: 36,
fill: 0x95A5A6
});
playAgainText.anchor.set(0.5, 0.5);
playAgainText.x = 1024;
playAgainText.y = 1200;
game.addChild(playAgainText);
// Update high score
var highScore = storage.highScore || 0;
if (score > highScore) {
storage.highScore = score;
LK.effects.flashScreen(0xf1c40f, 1000);
}
LK.setScore(score);
}
function getGrade(percentage) {
if (percentage >= 90) return 'A+';
if (percentage >= 80) return 'A';
if (percentage >= 70) return 'B';
if (percentage >= 60) return 'C';
if (percentage >= 50) return 'D';
return 'F';
}
function clearGameElements() {
// Clear topic buttons
for (var i = 0; i < topicButtons.length; i++) {
topicButtons[i].destroy();
}
topicButtons = [];
// Clear difficulty buttons
for (var i = 0; i < difficultyButtons.length; i++) {
difficultyButtons[i].destroy();
}
difficultyButtons = [];
// Clear answer buttons
for (var i = 0; i < answerButtons.length; i++) {
answerButtons[i].destroy();
}
answerButtons = [];
// Clear question panel
if (questionPanel) {
questionPanel.destroy();
questionPanel = null;
}
// Clear progress bars
if (progressBar) {
progressBar.destroy();
progressBar = null;
}
if (progressBarBackground) {
progressBarBackground.destroy();
progressBarBackground = null;
}
}
// Game event handlers
game.down = function (x, y, obj) {
if (gameState === 'results') {
showTopicSelection();
}
};
game.update = function () {
// Update score display if needed
if (gameState === 'playing') {
updateScoreDisplay();
}
};
// Initialize the game
initializeUI(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var AnswerButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphic = self.attachAsset('answerButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.answerText = new Text2('', {
size: 36,
fill: 0xFFFFFF
});
self.answerText.anchor.set(0.5, 0.5);
self.answerText.x = 0;
self.answerText.y = 0;
self.addChild(self.answerText);
self.isCorrect = false;
self.answered = false;
self.setText = function (text) {
self.answerText.setText(text);
};
self.setCorrect = function (correct) {
self.isCorrect = correct;
};
self.showResult = function () {
self.answered = true;
self.removeChild(self.buttonGraphic);
if (self.isCorrect) {
self.buttonGraphic = self.attachAsset('correctButton', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
self.buttonGraphic = self.attachAsset('incorrectButton', {
anchorX: 0.5,
anchorY: 0.5
});
}
};
self.down = function (x, y, obj) {
if (!self.answered && gameState === 'playing') {
handleAnswerClick(self);
}
};
return self;
});
var DifficultyButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphic = self.attachAsset('difficultyButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.difficultyText = new Text2('', {
size: 28,
fill: 0xFFFFFF
});
self.difficultyText.anchor.set(0.5, 0.5);
self.difficultyText.x = 0;
self.difficultyText.y = 0;
self.addChild(self.difficultyText);
self.difficulty = '';
self.setText = function (text) {
self.difficultyText.setText(text);
self.difficulty = text;
};
self.down = function (x, y, obj) {
if (gameState === 'difficultySelection') {
selectedDifficulty = self.difficulty;
startQuiz();
}
};
return self;
});
var QuestionPanel = Container.expand(function () {
var self = Container.call(this);
var panel = self.attachAsset('questionPanel', {
anchorX: 0.5,
anchorY: 0.5
});
self.questionText = new Text2('', {
size: 48,
fill: 0xFFFFFF
});
self.questionText.anchor.set(0.5, 0.5);
self.questionText.x = 0;
self.questionText.y = 0;
self.addChild(self.questionText);
self.setQuestion = function (text) {
self.questionText.setText(text);
};
return self;
});
var TopicButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphic = self.attachAsset('topicButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.topicText = new Text2('', {
size: 32,
fill: 0xFFFFFF
});
self.topicText.anchor.set(0.5, 0.5);
self.topicText.x = 0;
self.topicText.y = 0;
self.addChild(self.topicText);
self.topic = '';
self.setText = function (text) {
self.topicText.setText(text);
self.topic = text;
};
self.down = function (x, y, obj) {
if (gameState === 'topicSelection') {
selectedTopic = self.topic;
showDifficultySelection();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x34495e
});
/****
* Game Code
****/
// Game state variables
var gameState = 'topicSelection'; // 'topicSelection', 'difficultySelection', 'playing', 'results'
var selectedTopic = '';
var selectedDifficulty = '';
var currentQuestionIndex = 0;
var totalQuestions = 10;
var score = 0;
var questions = [];
// UI Elements
var questionPanel;
var answerButtons = [];
var topicButtons = [];
var difficultyButtons = [];
var progressBarBackground;
var progressBar;
var titleText;
var scoreText;
var questionCountText;
// Quiz data
var quizTopics = ['Science', 'History', 'Geography', 'Literature', 'Mathematics', 'Sports', 'Movies', 'Music'];
var difficulties = ['Easy', 'Medium', 'Hard'];
// Sample questions for different topics and difficulties
var questionBank = {
'Science': {
'Easy': [{
q: 'What is the chemical symbol for water?',
a: ['H2O', 'CO2', 'NaCl', 'O2'],
c: 0
}, {
q: 'How many planets are in our solar system?',
a: ['7', '8', '9', '10'],
c: 1
}, {
q: 'What gas do plants absorb from the atmosphere?',
a: ['Oxygen', 'Carbon Dioxide', 'Nitrogen', 'Hydrogen'],
c: 1
}, {
q: 'What is the center of an atom called?',
a: ['Electron', 'Proton', 'Nucleus', 'Neutron'],
c: 2
}, {
q: 'What is the hardest natural substance?',
a: ['Gold', 'Iron', 'Diamond', 'Silver'],
c: 2
}],
'Medium': [{
q: 'What is the speed of light in vacuum?',
a: ['300,000 km/s', '150,000 km/s', '450,000 km/s', '600,000 km/s'],
c: 0
}, {
q: 'Which element has the atomic number 1?',
a: ['Helium', 'Hydrogen', 'Lithium', 'Carbon'],
c: 1
}, {
q: 'What type of bond holds water molecules together?',
a: ['Ionic', 'Covalent', 'Hydrogen', 'Metallic'],
c: 2
}, {
q: 'What is the most abundant gas in Earth atmosphere?',
a: ['Oxygen', 'Carbon Dioxide', 'Nitrogen', 'Argon'],
c: 2
}, {
q: 'How many chambers does a human heart have?',
a: ['2', '3', '4', '5'],
c: 2
}],
'Hard': [{
q: 'What is the Heisenberg Uncertainty Principle about?',
a: ['Energy conservation', 'Position and momentum', 'Time dilation', 'Mass equivalence'],
c: 1
}, {
q: 'What is the name of the theoretical boundary around a black hole?',
a: ['Photon sphere', 'Event horizon', 'Ergosphere', 'Singularity'],
c: 1
}, {
q: 'Which subatomic particle was discovered by J.J. Thomson?',
a: ['Proton', 'Neutron', 'Electron', 'Positron'],
c: 2
}, {
q: 'What is the pH of pure water at 25°C?',
a: ['6', '7', '8', '9'],
c: 1
}, {
q: 'What is the first law of thermodynamics?',
a: ['Energy cannot be created or destroyed', 'Entropy always increases', 'Absolute zero is unattainable', 'Heat flows from hot to cold'],
c: 0
}]
},
'History': {
'Easy': [{
q: 'In which year did World War II end?',
a: ['1944', '1945', '1946', '1947'],
c: 1
}, {
q: 'Who was the first President of the United States?',
a: ['Thomas Jefferson', 'George Washington', 'John Adams', 'Benjamin Franklin'],
c: 1
}, {
q: 'Which ancient wonder was located in Egypt?',
a: ['Hanging Gardens', 'Colossus of Rhodes', 'Great Pyramid of Giza', 'Lighthouse of Alexandria'],
c: 2
}, {
q: 'What year did the Titanic sink?',
a: ['1910', '1911', '1912', '1913'],
c: 2
}, {
q: 'Who painted the Mona Lisa?',
a: ['Michelangelo', 'Leonardo da Vinci', 'Raphael', 'Donatello'],
c: 1
}],
'Medium': [{
q: 'Which empire was ruled by Julius Caesar?',
a: ['Greek Empire', 'Roman Empire', 'Persian Empire', 'Byzantine Empire'],
c: 1
}, {
q: 'In what year did the Berlin Wall fall?',
a: ['1987', '1988', '1989', '1990'],
c: 2
}, {
q: 'Who was the first person to walk on the moon?',
a: ['Buzz Aldrin', 'Neil Armstrong', 'John Glenn', 'Alan Shepard'],
c: 1
}, {
q: 'Which war was fought between the North and South in America?',
a: ['Revolutionary War', 'War of 1812', 'Civil War', 'Spanish-American War'],
c: 2
}, {
q: 'What ancient civilization built Machu Picchu?',
a: ['Maya', 'Aztec', 'Inca', 'Olmec'],
c: 2
}],
'Hard': [{
q: 'Which treaty ended World War I?',
a: ['Treaty of Versailles', 'Treaty of Paris', 'Treaty of Vienna', 'Treaty of Westphalia'],
c: 0
}, {
q: 'Who was the last Tsar of Russia?',
a: ['Alexander III', 'Nicholas II', 'Alexander II', 'Nicholas I'],
c: 1
}, {
q: 'What year did the Byzantine Empire fall?',
a: ['1453', '1492', '1517', '1571'],
c: 0
}, {
q: 'Which battle is considered the turning point of World War II in the Pacific?',
a: ['Pearl Harbor', 'Midway', 'Guadalcanal', 'Iwo Jima'],
c: 1
}, {
q: 'Who wrote "The Communist Manifesto"?',
a: ['Vladimir Lenin', 'Karl Marx and Friedrich Engels', 'Leon Trotsky', 'Joseph Stalin'],
c: 1
}]
}
};
// Initialize UI
function initializeUI() {
// Title text
titleText = new Text2('Quiz Master Challenge', {
size: 72,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0.5);
titleText.x = 1024;
titleText.y = 300;
game.addChild(titleText);
// Score display
scoreText = new Text2('Score: 0', {
size: 48,
fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
// Question count
questionCountText = new Text2('', {
size: 36,
fill: 0xFFFFFF
});
questionCountText.anchor.set(1, 0);
LK.gui.topRight.addChild(questionCountText);
showTopicSelection();
}
function showTopicSelection() {
gameState = 'topicSelection';
clearGameElements();
titleText.setText('Choose Your Topic');
titleText.y = 400;
// Create topic buttons
var cols = 4;
var rows = Math.ceil(quizTopics.length / cols);
var startX = 1024 - (cols - 1) * 450 / 2;
var startY = 800;
for (var i = 0; i < quizTopics.length; i++) {
var col = i % cols;
var row = Math.floor(i / cols);
var topicButton = new TopicButton();
topicButton.setText(quizTopics[i]);
topicButton.x = startX + col * 450;
topicButton.y = startY + row * 150;
topicButtons.push(topicButton);
game.addChild(topicButton);
}
}
function showDifficultySelection() {
gameState = 'difficultySelection';
clearGameElements();
titleText.setText('Choose Difficulty');
titleText.y = 500;
var startX = 1024;
var startY = 800;
for (var i = 0; i < difficulties.length; i++) {
var difficultyButton = new DifficultyButton();
difficultyButton.setText(difficulties[i]);
difficultyButton.x = startX;
difficultyButton.y = startY + i * 150;
difficultyButtons.push(difficultyButton);
game.addChild(difficultyButton);
}
}
function startQuiz() {
gameState = 'playing';
clearGameElements();
// Generate questions
generateQuestions();
currentQuestionIndex = 0;
score = 0;
// Create progress bar
progressBarBackground = game.addChild(LK.getAsset('progressBackground', {
anchorX: 0.5,
anchorY: 0.5
}));
progressBarBackground.x = 1024;
progressBarBackground.y = 200;
progressBar = game.addChild(LK.getAsset('progressBar', {
anchorX: 0,
anchorY: 0.5
}));
progressBar.x = progressBarBackground.x - progressBarBackground.width / 2;
progressBar.y = 200;
progressBar.width = 0;
// Create question panel
questionPanel = new QuestionPanel();
questionPanel.x = 1024;
questionPanel.y = 600;
game.addChild(questionPanel);
// Create answer buttons
for (var i = 0; i < 4; i++) {
var answerButton = new AnswerButton();
var col = i % 2;
var row = Math.floor(i / 2);
answerButton.x = 524 + col * 1000;
answerButton.y = 1000 + row * 200;
answerButtons.push(answerButton);
game.addChild(answerButton);
}
showQuestion();
}
function generateQuestions() {
questions = [];
var topicQuestions = questionBank[selectedTopic] && questionBank[selectedTopic][selectedDifficulty];
if (topicQuestions) {
// Use existing questions
for (var i = 0; i < Math.min(totalQuestions, topicQuestions.length); i++) {
questions.push(topicQuestions[i]);
}
} else {
// Generate placeholder questions
for (var i = 0; i < totalQuestions; i++) {
questions.push({
q: 'Sample question ' + (i + 1) + ' about ' + selectedTopic + ' (' + selectedDifficulty + ')',
a: ['Option A', 'Option B', 'Option C', 'Option D'],
c: Math.floor(Math.random() * 4)
});
}
}
}
function showQuestion() {
if (currentQuestionIndex >= questions.length) {
showResults();
return;
}
var question = questions[currentQuestionIndex];
// Update question panel
questionPanel.setQuestion(question.q);
// Update answer buttons
for (var i = 0; i < 4; i++) {
answerButtons[i].setText(question.a[i]);
answerButtons[i].setCorrect(i === question.c);
answerButtons[i].answered = false;
// Reset button graphics
answerButtons[i].removeChild(answerButtons[i].buttonGraphic);
answerButtons[i].buttonGraphic = answerButtons[i].attachAsset('answerButton', {
anchorX: 0.5,
anchorY: 0.5
});
}
// Update progress
updateProgress();
updateScoreDisplay();
}
function handleAnswerClick(button) {
// Show results for all buttons
for (var i = 0; i < answerButtons.length; i++) {
answerButtons[i].showResult();
}
// Update score
if (button.isCorrect) {
var points = getDifficultyPoints();
score += points;
LK.getSound('correct').play();
LK.effects.flashObject(button, 0x27ae60, 500);
} else {
LK.getSound('incorrect').play();
LK.effects.flashObject(button, 0xe74c3c, 500);
}
// Move to next question after delay
LK.setTimeout(function () {
currentQuestionIndex++;
showQuestion();
}, 2000);
}
function getDifficultyPoints() {
switch (selectedDifficulty) {
case 'Easy':
return 10;
case 'Medium':
return 20;
case 'Hard':
return 30;
default:
return 10;
}
}
function updateProgress() {
var progress = (currentQuestionIndex + 1) / totalQuestions;
progressBar.width = progressBarBackground.width * progress;
questionCountText.setText('Question ' + (currentQuestionIndex + 1) + '/' + totalQuestions);
}
function updateScoreDisplay() {
scoreText.setText('Score: ' + score);
}
function showResults() {
gameState = 'results';
clearGameElements();
var percentage = Math.round(score / (totalQuestions * getDifficultyPoints()) * 100);
var grade = getGrade(percentage);
titleText.setText('Quiz Complete!');
titleText.y = 500;
var resultText = new Text2('Final Score: ' + score + '\nAccuracy: ' + percentage + '%\nGrade: ' + grade, {
size: 48,
fill: 0xFFFFFF
});
resultText.anchor.set(0.5, 0.5);
resultText.x = 1024;
resultText.y = 900;
game.addChild(resultText);
var playAgainText = new Text2('Tap anywhere to play again', {
size: 36,
fill: 0x95A5A6
});
playAgainText.anchor.set(0.5, 0.5);
playAgainText.x = 1024;
playAgainText.y = 1200;
game.addChild(playAgainText);
// Update high score
var highScore = storage.highScore || 0;
if (score > highScore) {
storage.highScore = score;
LK.effects.flashScreen(0xf1c40f, 1000);
}
LK.setScore(score);
}
function getGrade(percentage) {
if (percentage >= 90) return 'A+';
if (percentage >= 80) return 'A';
if (percentage >= 70) return 'B';
if (percentage >= 60) return 'C';
if (percentage >= 50) return 'D';
return 'F';
}
function clearGameElements() {
// Clear topic buttons
for (var i = 0; i < topicButtons.length; i++) {
topicButtons[i].destroy();
}
topicButtons = [];
// Clear difficulty buttons
for (var i = 0; i < difficultyButtons.length; i++) {
difficultyButtons[i].destroy();
}
difficultyButtons = [];
// Clear answer buttons
for (var i = 0; i < answerButtons.length; i++) {
answerButtons[i].destroy();
}
answerButtons = [];
// Clear question panel
if (questionPanel) {
questionPanel.destroy();
questionPanel = null;
}
// Clear progress bars
if (progressBar) {
progressBar.destroy();
progressBar = null;
}
if (progressBarBackground) {
progressBarBackground.destroy();
progressBarBackground = null;
}
}
// Game event handlers
game.down = function (x, y, obj) {
if (gameState === 'results') {
showTopicSelection();
}
};
game.update = function () {
// Update score display if needed
if (gameState === 'playing') {
updateScoreDisplay();
}
};
// Initialize the game
initializeUI();