/**** * Classes ****/ // Class for the Answer Option var AnswerOption = Container.expand(function () { var self = Container.call(this); var optionText = new Text2('', { size: 80, fill: "#ffffff" }); optionText.anchor.set(0.5, 0.5); self.addChild(optionText); self.setText = function (text) { optionText.setText(text); }; self.down = function (x, y, obj) { if (self.correct) { LK.setScore(LK.getScore() + 1); scoreTxt.setText(LK.getScore()); LK.effects.flashObject(self, 0x00ff00, 500); nextQuestion(); } else { LK.effects.flashObject(self, 0xff0000, 500); } }; return self; }); //<Assets used in the game will automatically appear here> // Class for the Question var Question = Container.expand(function () { var self = Container.call(this); var questionText = new Text2('', { size: 100, fill: "#ffffff" }); questionText.anchor.set(0.5, 0.5); self.addChild(questionText); self.setText = function (text) { questionText.setText(text); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ var questions = [{ question: "What is 2 + 2?", options: ["3", "4", "5"], correct: 1 }, { question: "What is the capital of France?", options: ["Berlin", "Paris", "Rome"], correct: 1 }, { question: "What is the color of the sky?", options: ["Blue", "Green", "Red"], correct: 0 }]; var currentQuestionIndex = 0; var questionDisplay = new Question(); questionDisplay.x = 2048 / 2; questionDisplay.y = 500; game.addChild(questionDisplay); var answerOptions = []; for (var i = 0; i < 3; i++) { var option = new AnswerOption(); option.x = 2048 / 2; option.y = 1000 + i * 200; game.addChild(option); answerOptions.push(option); } var scoreTxt = new Text2('0', { size: 150, fill: "#ffffff" }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); function nextQuestion() { if (currentQuestionIndex >= questions.length) { LK.showGameOver(); return; } var currentQuestion = questions[currentQuestionIndex]; questionDisplay.setText(currentQuestion.question); for (var i = 0; i < answerOptions.length; i++) { answerOptions[i].setText(currentQuestion.options[i]); answerOptions[i].correct = i === currentQuestion.correct; } currentQuestionIndex++; } nextQuestion(); game.update = function () { // Game update logic if needed };
/****
* Classes
****/
// Class for the Answer Option
var AnswerOption = Container.expand(function () {
var self = Container.call(this);
var optionText = new Text2('', {
size: 80,
fill: "#ffffff"
});
optionText.anchor.set(0.5, 0.5);
self.addChild(optionText);
self.setText = function (text) {
optionText.setText(text);
};
self.down = function (x, y, obj) {
if (self.correct) {
LK.setScore(LK.getScore() + 1);
scoreTxt.setText(LK.getScore());
LK.effects.flashObject(self, 0x00ff00, 500);
nextQuestion();
} else {
LK.effects.flashObject(self, 0xff0000, 500);
}
};
return self;
});
//<Assets used in the game will automatically appear here>
// Class for the Question
var Question = Container.expand(function () {
var self = Container.call(this);
var questionText = new Text2('', {
size: 100,
fill: "#ffffff"
});
questionText.anchor.set(0.5, 0.5);
self.addChild(questionText);
self.setText = function (text) {
questionText.setText(text);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var questions = [{
question: "What is 2 + 2?",
options: ["3", "4", "5"],
correct: 1
}, {
question: "What is the capital of France?",
options: ["Berlin", "Paris", "Rome"],
correct: 1
}, {
question: "What is the color of the sky?",
options: ["Blue", "Green", "Red"],
correct: 0
}];
var currentQuestionIndex = 0;
var questionDisplay = new Question();
questionDisplay.x = 2048 / 2;
questionDisplay.y = 500;
game.addChild(questionDisplay);
var answerOptions = [];
for (var i = 0; i < 3; i++) {
var option = new AnswerOption();
option.x = 2048 / 2;
option.y = 1000 + i * 200;
game.addChild(option);
answerOptions.push(option);
}
var scoreTxt = new Text2('0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function nextQuestion() {
if (currentQuestionIndex >= questions.length) {
LK.showGameOver();
return;
}
var currentQuestion = questions[currentQuestionIndex];
questionDisplay.setText(currentQuestion.question);
for (var i = 0; i < answerOptions.length; i++) {
answerOptions[i].setText(currentQuestion.options[i]);
answerOptions[i].correct = i === currentQuestion.correct;
}
currentQuestionIndex++;
}
nextQuestion();
game.update = function () {
// Game update logic if needed
};