User prompt
Remove music
User prompt
fix
User prompt
fix
User prompt
fix code
User prompt
fix code
User prompt
Put the 2nd stage
User prompt
Questions in stage 1 should be simpler
User prompt
After 10 questions in the game, the time to move on to the second stage should be shortened by 1 second and the questions should be a little more difficult.
User prompt
Put the timer in the corner of the screen to show how much time has passed
User prompt
add time counter
User prompt
subtraction multiplication and division add
User prompt
make the questions a little harder
User prompt
Let the background be a lively image
User prompt
The person who gets 10 correct answers wins the game.
User prompt
Let the background be more colorful
User prompt
Let the background be like school and write the questions on the board.
User prompt
The questions get harder every 10 questions, a sound will be heard when you get the answer right, and your score will decrease when you get the answer wrong.
User prompt
Please fix the bug: 'LK.now is not a function' in or related to this line: 'questionStartTime = LK.now();' Line Number: 293
Code edit (1 edits merged)
Please save this source code
User prompt
Math Dash: Quick Solve
Initial prompt
Make me a math game that looks good and gives you points when you get the answer right
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
// Answer Button Class
var AnswerButton = Container.expand(function () {
var self = Container.call(this);
// Attach default button asset
var btn = self.attachAsset('answerBtn', {
anchorX: 0.5,
anchorY: 0.5
});
// Answer text
self.txt = self.addChild(new Text2('', {
size: 120,
fill: 0xFFFFFF
}));
self.txt.anchor.set(0.5, 0.5);
// Value this button represents
self.value = null;
// State: 'default', 'correct', 'wrong'
self.state = 'default';
// Set answer value and text
self.setValue = function (val) {
self.value = val;
self.txt.setText(val + '');
};
// Set state for visual feedback
self.setState = function (state) {
self.state = state;
if (state === 'default') {
btn.assetId = 'answerBtn';
btn.setAsset('answerBtn');
} else if (state === 'correct') {
btn.assetId = 'answerBtnCorrect';
btn.setAsset('answerBtnCorrect');
} else if (state === 'wrong') {
btn.assetId = 'answerBtnWrong';
btn.setAsset('answerBtnWrong');
}
};
// Reset to default state
self.reset = function () {
self.setState('default');
};
// Touch event handler
self.down = function (x, y, obj) {
if (typeof self.onSelect === 'function') {
self.onSelect(self.value, self);
}
};
return self;
});
// Question Box Class
var QuestionBox = Container.expand(function () {
var self = Container.call(this);
var box = self.attachAsset('questionBox', {
anchorX: 0.5,
anchorY: 0.5
});
self.txt = self.addChild(new Text2('', {
size: 150,
fill: "#fff"
}));
self.txt.anchor.set(0.5, 0.5);
self.setQuestion = function (q) {
self.txt.setText(q);
};
return self;
});
// Timer Bar Class
var TimerBar = Container.expand(function () {
var self = Container.call(this);
// The bar background
var bar = self.attachAsset('timerBar', {
anchorX: 0.5,
anchorY: 0.5
});
// The fill bar (will be scaled)
self.fill = self.attachAsset('timerBar', {
anchorX: 0.5,
anchorY: 0.5
});
self.fill.tint = 0x50E3C2;
self.fill.y = 0;
self.fill.x = 0;
// Set progress (0 to 1)
self.setProgress = function (p) {
if (p < 0) p = 0;
if (p > 1) p = 1;
self.fill.scaleX = p;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x222244
});
/****
* Game Code
****/
// Colors are chosen for visual clarity and engagement.
// Math Dash: Quick Solve uses simple shapes for answer buttons and question area.
// Game variables
var score = 0;
var question = null;
var correctAnswer = null;
var answerChoices = [];
var answerButtons = [];
var questionBox = null;
var timerBar = null;
var timer = null;
var timePerQuestion = 3500; // ms, will decrease as difficulty increases
var timeLeft = 0;
var questionStartTime = 0;
var scoreTxt = null;
var gameActive = true;
var difficultyLevel = 1; // Increases as player scores
// Layout positions
var centerX = 2048 / 2;
var questionY = 500;
var answersStartY = 1100;
var answerSpacing = 300;
// Create and add question box
questionBox = new QuestionBox();
questionBox.x = centerX;
questionBox.y = questionY;
game.addChild(questionBox);
// Create and add timer bar
timerBar = new TimerBar();
timerBar.x = centerX;
timerBar.y = questionY + 180;
game.addChild(timerBar);
// Create answer buttons (4 choices)
for (var i = 0; i < 4; i++) {
var btn = new AnswerButton();
btn.x = centerX;
btn.y = answersStartY + i * answerSpacing;
btn.idx = i;
btn.onSelect = handleAnswer;
answerButtons.push(btn);
game.addChild(btn);
}
// Score text (top center, not in top left 100x100)
scoreTxt = new Text2('0', {
size: 120,
fill: "#fff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Generate a new math question
function generateQuestion() {
// Increase difficulty every 5 points
difficultyLevel = 1 + Math.floor(score / 5);
// Choose operation and range based on difficulty
var opRand = Math.random();
var op = '+';
var min = 1,
max = 10 + difficultyLevel * 5;
if (difficultyLevel >= 3 && opRand > 0.5) op = '-';
if (difficultyLevel >= 5 && opRand > 0.7) op = '×';
if (difficultyLevel >= 8 && opRand > 0.85) op = '÷';
var a, b, q, ans;
if (op === '+') {
a = randInt(min, max);
b = randInt(min, max);
ans = a + b;
q = a + " + " + b + " = ?";
} else if (op === '-') {
a = randInt(min, max);
b = randInt(min, a); // ensure non-negative
ans = a - b;
q = a + " - " + b + " = ?";
} else if (op === '×') {
a = randInt(min, max - 5);
b = randInt(2, 12);
ans = a * b;
q = a + " × " + b + " = ?";
} else if (op === '÷') {
b = randInt(2, 12);
ans = randInt(min, max - 5);
a = ans * b;
q = a + " ÷ " + b + " = ?";
}
correctAnswer = ans;
question = q;
// Generate 3 wrong answers
var wrongs = [];
while (wrongs.length < 3) {
var delta = randInt(1, 8 + difficultyLevel * 2);
var wrong = ans + (Math.random() > 0.5 ? delta : -delta);
if (wrong !== ans && wrong >= 0 && wrongs.indexOf(wrong) === -1) {
wrongs.push(wrong);
}
}
// Shuffle answers
answerChoices = wrongs.concat([ans]);
shuffleArray(answerChoices);
// Set question and answers
questionBox.setQuestion(question);
for (var i = 0; i < 4; i++) {
answerButtons[i].setValue(answerChoices[i]);
answerButtons[i].reset();
}
}
// Handle answer selection
function handleAnswer(val, btn) {
if (!gameActive) return;
if (val === correctAnswer) {
btn.setState('correct');
LK.getSound('correct').play();
score += 1;
LK.setScore(score);
scoreTxt.setText(score + '');
// Animate button
tween(btn, {
scaleX: 1.15,
scaleY: 1.15
}, {
duration: 120,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(btn, {
scaleX: 1,
scaleY: 1
}, {
duration: 120
});
}
});
// Next question after short delay
LK.setTimeout(nextQuestion, 350);
} else {
btn.setState('wrong');
LK.getSound('wrong').play();
// Flash wrong, then game over
tween(btn, {
scaleX: 0.9,
scaleY: 0.9
}, {
duration: 120,
onFinish: function onFinish() {
tween(btn, {
scaleX: 1,
scaleY: 1
}, {
duration: 120
});
}
});
endGame();
}
}
// Start next question
function nextQuestion() {
if (!gameActive) return;
// Increase speed as score increases
timePerQuestion = Math.max(1200, 3500 - score * 80);
timeLeft = timePerQuestion;
questionStartTime = Date.now();
generateQuestion();
}
// End game
function endGame() {
if (!gameActive) return;
gameActive = false;
// Flash screen red
LK.effects.flashScreen(0xD0021B, 800);
// Show game over (handled by LK)
LK.setTimeout(function () {
LK.showGameOver();
}, 600);
}
// Utility: random integer [min, max]
function randInt(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
// Utility: shuffle array in-place
function shuffleArray(arr) {
for (var i = arr.length - 1; i > 0; i--) {
var j = randInt(0, i);
var t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
// Game update loop
game.update = function () {
if (!gameActive) return;
// Update timer
var now = Date.now();
timeLeft = timePerQuestion - (now - questionStartTime);
var progress = timeLeft / timePerQuestion;
timerBar.setProgress(progress);
// If time runs out
if (timeLeft <= 0) {
// Flash all buttons red
for (var i = 0; i < 4; i++) {
answerButtons[i].setState('wrong');
}
endGame();
}
};
// Start first question
function startGame() {
score = 0;
LK.setScore(0);
scoreTxt.setText('0');
gameActive = true;
difficultyLevel = 1;
nextQuestion();
}
// On game reset, start again
startGame(); ===================================================================
--- original.js
+++ change.js
@@ -264,9 +264,9 @@
if (!gameActive) return;
// Increase speed as score increases
timePerQuestion = Math.max(1200, 3500 - score * 80);
timeLeft = timePerQuestion;
- questionStartTime = LK.now();
+ questionStartTime = Date.now();
generateQuestion();
}
// End game
function endGame() {
@@ -295,9 +295,9 @@
// Game update loop
game.update = function () {
if (!gameActive) return;
// Update timer
- var now = LK.now();
+ var now = Date.now();
timeLeft = timePerQuestion - (now - questionStartTime);
var progress = timeLeft / timePerQuestion;
timerBar.setProgress(progress);
// If time runs out