/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0,
completedTables: {}
});
/****
* Classes
****/
var Confetti = Container.expand(function (color) {
var self = Container.call(this);
var confettiGraphic = self.attachAsset('confetti', {
anchorX: 0.5,
anchorY: 0.5
});
confettiGraphic.tint = color || 0xf54242;
self.vx = Math.random() * 10 - 5;
self.vy = Math.random() * -15 - 5;
self.vr = (Math.random() - 0.5) * 0.2;
self.gravity = 0.3;
self.update = function () {
self.x += self.vx;
self.y += self.vy;
self.vy += self.gravity;
confettiGraphic.rotation += self.vr;
if (self.y > 2732) {
self.destroy();
var index = confetti.indexOf(self);
if (index > -1) {
confetti.splice(index, 1);
}
}
};
return self;
});
var Heart = Container.expand(function () {
var self = Container.call(this);
var heartGraphic = self.attachAsset('heart', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1,
scaleY: 1
});
self.setActive = function (active) {
heartGraphic.alpha = active ? 1 : 0.3;
};
return self;
});
var NumberKey = Container.expand(function (number) {
var self = Container.call(this);
var keyGraphic = self.attachAsset('numberKey', {
anchorX: 0.5,
anchorY: 0.5
});
var keyText = new Text2(number.toString(), {
size: 90,
fill: 0x000000
});
keyText.anchor.set(0.5, 0.5);
self.addChild(keyText);
self.number = number;
self.down = function (x, y, obj) {
keyGraphic.alpha = 0.7;
tween(keyGraphic, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
if (currentInput.length < 3) {
currentInput += self.number.toString();
updateInputDisplay();
}
};
self.up = function (x, y, obj) {
keyGraphic.alpha = 1;
tween(keyGraphic, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
};
return self;
});
var SpecialKey = Container.expand(function (type) {
var self = Container.call(this);
var keyGraphic = self.attachAsset(type === 'delete' ? 'deleteKey' : 'enterKey', {
anchorX: 0.5,
anchorY: 0.5
});
var keyText = new Text2(type === 'delete' ? "←" : "✓", {
size: 90,
fill: 0xFFFFFF
});
keyText.anchor.set(0.5, 0.5);
self.addChild(keyText);
self.keyType = type;
self.down = function (x, y, obj) {
keyGraphic.alpha = 0.7;
tween(keyGraphic, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
if (type === 'delete') {
if (currentInput.length > 0) {
currentInput = currentInput.slice(0, -1);
updateInputDisplay();
}
} else if (type === 'enter') {
checkAnswer();
}
};
self.up = function (x, y, obj) {
keyGraphic.alpha = 1;
tween(keyGraphic, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xededed
});
/****
* Game Code
****/
// Global game variables
var currentTable = 1;
var currentQuestion = 0;
var score = 0;
var lives = 3;
var timer = 0;
var timerMax = 15;
var timerInterval;
var currentInput = "";
var hearts = [];
var confetti = [];
var questions = [];
var keypad = [];
var isGameOver = false;
var isLevelComplete = false;
// Game UI Elements
var panel = LK.getAsset('panel', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(panel);
// Score text
var scoreTxt = new Text2('Score: 0', {
size: 70,
fill: 0x333333
});
scoreTxt.anchor.set(0, 0);
scoreTxt.x = 50;
scoreTxt.y = 50;
game.addChild(scoreTxt);
// Lives
for (var i = 0; i < 3; i++) {
var heart = new Heart();
heart.x = 2048 - 70 - i * 140;
heart.y = 70;
hearts.push(heart);
game.addChild(heart);
}
// Table display
var tableTxt = new Text2('Table: 1', {
size: 70,
fill: 0x333333
});
tableTxt.anchor.set(0.5, 0);
tableTxt.x = 2048 / 2;
tableTxt.y = 50;
game.addChild(tableTxt);
// Timer bar background
var timerBg = LK.getAsset('panel', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 150,
width: 1948,
height: 50,
tint: 0xcccccc
});
game.addChild(timerBg);
// Timer bar fill
var timerFill = LK.getAsset('timer', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 150,
tint: 0x42f54e
});
game.addChild(timerFill);
// Question display
var questionTxt = new Text2('', {
size: 120,
fill: 0x000000
});
questionTxt.anchor.set(0.5, 0.5);
questionTxt.x = 2048 / 2;
questionTxt.y = 500;
game.addChild(questionTxt);
// Answer input display
var inputBox = LK.getAsset('inputBox', {
anchorX: 0.5,
anchorY: -0.5,
x: 2048 / 2,
y: 600
});
game.addChild(inputBox);
var inputTxt = new Text2('', {
size: 100,
fill: 0x333333
});
inputTxt.anchor.set(0.5, 0.5);
inputTxt.x = 2048 / 2;
inputTxt.y = 600;
game.addChild(inputTxt);
// Keypad
var keypadStartY = 900;
var keypadWidth = 700;
var keySize = 250;
// Create number keys 1-9
for (var row = 0; row < 3; row++) {
for (var col = 0; col < 3; col++) {
var num = row * 3 + col + 1;
var key = new NumberKey(num);
key.x = 2048 / 2 - keypadWidth / 2 + col * keySize + keySize / 2;
key.y = keypadStartY + row * keySize + keySize / 2;
keypad.push(key);
game.addChild(key);
}
}
// Create 0 key
var zeroKey = new NumberKey(0);
zeroKey.x = 2048 / 2 - keypadWidth / 2 + keySize * 1.5;
zeroKey.y = keypadStartY + 3 * keySize + keySize / 2;
keypad.push(zeroKey);
game.addChild(zeroKey);
// Create delete key
var deleteKey = new SpecialKey('delete');
deleteKey.x = 2048 / 2 - keypadWidth / 2 + keySize / 2;
deleteKey.y = keypadStartY + 3 * keySize + keySize / 2;
keypad.push(deleteKey);
game.addChild(deleteKey);
// Create enter key
var enterKey = new SpecialKey('enter');
enterKey.x = 2048 / 2 - keypadWidth / 2 + keySize * 2.5;
enterKey.y = keypadStartY + 3 * keySize + keySize / 2;
keypad.push(enterKey);
game.addChild(enterKey);
// Level complete UI
var levelCompletePanel = LK.getAsset('panel', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
width: 1600,
height: 900,
visible: false
});
game.addChild(levelCompletePanel);
var levelCompleteTxt = new Text2('', {
size: 90,
fill: 0x333333
});
levelCompleteTxt.anchor.set(0.5, 0.5);
levelCompleteTxt.x = 2048 / 2;
levelCompleteTxt.y = 2732 / 2 - 200;
levelCompleteTxt.visible = false;
game.addChild(levelCompleteTxt);
var continueBtn = LK.getAsset('button', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 + 100,
visible: false
});
game.addChild(continueBtn);
var continueTxt = new Text2('Continue', {
size: 70,
fill: 0xFFFFFF
});
continueTxt.anchor.set(0.5, 0.5);
continueTxt.x = 2048 / 2;
continueTxt.y = 2732 / 2 + 100;
continueTxt.visible = false;
game.addChild(continueTxt);
// Game logic functions
function generateQuestions() {
questions = [];
for (var i = 1; i <= 10; i++) {
questions.push({
num1: currentTable,
num2: i,
answer: currentTable * i
});
}
// Shuffle questions
for (var i = questions.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = questions[i];
questions[i] = questions[j];
questions[j] = temp;
}
currentQuestion = 0;
showQuestion();
}
function showQuestion() {
if (currentQuestion >= questions.length) {
showLevelComplete();
return;
}
var q = questions[currentQuestion];
questionTxt.setText(q.num1 + " × " + q.num2 + " = ?");
currentInput = "";
updateInputDisplay();
// Reset and start timer
timer = timerMax;
updateTimerDisplay();
LK.clearInterval(timerInterval);
timerInterval = LK.setInterval(updateTimer, 100);
}
function updateTimer() {
timer -= 0.1;
updateTimerDisplay();
if (timer <= 0) {
LK.clearInterval(timerInterval);
handleWrongAnswer();
}
}
function updateTimerDisplay() {
var percentage = Math.max(0, timer / timerMax);
timerFill.width = 1948 * percentage;
// Change color based on time left
if (percentage > 0.6) {
timerFill.tint = 0x42f54e; // Green
} else if (percentage > 0.3) {
timerFill.tint = 0xf5d442; // Yellow
} else {
timerFill.tint = 0xf54242; // Red
}
}
function updateInputDisplay() {
inputTxt.setText(currentInput);
}
function checkAnswer() {
if (currentInput === "") {
return;
}
var userAnswer = parseInt(currentInput, 10);
var correctAnswer = questions[currentQuestion].answer;
if (userAnswer === correctAnswer) {
handleCorrectAnswer();
} else {
handleWrongAnswer();
}
}
function handleCorrectAnswer() {
LK.clearInterval(timerInterval);
LK.getSound('correct').play();
score += Math.ceil(timer); // More points for faster answers
scoreTxt.setText('Score: ' + score);
// Flash input box green
// Display example of correct answer feedback
var correctAnswerExample = new Text2('Correct! ' + questions[currentQuestion].num1 + ' × ' + questions[currentQuestion].num2 + ' = ' + questions[currentQuestion].answer, {
size: 70,
fill: 0x42f54e
});
correctAnswerExample.anchor.set(0.5, 0.5);
correctAnswerExample.x = 2048 / 2;
correctAnswerExample.y = 800;
game.addChild(correctAnswerExample);
tween(correctAnswerExample, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
correctAnswerExample.destroy();
}
});
tween(inputBox, {
tint: 0x42f54e
}, {
duration: 300,
onFinish: function onFinish() {
tween(inputBox, {
tint: 0xffffff
}, {
duration: 300
});
}
});
// Check if the player has guessed all questions correctly without any errors
if (lives === 3) {
// Increase health count or provide a bonus
console.log("Bonus! No errors made.");
// You can add additional logic here to increase health or provide a bonus
}
currentQuestion++;
// Move to next question after a short delay
LK.setTimeout(function () {
showQuestion();
}, 700);
}
function handleWrongAnswer() {
LK.clearInterval(timerInterval);
LK.getSound('incorrect').play();
// Show correct answer
var correctAnswerText = questions[currentQuestion].num1 + " × " + questions[currentQuestion].num2 + " = " + questions[currentQuestion].answer;
inputTxt.setText(correctAnswerText);
// Flash input box red
tween(inputBox, {
tint: 0xf54242
}, {
duration: 300,
onFinish: function onFinish() {
tween(inputBox, {
tint: 0xffffff
}, {
duration: 300
});
}
});
// Decrease lives
lives--;
hearts[lives].setActive(false);
if (lives <= 0) {
// Game over
LK.setTimeout(function () {
isGameOver = true;
// Save high score if applicable
if (score > storage.highScore) {
storage.highScore = score;
}
LK.showGameOver();
}, 1500);
} else {
// Continue to next question after showing the correct answer
currentQuestion++;
LK.setTimeout(function () {
showQuestion();
}, 1500);
}
}
function showLevelComplete() {
isLevelComplete = true;
LK.clearInterval(timerInterval);
LK.getSound('levelComplete').play();
// Mark this table as completed in storage
storage.completedTables[currentTable] = true;
levelCompletePanel.visible = true;
levelCompleteTxt.setText("Great job!\nYou've mastered the " + currentTable + " times table!");
levelCompleteTxt.visible = true;
continueBtn.visible = true;
continueTxt.visible = true;
// Create celebration confetti
for (var i = 0; i < 50; i++) {
createConfetti();
}
// Create more confetti every 300ms
var confettiInterval = LK.setInterval(function () {
for (var i = 0; i < 10; i++) {
createConfetti();
}
}, 300);
// Stop creating confetti after 2 seconds
LK.setTimeout(function () {
LK.clearInterval(confettiInterval);
}, 2000);
}
function createConfetti() {
var colors = [0xf54242, 0x42f54e, 0x4287f5, 0xf5d442, 0xf542f5];
var conf = new Confetti(colors[Math.floor(Math.random() * colors.length)]);
conf.x = Math.random() * 2048;
conf.y = Math.random() * 1000;
confetti.push(conf);
game.addChild(conf);
}
function startNextLevel() {
// Increase difficulty
currentTable++;
if (currentTable > 20) {
// Player completed all tables
LK.showYouWin();
return;
}
// Reduce timer max for higher tables to increase difficulty
timerMax = Math.max(5, 15 - Math.floor(currentTable / 4));
// Reset UI
tableTxt.setText('Table: ' + currentTable);
levelCompletePanel.visible = false;
levelCompleteTxt.visible = false;
continueBtn.visible = false;
continueTxt.visible = false;
isLevelComplete = false;
// Generate new questions
generateQuestions();
}
// Event handlers
continueBtn.down = function (x, y, obj) {
tween(continueBtn, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100
});
};
continueBtn.up = function (x, y, obj) {
tween(continueBtn, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
startNextLevel();
};
// Update function
game.update = function () {
// Update confetti if level complete is showing
if (isLevelComplete) {
for (var i = 0; i < confetti.length; i++) {
confetti[i].update();
}
}
};
// Start game
function initGame() {
// Reset game variables
currentTable = 1;
score = 0;
lives = 3;
isGameOver = false;
isLevelComplete = false;
// Update UI
scoreTxt.setText('Score: 0');
tableTxt.setText('Table: ' + currentTable);
// Reset hearts
for (var i = 0; i < hearts.length; i++) {
hearts[i].setActive(true);
}
// Hide level complete UI
levelCompletePanel.visible = false;
levelCompleteTxt.visible = false;
continueBtn.visible = false;
continueTxt.visible = false;
// Remove any confetti
for (var i = confetti.length - 1; i >= 0; i--) {
confetti[i].destroy();
confetti.splice(i, 1);
}
// Start with the current table
generateQuestions();
// Play background music
LK.playMusic('bgMusic');
}
// Initialize the game
initGame(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
highScore: 0,
completedTables: {}
});
/****
* Classes
****/
var Confetti = Container.expand(function (color) {
var self = Container.call(this);
var confettiGraphic = self.attachAsset('confetti', {
anchorX: 0.5,
anchorY: 0.5
});
confettiGraphic.tint = color || 0xf54242;
self.vx = Math.random() * 10 - 5;
self.vy = Math.random() * -15 - 5;
self.vr = (Math.random() - 0.5) * 0.2;
self.gravity = 0.3;
self.update = function () {
self.x += self.vx;
self.y += self.vy;
self.vy += self.gravity;
confettiGraphic.rotation += self.vr;
if (self.y > 2732) {
self.destroy();
var index = confetti.indexOf(self);
if (index > -1) {
confetti.splice(index, 1);
}
}
};
return self;
});
var Heart = Container.expand(function () {
var self = Container.call(this);
var heartGraphic = self.attachAsset('heart', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 1,
scaleY: 1
});
self.setActive = function (active) {
heartGraphic.alpha = active ? 1 : 0.3;
};
return self;
});
var NumberKey = Container.expand(function (number) {
var self = Container.call(this);
var keyGraphic = self.attachAsset('numberKey', {
anchorX: 0.5,
anchorY: 0.5
});
var keyText = new Text2(number.toString(), {
size: 90,
fill: 0x000000
});
keyText.anchor.set(0.5, 0.5);
self.addChild(keyText);
self.number = number;
self.down = function (x, y, obj) {
keyGraphic.alpha = 0.7;
tween(keyGraphic, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
if (currentInput.length < 3) {
currentInput += self.number.toString();
updateInputDisplay();
}
};
self.up = function (x, y, obj) {
keyGraphic.alpha = 1;
tween(keyGraphic, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
};
return self;
});
var SpecialKey = Container.expand(function (type) {
var self = Container.call(this);
var keyGraphic = self.attachAsset(type === 'delete' ? 'deleteKey' : 'enterKey', {
anchorX: 0.5,
anchorY: 0.5
});
var keyText = new Text2(type === 'delete' ? "←" : "✓", {
size: 90,
fill: 0xFFFFFF
});
keyText.anchor.set(0.5, 0.5);
self.addChild(keyText);
self.keyType = type;
self.down = function (x, y, obj) {
keyGraphic.alpha = 0.7;
tween(keyGraphic, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 100
});
if (type === 'delete') {
if (currentInput.length > 0) {
currentInput = currentInput.slice(0, -1);
updateInputDisplay();
}
} else if (type === 'enter') {
checkAnswer();
}
};
self.up = function (x, y, obj) {
keyGraphic.alpha = 1;
tween(keyGraphic, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0xededed
});
/****
* Game Code
****/
// Global game variables
var currentTable = 1;
var currentQuestion = 0;
var score = 0;
var lives = 3;
var timer = 0;
var timerMax = 15;
var timerInterval;
var currentInput = "";
var hearts = [];
var confetti = [];
var questions = [];
var keypad = [];
var isGameOver = false;
var isLevelComplete = false;
// Game UI Elements
var panel = LK.getAsset('panel', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2
});
game.addChild(panel);
// Score text
var scoreTxt = new Text2('Score: 0', {
size: 70,
fill: 0x333333
});
scoreTxt.anchor.set(0, 0);
scoreTxt.x = 50;
scoreTxt.y = 50;
game.addChild(scoreTxt);
// Lives
for (var i = 0; i < 3; i++) {
var heart = new Heart();
heart.x = 2048 - 70 - i * 140;
heart.y = 70;
hearts.push(heart);
game.addChild(heart);
}
// Table display
var tableTxt = new Text2('Table: 1', {
size: 70,
fill: 0x333333
});
tableTxt.anchor.set(0.5, 0);
tableTxt.x = 2048 / 2;
tableTxt.y = 50;
game.addChild(tableTxt);
// Timer bar background
var timerBg = LK.getAsset('panel', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 150,
width: 1948,
height: 50,
tint: 0xcccccc
});
game.addChild(timerBg);
// Timer bar fill
var timerFill = LK.getAsset('timer', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 150,
tint: 0x42f54e
});
game.addChild(timerFill);
// Question display
var questionTxt = new Text2('', {
size: 120,
fill: 0x000000
});
questionTxt.anchor.set(0.5, 0.5);
questionTxt.x = 2048 / 2;
questionTxt.y = 500;
game.addChild(questionTxt);
// Answer input display
var inputBox = LK.getAsset('inputBox', {
anchorX: 0.5,
anchorY: -0.5,
x: 2048 / 2,
y: 600
});
game.addChild(inputBox);
var inputTxt = new Text2('', {
size: 100,
fill: 0x333333
});
inputTxt.anchor.set(0.5, 0.5);
inputTxt.x = 2048 / 2;
inputTxt.y = 600;
game.addChild(inputTxt);
// Keypad
var keypadStartY = 900;
var keypadWidth = 700;
var keySize = 250;
// Create number keys 1-9
for (var row = 0; row < 3; row++) {
for (var col = 0; col < 3; col++) {
var num = row * 3 + col + 1;
var key = new NumberKey(num);
key.x = 2048 / 2 - keypadWidth / 2 + col * keySize + keySize / 2;
key.y = keypadStartY + row * keySize + keySize / 2;
keypad.push(key);
game.addChild(key);
}
}
// Create 0 key
var zeroKey = new NumberKey(0);
zeroKey.x = 2048 / 2 - keypadWidth / 2 + keySize * 1.5;
zeroKey.y = keypadStartY + 3 * keySize + keySize / 2;
keypad.push(zeroKey);
game.addChild(zeroKey);
// Create delete key
var deleteKey = new SpecialKey('delete');
deleteKey.x = 2048 / 2 - keypadWidth / 2 + keySize / 2;
deleteKey.y = keypadStartY + 3 * keySize + keySize / 2;
keypad.push(deleteKey);
game.addChild(deleteKey);
// Create enter key
var enterKey = new SpecialKey('enter');
enterKey.x = 2048 / 2 - keypadWidth / 2 + keySize * 2.5;
enterKey.y = keypadStartY + 3 * keySize + keySize / 2;
keypad.push(enterKey);
game.addChild(enterKey);
// Level complete UI
var levelCompletePanel = LK.getAsset('panel', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2,
width: 1600,
height: 900,
visible: false
});
game.addChild(levelCompletePanel);
var levelCompleteTxt = new Text2('', {
size: 90,
fill: 0x333333
});
levelCompleteTxt.anchor.set(0.5, 0.5);
levelCompleteTxt.x = 2048 / 2;
levelCompleteTxt.y = 2732 / 2 - 200;
levelCompleteTxt.visible = false;
game.addChild(levelCompleteTxt);
var continueBtn = LK.getAsset('button', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 2732 / 2 + 100,
visible: false
});
game.addChild(continueBtn);
var continueTxt = new Text2('Continue', {
size: 70,
fill: 0xFFFFFF
});
continueTxt.anchor.set(0.5, 0.5);
continueTxt.x = 2048 / 2;
continueTxt.y = 2732 / 2 + 100;
continueTxt.visible = false;
game.addChild(continueTxt);
// Game logic functions
function generateQuestions() {
questions = [];
for (var i = 1; i <= 10; i++) {
questions.push({
num1: currentTable,
num2: i,
answer: currentTable * i
});
}
// Shuffle questions
for (var i = questions.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = questions[i];
questions[i] = questions[j];
questions[j] = temp;
}
currentQuestion = 0;
showQuestion();
}
function showQuestion() {
if (currentQuestion >= questions.length) {
showLevelComplete();
return;
}
var q = questions[currentQuestion];
questionTxt.setText(q.num1 + " × " + q.num2 + " = ?");
currentInput = "";
updateInputDisplay();
// Reset and start timer
timer = timerMax;
updateTimerDisplay();
LK.clearInterval(timerInterval);
timerInterval = LK.setInterval(updateTimer, 100);
}
function updateTimer() {
timer -= 0.1;
updateTimerDisplay();
if (timer <= 0) {
LK.clearInterval(timerInterval);
handleWrongAnswer();
}
}
function updateTimerDisplay() {
var percentage = Math.max(0, timer / timerMax);
timerFill.width = 1948 * percentage;
// Change color based on time left
if (percentage > 0.6) {
timerFill.tint = 0x42f54e; // Green
} else if (percentage > 0.3) {
timerFill.tint = 0xf5d442; // Yellow
} else {
timerFill.tint = 0xf54242; // Red
}
}
function updateInputDisplay() {
inputTxt.setText(currentInput);
}
function checkAnswer() {
if (currentInput === "") {
return;
}
var userAnswer = parseInt(currentInput, 10);
var correctAnswer = questions[currentQuestion].answer;
if (userAnswer === correctAnswer) {
handleCorrectAnswer();
} else {
handleWrongAnswer();
}
}
function handleCorrectAnswer() {
LK.clearInterval(timerInterval);
LK.getSound('correct').play();
score += Math.ceil(timer); // More points for faster answers
scoreTxt.setText('Score: ' + score);
// Flash input box green
// Display example of correct answer feedback
var correctAnswerExample = new Text2('Correct! ' + questions[currentQuestion].num1 + ' × ' + questions[currentQuestion].num2 + ' = ' + questions[currentQuestion].answer, {
size: 70,
fill: 0x42f54e
});
correctAnswerExample.anchor.set(0.5, 0.5);
correctAnswerExample.x = 2048 / 2;
correctAnswerExample.y = 800;
game.addChild(correctAnswerExample);
tween(correctAnswerExample, {
alpha: 0
}, {
duration: 1000,
onFinish: function onFinish() {
correctAnswerExample.destroy();
}
});
tween(inputBox, {
tint: 0x42f54e
}, {
duration: 300,
onFinish: function onFinish() {
tween(inputBox, {
tint: 0xffffff
}, {
duration: 300
});
}
});
// Check if the player has guessed all questions correctly without any errors
if (lives === 3) {
// Increase health count or provide a bonus
console.log("Bonus! No errors made.");
// You can add additional logic here to increase health or provide a bonus
}
currentQuestion++;
// Move to next question after a short delay
LK.setTimeout(function () {
showQuestion();
}, 700);
}
function handleWrongAnswer() {
LK.clearInterval(timerInterval);
LK.getSound('incorrect').play();
// Show correct answer
var correctAnswerText = questions[currentQuestion].num1 + " × " + questions[currentQuestion].num2 + " = " + questions[currentQuestion].answer;
inputTxt.setText(correctAnswerText);
// Flash input box red
tween(inputBox, {
tint: 0xf54242
}, {
duration: 300,
onFinish: function onFinish() {
tween(inputBox, {
tint: 0xffffff
}, {
duration: 300
});
}
});
// Decrease lives
lives--;
hearts[lives].setActive(false);
if (lives <= 0) {
// Game over
LK.setTimeout(function () {
isGameOver = true;
// Save high score if applicable
if (score > storage.highScore) {
storage.highScore = score;
}
LK.showGameOver();
}, 1500);
} else {
// Continue to next question after showing the correct answer
currentQuestion++;
LK.setTimeout(function () {
showQuestion();
}, 1500);
}
}
function showLevelComplete() {
isLevelComplete = true;
LK.clearInterval(timerInterval);
LK.getSound('levelComplete').play();
// Mark this table as completed in storage
storage.completedTables[currentTable] = true;
levelCompletePanel.visible = true;
levelCompleteTxt.setText("Great job!\nYou've mastered the " + currentTable + " times table!");
levelCompleteTxt.visible = true;
continueBtn.visible = true;
continueTxt.visible = true;
// Create celebration confetti
for (var i = 0; i < 50; i++) {
createConfetti();
}
// Create more confetti every 300ms
var confettiInterval = LK.setInterval(function () {
for (var i = 0; i < 10; i++) {
createConfetti();
}
}, 300);
// Stop creating confetti after 2 seconds
LK.setTimeout(function () {
LK.clearInterval(confettiInterval);
}, 2000);
}
function createConfetti() {
var colors = [0xf54242, 0x42f54e, 0x4287f5, 0xf5d442, 0xf542f5];
var conf = new Confetti(colors[Math.floor(Math.random() * colors.length)]);
conf.x = Math.random() * 2048;
conf.y = Math.random() * 1000;
confetti.push(conf);
game.addChild(conf);
}
function startNextLevel() {
// Increase difficulty
currentTable++;
if (currentTable > 20) {
// Player completed all tables
LK.showYouWin();
return;
}
// Reduce timer max for higher tables to increase difficulty
timerMax = Math.max(5, 15 - Math.floor(currentTable / 4));
// Reset UI
tableTxt.setText('Table: ' + currentTable);
levelCompletePanel.visible = false;
levelCompleteTxt.visible = false;
continueBtn.visible = false;
continueTxt.visible = false;
isLevelComplete = false;
// Generate new questions
generateQuestions();
}
// Event handlers
continueBtn.down = function (x, y, obj) {
tween(continueBtn, {
scaleX: 0.95,
scaleY: 0.95
}, {
duration: 100
});
};
continueBtn.up = function (x, y, obj) {
tween(continueBtn, {
scaleX: 1,
scaleY: 1
}, {
duration: 100
});
startNextLevel();
};
// Update function
game.update = function () {
// Update confetti if level complete is showing
if (isLevelComplete) {
for (var i = 0; i < confetti.length; i++) {
confetti[i].update();
}
}
};
// Start game
function initGame() {
// Reset game variables
currentTable = 1;
score = 0;
lives = 3;
isGameOver = false;
isLevelComplete = false;
// Update UI
scoreTxt.setText('Score: 0');
tableTxt.setText('Table: ' + currentTable);
// Reset hearts
for (var i = 0; i < hearts.length; i++) {
hearts[i].setActive(true);
}
// Hide level complete UI
levelCompletePanel.visible = false;
levelCompleteTxt.visible = false;
continueBtn.visible = false;
continueTxt.visible = false;
// Remove any confetti
for (var i = confetti.length - 1; i >= 0; i--) {
confetti[i].destroy();
confetti.splice(i, 1);
}
// Start with the current table
generateQuestions();
// Play background music
LK.playMusic('bgMusic');
}
// Initialize the game
initGame();