/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var AnswerButton = Container.expand(function () { var self = Container.call(this); var buttonBg = self.attachAsset('answerButton', { anchorX: 0.5, anchorY: 0.5 }); self.buttonText = new Text2('', { size: 28, fill: '#ffffff' }); self.buttonText.anchor.set(0.5, 0.5); self.addChild(self.buttonText); self.isCorrect = false; self.isClicked = false; self.setText = function (text) { self.buttonText.setText(text); }; self.setCorrect = function (correct) { self.isCorrect = correct; }; self.showResult = function () { if (self.isCorrect) { self.removeChild(buttonBg); var correctBg = self.attachAsset('correctButton', { anchorX: 0.5, anchorY: 0.5 }); self.addChildAt(correctBg, 0); } else { self.removeChild(buttonBg); var wrongBg = self.attachAsset('wrongButton', { anchorX: 0.5, anchorY: 0.5 }); self.addChildAt(wrongBg, 0); } }; self.down = function (x, y, obj) { if (self.isClicked) return; self.isClicked = true; if (self.isCorrect) { LK.getSound('correct').play(); LK.setScore(LK.getScore() + 1); scoreText.setText('Score: ' + LK.getScore()); } else { LK.getSound('wrong').play(); } for (var i = 0; i < answerButtons.length; i++) { answerButtons[i].showResult(); } LK.setTimeout(function () { nextCard(); }, 1500); }; return self; }); var ScratchCard = Container.expand(function () { var self = Container.call(this); var cardBack = self.attachAsset('cardBack', { anchorX: 0.5, anchorY: 0.5 }); var cardFront = self.attachAsset('cardFront', { anchorX: 0.5, anchorY: 0.5 }); var scratchOverlay = self.attachAsset('scratchOverlay', { anchorX: 0.5, anchorY: 0.5 }); self.questionText = new Text2('', { size: 32, fill: '#000000' }); self.questionText.anchor.set(0.5, 0.5); self.addChild(self.questionText); self.isScratched = false; self.scratchedAreas = []; self.question = ''; self.answers = []; self.correctAnswer = 0; self.isRevealed = false; self.setQuestion = function (question, answers, correctIndex) { self.question = question; self.answers = answers; self.correctAnswer = correctIndex; self.questionText.setText(question); self.questionText.alpha = 0; }; self.scratch = function (x, y) { if (self.isRevealed) return; var localPos = self.toLocal({ x: x, y: y }); if (Math.abs(localPos.x) < 300 && Math.abs(localPos.y) < 200) { self.scratchedAreas.push({ x: localPos.x, y: localPos.y }); if (self.scratchedAreas.length > 15) { self.revealQuestion(); } else { var scratchProgress = self.scratchedAreas.length / 15; scratchOverlay.alpha = 1 - scratchProgress; self.questionText.alpha = scratchProgress * 0.8; } LK.getSound('scratch').play(); } }; self.revealQuestion = function () { self.isRevealed = true; scratchOverlay.alpha = 0; self.questionText.alpha = 1; tween(self.questionText, { scaleX: 1.1, scaleY: 1.1 }, { duration: 200, easing: tween.easeOut, onFinish: function onFinish() { tween(self.questionText, { scaleX: 1, scaleY: 1 }, { duration: 200, easing: tween.easeIn }); } }); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x2196F3 }); /**** * Game Code ****/ var questions = [{ question: "What is the capital of France?", answers: ["London", "Paris", "Berlin"], correct: 1 }, { question: "Which planet is closest to the Sun?", answers: ["Venus", "Mercury", "Mars"], correct: 1 }, { question: "What is 2 + 2?", answers: ["3", "4", "5"], correct: 1 }, { question: "Which animal is known as the King of the Jungle?", answers: ["Tiger", "Lion", "Elephant"], correct: 1 }, { question: "What color do you get when you mix red and blue?", answers: ["Purple", "Green", "Orange"], correct: 0 }, { question: "How many continents are there?", answers: ["5", "6", "7"], correct: 2 }, { question: "Which is the largest ocean?", answers: ["Atlantic", "Pacific", "Indian"], correct: 1 }, { question: "What is the fastest land animal?", answers: ["Cheetah", "Lion", "Horse"], correct: 0 }]; var currentCard = null; var answerButtons = []; var currentQuestionIndex = 0; var isAnswering = false; var scoreText = new Text2('Score: 0', { size: 48, fill: '#ffffff' }); scoreText.anchor.set(0.5, 0); LK.gui.top.addChild(scoreText); var instructionText = new Text2('Scratch the card to reveal the question!', { size: 36, fill: '#ffffff' }); instructionText.anchor.set(0.5, 0); instructionText.y = 100; LK.gui.top.addChild(instructionText); function createNewCard() { if (currentQuestionIndex >= questions.length) { LK.setTimeout(function () { LK.showYouWin(); }, 1000); return; } var questionData = questions[currentQuestionIndex]; currentCard = new ScratchCard(); currentCard.x = 2048 / 2; currentCard.y = 2732 / 2 - 100; currentCard.setQuestion(questionData.question, questionData.answers, questionData.correct); game.addChild(currentCard); isAnswering = false; instructionText.setText('Scratch the card to reveal the question!'); } function createAnswerButtons() { var questionData = questions[currentQuestionIndex]; for (var i = 0; i < 3; i++) { var button = new AnswerButton(); button.x = 2048 / 2; button.y = 2732 / 2 + 200 + i * 120; button.setText(questionData.answers[i]); button.setCorrect(i === questionData.correct); answerButtons.push(button); game.addChild(button); } isAnswering = true; instructionText.setText('Tap the correct answer!'); } function nextCard() { if (currentCard) { currentCard.destroy(); currentCard = null; } for (var i = 0; i < answerButtons.length; i++) { answerButtons[i].destroy(); } answerButtons = []; currentQuestionIndex++; createNewCard(); } var isDragging = false; game.move = function (x, y, obj) { if (isDragging && currentCard && !currentCard.isRevealed) { currentCard.scratch(x, y); } }; game.down = function (x, y, obj) { if (currentCard && !currentCard.isRevealed && !isAnswering) { isDragging = true; currentCard.scratch(x, y); } }; game.up = function (x, y, obj) { isDragging = false; }; game.update = function () { if (currentCard && currentCard.isRevealed && answerButtons.length === 0 && !isAnswering) { createAnswerButtons(); } }; createNewCard();
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var AnswerButton = Container.expand(function () {
var self = Container.call(this);
var buttonBg = self.attachAsset('answerButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.buttonText = new Text2('', {
size: 28,
fill: '#ffffff'
});
self.buttonText.anchor.set(0.5, 0.5);
self.addChild(self.buttonText);
self.isCorrect = false;
self.isClicked = false;
self.setText = function (text) {
self.buttonText.setText(text);
};
self.setCorrect = function (correct) {
self.isCorrect = correct;
};
self.showResult = function () {
if (self.isCorrect) {
self.removeChild(buttonBg);
var correctBg = self.attachAsset('correctButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChildAt(correctBg, 0);
} else {
self.removeChild(buttonBg);
var wrongBg = self.attachAsset('wrongButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChildAt(wrongBg, 0);
}
};
self.down = function (x, y, obj) {
if (self.isClicked) return;
self.isClicked = true;
if (self.isCorrect) {
LK.getSound('correct').play();
LK.setScore(LK.getScore() + 1);
scoreText.setText('Score: ' + LK.getScore());
} else {
LK.getSound('wrong').play();
}
for (var i = 0; i < answerButtons.length; i++) {
answerButtons[i].showResult();
}
LK.setTimeout(function () {
nextCard();
}, 1500);
};
return self;
});
var ScratchCard = Container.expand(function () {
var self = Container.call(this);
var cardBack = self.attachAsset('cardBack', {
anchorX: 0.5,
anchorY: 0.5
});
var cardFront = self.attachAsset('cardFront', {
anchorX: 0.5,
anchorY: 0.5
});
var scratchOverlay = self.attachAsset('scratchOverlay', {
anchorX: 0.5,
anchorY: 0.5
});
self.questionText = new Text2('', {
size: 32,
fill: '#000000'
});
self.questionText.anchor.set(0.5, 0.5);
self.addChild(self.questionText);
self.isScratched = false;
self.scratchedAreas = [];
self.question = '';
self.answers = [];
self.correctAnswer = 0;
self.isRevealed = false;
self.setQuestion = function (question, answers, correctIndex) {
self.question = question;
self.answers = answers;
self.correctAnswer = correctIndex;
self.questionText.setText(question);
self.questionText.alpha = 0;
};
self.scratch = function (x, y) {
if (self.isRevealed) return;
var localPos = self.toLocal({
x: x,
y: y
});
if (Math.abs(localPos.x) < 300 && Math.abs(localPos.y) < 200) {
self.scratchedAreas.push({
x: localPos.x,
y: localPos.y
});
if (self.scratchedAreas.length > 15) {
self.revealQuestion();
} else {
var scratchProgress = self.scratchedAreas.length / 15;
scratchOverlay.alpha = 1 - scratchProgress;
self.questionText.alpha = scratchProgress * 0.8;
}
LK.getSound('scratch').play();
}
};
self.revealQuestion = function () {
self.isRevealed = true;
scratchOverlay.alpha = 0;
self.questionText.alpha = 1;
tween(self.questionText, {
scaleX: 1.1,
scaleY: 1.1
}, {
duration: 200,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self.questionText, {
scaleX: 1,
scaleY: 1
}, {
duration: 200,
easing: tween.easeIn
});
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2196F3
});
/****
* Game Code
****/
var questions = [{
question: "What is the capital of France?",
answers: ["London", "Paris", "Berlin"],
correct: 1
}, {
question: "Which planet is closest to the Sun?",
answers: ["Venus", "Mercury", "Mars"],
correct: 1
}, {
question: "What is 2 + 2?",
answers: ["3", "4", "5"],
correct: 1
}, {
question: "Which animal is known as the King of the Jungle?",
answers: ["Tiger", "Lion", "Elephant"],
correct: 1
}, {
question: "What color do you get when you mix red and blue?",
answers: ["Purple", "Green", "Orange"],
correct: 0
}, {
question: "How many continents are there?",
answers: ["5", "6", "7"],
correct: 2
}, {
question: "Which is the largest ocean?",
answers: ["Atlantic", "Pacific", "Indian"],
correct: 1
}, {
question: "What is the fastest land animal?",
answers: ["Cheetah", "Lion", "Horse"],
correct: 0
}];
var currentCard = null;
var answerButtons = [];
var currentQuestionIndex = 0;
var isAnswering = false;
var scoreText = new Text2('Score: 0', {
size: 48,
fill: '#ffffff'
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var instructionText = new Text2('Scratch the card to reveal the question!', {
size: 36,
fill: '#ffffff'
});
instructionText.anchor.set(0.5, 0);
instructionText.y = 100;
LK.gui.top.addChild(instructionText);
function createNewCard() {
if (currentQuestionIndex >= questions.length) {
LK.setTimeout(function () {
LK.showYouWin();
}, 1000);
return;
}
var questionData = questions[currentQuestionIndex];
currentCard = new ScratchCard();
currentCard.x = 2048 / 2;
currentCard.y = 2732 / 2 - 100;
currentCard.setQuestion(questionData.question, questionData.answers, questionData.correct);
game.addChild(currentCard);
isAnswering = false;
instructionText.setText('Scratch the card to reveal the question!');
}
function createAnswerButtons() {
var questionData = questions[currentQuestionIndex];
for (var i = 0; i < 3; i++) {
var button = new AnswerButton();
button.x = 2048 / 2;
button.y = 2732 / 2 + 200 + i * 120;
button.setText(questionData.answers[i]);
button.setCorrect(i === questionData.correct);
answerButtons.push(button);
game.addChild(button);
}
isAnswering = true;
instructionText.setText('Tap the correct answer!');
}
function nextCard() {
if (currentCard) {
currentCard.destroy();
currentCard = null;
}
for (var i = 0; i < answerButtons.length; i++) {
answerButtons[i].destroy();
}
answerButtons = [];
currentQuestionIndex++;
createNewCard();
}
var isDragging = false;
game.move = function (x, y, obj) {
if (isDragging && currentCard && !currentCard.isRevealed) {
currentCard.scratch(x, y);
}
};
game.down = function (x, y, obj) {
if (currentCard && !currentCard.isRevealed && !isAnswering) {
isDragging = true;
currentCard.scratch(x, y);
}
};
game.up = function (x, y, obj) {
isDragging = false;
};
game.update = function () {
if (currentCard && currentCard.isRevealed && answerButtons.length === 0 && !isAnswering) {
createAnswerButtons();
}
};
createNewCard();