/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var AnswerButton = Container.expand(function () { var self = Container.call(this); var buttonGraphics = self.attachAsset('answerButton', { anchorX: 0.5, anchorY: 0.5 }); self.answer = 0; self.isCorrect = false; self.answerText = new Text2('', { size: 32, fill: 0x000000 }); self.answerText.anchor.set(0.5, 0.5); self.addChild(self.answerText); self.setAnswer = function (answer) { self.answer = answer; self.answerText.setText(answer.toString()); }; self.flashCorrect = function () { var correctGraphics = self.attachAsset('correctButton', { anchorX: 0.5, anchorY: 0.5 }); tween(correctGraphics, { alpha: 0 }, { duration: 500, onFinish: function onFinish() { if (correctGraphics.parent) { correctGraphics.parent.removeChild(correctGraphics); } } }); }; self.flashWrong = function () { var wrongGraphics = self.attachAsset('wrongButton', { anchorX: 0.5, anchorY: 0.5 }); tween(wrongGraphics, { alpha: 0 }, { duration: 500, onFinish: function onFinish() { if (wrongGraphics.parent) { wrongGraphics.parent.removeChild(wrongGraphics); } } }); }; self.down = function (x, y, obj) { checkAnswer(self.answer); }; return self; }); var FallingNumber = Container.expand(function () { var self = Container.call(this); var numberGraphics = self.attachAsset('fallingNumber', { anchorX: 0.5, anchorY: 0.5 }); self.speed = 2; self.equation = ""; self.correctAnswer = 0; self.answered = false; self.equationText = new Text2('', { size: 24, fill: 0xFFFFFF }); self.equationText.anchor.set(0.5, 0.5); self.addChild(self.equationText); self.setEquation = function (equation, answer) { self.equation = equation; self.correctAnswer = answer; self.equationText.setText(equation); }; self.destroy = function () { if (self.parent) { self.parent.removeChild(self); } }; self.update = function () { self.y += self.speed; }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x1a1a2e }); /**** * Game Code ****/ var fallingNumbers = []; var answerButtons = []; var currentEquation = null; var currentAnswers = []; var score = 0; var health = 100; var maxHealth = 100; var difficulty = 1; var gameSpeed = 1; var nextNumberTimer = 0; var numberSpawnRate = 180; // frames between spawns // Create base var base = game.addChild(LK.getAsset('base', { anchorX: 0.5, anchorY: 0.5 })); base.x = 2048 / 2; base.y = 2732 - 60; // Create health bar background var healthBarBg = LK.getAsset('healthBar', { anchorX: 0, anchorY: 0, color: 0x333333 }); healthBarBg.x = 50; healthBarBg.y = 50; LK.gui.topLeft.addChild(healthBarBg); // Create health bar var healthBar = LK.getAsset('healthBar', { anchorX: 0, anchorY: 0 }); healthBar.x = 50; healthBar.y = 50; LK.gui.topLeft.addChild(healthBar); // Create score text var scoreTxt = new Text2('Score: 0', { size: 48, fill: 0xFFFFFF }); scoreTxt.anchor.set(0.5, 0); LK.gui.top.addChild(scoreTxt); // Create answer buttons for (var i = 0; i < 4; i++) { var button = new AnswerButton(); button.x = 400 + i * 300; button.y = 2732 - 150; answerButtons.push(button); game.addChild(button); } // Math operations var operations = ['+', '-', '*', '/']; function generateEquation() { var operation = operations[Math.floor(Math.random() * Math.min(difficulty, operations.length))]; var num1, num2, answer; switch (operation) { case '+': num1 = Math.floor(Math.random() * 20) + 1; num2 = Math.floor(Math.random() * 20) + 1; answer = num1 + num2; break; case '-': num1 = Math.floor(Math.random() * 20) + 10; num2 = Math.floor(Math.random() * num1) + 1; answer = num1 - num2; break; case '*': num1 = Math.floor(Math.random() * 10) + 1; num2 = Math.floor(Math.random() * 10) + 1; answer = num1 * num2; break; case '/': answer = Math.floor(Math.random() * 10) + 1; num2 = Math.floor(Math.random() * 5) + 1; num1 = answer * num2; break; } return { equation: num1 + ' ' + operation + ' ' + num2 + ' = ?', answer: answer }; } function generateAnswers(correctAnswer) { var answers = [correctAnswer]; var wrongAnswers = []; // Generate 3 wrong answers for (var i = 0; i < 3; i++) { var wrongAnswer; do { wrongAnswer = correctAnswer + Math.floor(Math.random() * 20) - 10; } while (wrongAnswer === correctAnswer || wrongAnswers.indexOf(wrongAnswer) !== -1 || wrongAnswer < 0); wrongAnswers.push(wrongAnswer); } answers = answers.concat(wrongAnswers); // Shuffle answers for (var i = answers.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = answers[i]; answers[i] = answers[j]; answers[j] = temp; } return answers; } function spawnFallingNumber() { var equationData = generateEquation(); var fallingNumber = new FallingNumber(); fallingNumber.x = Math.random() * (2048 - 200) + 100; fallingNumber.y = -50; fallingNumber.speed = 1 + difficulty * 0.5; fallingNumber.setEquation(equationData.equation, equationData.answer); fallingNumbers.push(fallingNumber); game.addChild(fallingNumber); currentEquation = fallingNumber; currentAnswers = generateAnswers(equationData.answer); // Update answer buttons for (var i = 0; i < 4; i++) { answerButtons[i].setAnswer(currentAnswers[i]); answerButtons[i].isCorrect = currentAnswers[i] === equationData.answer; } } function checkAnswer(selectedAnswer) { if (!currentEquation || currentEquation.answered) return; currentEquation.answered = true; if (selectedAnswer === currentEquation.correctAnswer) { // Correct answer LK.getSound('correct').play(); score += 10 * difficulty; LK.setScore(score); scoreTxt.setText('Score: ' + score); // Flash correct button for (var i = 0; i < answerButtons.length; i++) { if (answerButtons[i].answer === selectedAnswer) { answerButtons[i].flashCorrect(); break; } } // Remove the number var index = fallingNumbers.indexOf(currentEquation); if (index !== -1) { fallingNumbers.splice(index, 1); currentEquation.destroy(); } LK.effects.flashObject(currentEquation, 0x00FF00, 300); } else { // Wrong answer LK.getSound('wrong').play(); health -= 10; // Flash wrong button for (var i = 0; i < answerButtons.length; i++) { if (answerButtons[i].answer === selectedAnswer) { answerButtons[i].flashWrong(); break; } } LK.effects.flashScreen(0xFF0000, 500); } updateHealthBar(); currentEquation = null; } function updateHealthBar() { var healthPercent = health / maxHealth; healthBar.width = 300 * healthPercent; if (health <= 0) { LK.showGameOver(); } } function increaseDifficulty() { if (score > 0 && score % 100 === 0) { difficulty = Math.min(4, Math.floor(score / 100) + 1); numberSpawnRate = Math.max(60, 180 - difficulty * 30); } } game.update = function () { nextNumberTimer++; // Spawn new numbers if (nextNumberTimer >= numberSpawnRate && !currentEquation) { spawnFallingNumber(); nextNumberTimer = 0; } // Update falling numbers for (var i = fallingNumbers.length - 1; i >= 0; i--) { var number = fallingNumbers[i]; if (number.lastY === undefined) number.lastY = number.y; // Check if number reached the base if (number.lastY < base.y - 50 && number.y >= base.y - 50 && !number.answered) { LK.getSound('hit').play(); health -= 15; updateHealthBar(); fallingNumbers.splice(i, 1); number.destroy(); if (number === currentEquation) { currentEquation = null; } LK.effects.flashScreen(0xFF4500, 400); } // Remove numbers that are off screen if (number.y > 2732 + 100) { fallingNumbers.splice(i, 1); number.destroy(); if (number === currentEquation) { currentEquation = null; } } number.lastY = number.y; } increaseDifficulty(); };
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var AnswerButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('answerButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.answer = 0;
self.isCorrect = false;
self.answerText = new Text2('', {
size: 32,
fill: 0x000000
});
self.answerText.anchor.set(0.5, 0.5);
self.addChild(self.answerText);
self.setAnswer = function (answer) {
self.answer = answer;
self.answerText.setText(answer.toString());
};
self.flashCorrect = function () {
var correctGraphics = self.attachAsset('correctButton', {
anchorX: 0.5,
anchorY: 0.5
});
tween(correctGraphics, {
alpha: 0
}, {
duration: 500,
onFinish: function onFinish() {
if (correctGraphics.parent) {
correctGraphics.parent.removeChild(correctGraphics);
}
}
});
};
self.flashWrong = function () {
var wrongGraphics = self.attachAsset('wrongButton', {
anchorX: 0.5,
anchorY: 0.5
});
tween(wrongGraphics, {
alpha: 0
}, {
duration: 500,
onFinish: function onFinish() {
if (wrongGraphics.parent) {
wrongGraphics.parent.removeChild(wrongGraphics);
}
}
});
};
self.down = function (x, y, obj) {
checkAnswer(self.answer);
};
return self;
});
var FallingNumber = Container.expand(function () {
var self = Container.call(this);
var numberGraphics = self.attachAsset('fallingNumber', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2;
self.equation = "";
self.correctAnswer = 0;
self.answered = false;
self.equationText = new Text2('', {
size: 24,
fill: 0xFFFFFF
});
self.equationText.anchor.set(0.5, 0.5);
self.addChild(self.equationText);
self.setEquation = function (equation, answer) {
self.equation = equation;
self.correctAnswer = answer;
self.equationText.setText(equation);
};
self.destroy = function () {
if (self.parent) {
self.parent.removeChild(self);
}
};
self.update = function () {
self.y += self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a2e
});
/****
* Game Code
****/
var fallingNumbers = [];
var answerButtons = [];
var currentEquation = null;
var currentAnswers = [];
var score = 0;
var health = 100;
var maxHealth = 100;
var difficulty = 1;
var gameSpeed = 1;
var nextNumberTimer = 0;
var numberSpawnRate = 180; // frames between spawns
// Create base
var base = game.addChild(LK.getAsset('base', {
anchorX: 0.5,
anchorY: 0.5
}));
base.x = 2048 / 2;
base.y = 2732 - 60;
// Create health bar background
var healthBarBg = LK.getAsset('healthBar', {
anchorX: 0,
anchorY: 0,
color: 0x333333
});
healthBarBg.x = 50;
healthBarBg.y = 50;
LK.gui.topLeft.addChild(healthBarBg);
// Create health bar
var healthBar = LK.getAsset('healthBar', {
anchorX: 0,
anchorY: 0
});
healthBar.x = 50;
healthBar.y = 50;
LK.gui.topLeft.addChild(healthBar);
// Create score text
var scoreTxt = new Text2('Score: 0', {
size: 48,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create answer buttons
for (var i = 0; i < 4; i++) {
var button = new AnswerButton();
button.x = 400 + i * 300;
button.y = 2732 - 150;
answerButtons.push(button);
game.addChild(button);
}
// Math operations
var operations = ['+', '-', '*', '/'];
function generateEquation() {
var operation = operations[Math.floor(Math.random() * Math.min(difficulty, operations.length))];
var num1, num2, answer;
switch (operation) {
case '+':
num1 = Math.floor(Math.random() * 20) + 1;
num2 = Math.floor(Math.random() * 20) + 1;
answer = num1 + num2;
break;
case '-':
num1 = Math.floor(Math.random() * 20) + 10;
num2 = Math.floor(Math.random() * num1) + 1;
answer = num1 - num2;
break;
case '*':
num1 = Math.floor(Math.random() * 10) + 1;
num2 = Math.floor(Math.random() * 10) + 1;
answer = num1 * num2;
break;
case '/':
answer = Math.floor(Math.random() * 10) + 1;
num2 = Math.floor(Math.random() * 5) + 1;
num1 = answer * num2;
break;
}
return {
equation: num1 + ' ' + operation + ' ' + num2 + ' = ?',
answer: answer
};
}
function generateAnswers(correctAnswer) {
var answers = [correctAnswer];
var wrongAnswers = [];
// Generate 3 wrong answers
for (var i = 0; i < 3; i++) {
var wrongAnswer;
do {
wrongAnswer = correctAnswer + Math.floor(Math.random() * 20) - 10;
} while (wrongAnswer === correctAnswer || wrongAnswers.indexOf(wrongAnswer) !== -1 || wrongAnswer < 0);
wrongAnswers.push(wrongAnswer);
}
answers = answers.concat(wrongAnswers);
// Shuffle answers
for (var i = answers.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = answers[i];
answers[i] = answers[j];
answers[j] = temp;
}
return answers;
}
function spawnFallingNumber() {
var equationData = generateEquation();
var fallingNumber = new FallingNumber();
fallingNumber.x = Math.random() * (2048 - 200) + 100;
fallingNumber.y = -50;
fallingNumber.speed = 1 + difficulty * 0.5;
fallingNumber.setEquation(equationData.equation, equationData.answer);
fallingNumbers.push(fallingNumber);
game.addChild(fallingNumber);
currentEquation = fallingNumber;
currentAnswers = generateAnswers(equationData.answer);
// Update answer buttons
for (var i = 0; i < 4; i++) {
answerButtons[i].setAnswer(currentAnswers[i]);
answerButtons[i].isCorrect = currentAnswers[i] === equationData.answer;
}
}
function checkAnswer(selectedAnswer) {
if (!currentEquation || currentEquation.answered) return;
currentEquation.answered = true;
if (selectedAnswer === currentEquation.correctAnswer) {
// Correct answer
LK.getSound('correct').play();
score += 10 * difficulty;
LK.setScore(score);
scoreTxt.setText('Score: ' + score);
// Flash correct button
for (var i = 0; i < answerButtons.length; i++) {
if (answerButtons[i].answer === selectedAnswer) {
answerButtons[i].flashCorrect();
break;
}
}
// Remove the number
var index = fallingNumbers.indexOf(currentEquation);
if (index !== -1) {
fallingNumbers.splice(index, 1);
currentEquation.destroy();
}
LK.effects.flashObject(currentEquation, 0x00FF00, 300);
} else {
// Wrong answer
LK.getSound('wrong').play();
health -= 10;
// Flash wrong button
for (var i = 0; i < answerButtons.length; i++) {
if (answerButtons[i].answer === selectedAnswer) {
answerButtons[i].flashWrong();
break;
}
}
LK.effects.flashScreen(0xFF0000, 500);
}
updateHealthBar();
currentEquation = null;
}
function updateHealthBar() {
var healthPercent = health / maxHealth;
healthBar.width = 300 * healthPercent;
if (health <= 0) {
LK.showGameOver();
}
}
function increaseDifficulty() {
if (score > 0 && score % 100 === 0) {
difficulty = Math.min(4, Math.floor(score / 100) + 1);
numberSpawnRate = Math.max(60, 180 - difficulty * 30);
}
}
game.update = function () {
nextNumberTimer++;
// Spawn new numbers
if (nextNumberTimer >= numberSpawnRate && !currentEquation) {
spawnFallingNumber();
nextNumberTimer = 0;
}
// Update falling numbers
for (var i = fallingNumbers.length - 1; i >= 0; i--) {
var number = fallingNumbers[i];
if (number.lastY === undefined) number.lastY = number.y;
// Check if number reached the base
if (number.lastY < base.y - 50 && number.y >= base.y - 50 && !number.answered) {
LK.getSound('hit').play();
health -= 15;
updateHealthBar();
fallingNumbers.splice(i, 1);
number.destroy();
if (number === currentEquation) {
currentEquation = null;
}
LK.effects.flashScreen(0xFF4500, 400);
}
// Remove numbers that are off screen
if (number.y > 2732 + 100) {
fallingNumbers.splice(i, 1);
number.destroy();
if (number === currentEquation) {
currentEquation = null;
}
}
number.lastY = number.y;
}
increaseDifficulty();
};