/****
* Classes
****/
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for Math Puzzle
var MathPuzzle = Container.expand(function () {
var self = Container.call(this);
self.question = '';
self.correctAnswer = 0;
self.wrongAnswers = [];
self.answerOptions = [];
// Method to generate a new math puzzle
self.generatePuzzle = function () {
var num1 = Math.floor(Math.random() * (10 + Math.floor(score / 10))) + 1;
var num2 = Math.floor(Math.random() * (10 + Math.floor(score / 10))) + 1;
self.correctAnswer = num1 + num2;
self.question = num1 + " + " + num2 + " = ?";
self.wrongAnswers = [self.correctAnswer + Math.floor(Math.random() * 3) + 1, self.correctAnswer - Math.floor(Math.random() * 3) - 1];
self.answerOptions = [self.correctAnswer].concat(_toConsumableArray(self.wrongAnswers));
self.answerOptions.sort(function () {
return Math.random() - 0.5;
}); // Shuffle answers
};
// Method to display the puzzle
self.displayPuzzle = function () {
questionText.setText(self.question);
for (var i = 0; i < answerTexts.length; i++) {
answerTexts[i].setText(self.answerOptions[i]);
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize variables
function _toConsumableArray(r) {
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) {
return _arrayLikeToArray(r, a);
}
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
}
}
function _iterableToArray(r) {
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) {
return Array.from(r);
}
}
function _arrayWithoutHoles(r) {
if (Array.isArray(r)) {
return _arrayLikeToArray(r);
}
}
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) {
n[e] = r[e];
}
return n;
}
var score = 0;
var mathPuzzle = new MathPuzzle();
var questionText = new Text2('', {
size: 150,
fill: 0xFFFFFF
});
var scoreText = new Text2('Score: 0', {
size: 120,
fill: 0xFFFFFF
});
scoreText.anchor.set(1, 0); // Anchor to the top right
scoreText.x = 2048; // Position at the right edge of the screen
scoreText.y = 0; // Position at the top of the screen
game.addChild(scoreText);
var answerTexts = [new Text2('', {
size: 120,
fill: 0xFFFFFF
}), new Text2('', {
size: 120,
fill: 0xFFFFFF
}), new Text2('', {
size: 120,
fill: 0xFFFFFF
})];
// Position question and answers
questionText.anchor.set(0.5, 0);
questionText.x = 2048 / 2;
questionText.y = 200;
game.addChild(questionText);
for (var i = 0; i < answerTexts.length; i++) {
answerTexts[i].anchor.set(0.5, 0);
answerTexts[i].x = 2048 / 2; // Center the answer options
answerTexts[i].y = 500 + i * 200; // Distribute the answer options vertically
game.addChild(answerTexts[i]);
}
// Function to start a new puzzle
function startNewPuzzle() {
mathPuzzle.generatePuzzle();
mathPuzzle.displayPuzzle();
// Reset the time limit for the new puzzle
timeLimit = 300; // 5 seconds * 60 FPS
}
// Event handler for selecting an answer
function selectAnswer(index) {
if (mathPuzzle.answerOptions[index] === mathPuzzle.correctAnswer) {
score++;
scoreText.setText('Score: ' + score); // Update the score display
LK.effects.flashScreen(0x00ff00, 1000); // Flash screen green
startNewPuzzle();
} else {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
}
// Add event listeners for answer selection
for (var i = 0; i < answerTexts.length; i++) {
(function (i) {
answerTexts[i].down = function (x, y, obj) {
selectAnswer(i);
};
})(i);
}
// Start the first puzzle
startNewPuzzle();
// Initialize a variable to keep track of the time limit for each question
var timeLimit = Math.max(60, 300 - score * 10); // Decrease time limit by 0.1 second for each point scored, with a minimum of 1 second
// Create a new Text2 object to display the timer
var timerText = new Text2('Time: ' + (timeLimit / 60).toFixed(1), {
size: 80,
fill: 0xFFFFFF
});
timerText.anchor.set(0, 0); // Anchor to the top left
timerText.x = 0; // Position at the left edge of the screen
timerText.y = 0; // Position at the top of the screen
game.addChild(timerText);
// Update function to increase difficulty over time and check if time limit is exceeded
game.update = function () {
// Decrease the time limit every tick
timeLimit--;
// Update the timer display
timerText.setText('Time: ' + (timeLimit / 60).toFixed(1));
// If time limit is exceeded, end the game
if (timeLimit <= 0) {
LK.effects.flashScreen(0xff0000, 1000);
LK.showGameOver();
}
if (LK.ticks % 600 === 0) {// Every 10 seconds
// Increase difficulty by reducing time to answer or increasing number range
}
};