Code edit (1 edits merged)
Please save this source code
User prompt
Math Castle Quest
Initial prompt
create a mini game easy math question with 4 answers the one of them will be qorrect and make a man walking through a castle in 10 questions if the answer is correct make the person go step by step to castle. if the player completes the ten questions he will reach the castle and will go to next level to the second level.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var AnswerButton = Container.expand(function (answerText, isCorrect, onAnswerSelected) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('answerButton', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(answerText, {
size: 60,
fill: 0x000000
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.isCorrect = isCorrect;
self.onAnswerSelected = onAnswerSelected;
self.down = function (x, y, obj) {
self.onAnswerSelected(self.isCorrect);
};
self.setCorrectStyle = function () {
buttonGraphics.tint = 0x00ff00;
};
self.setIncorrectStyle = function () {
buttonGraphics.tint = 0xff0000;
};
self.resetStyle = function () {
buttonGraphics.tint = 0xffffff;
};
return self;
});
var Castle = Container.expand(function () {
var self = Container.call(this);
var castleGraphics = self.attachAsset('castle', {
anchorX: 0.5,
anchorY: 1.0
});
return self;
});
var Character = Container.expand(function () {
var self = Container.call(this);
var characterGraphics = self.attachAsset('character', {
anchorX: 0.5,
anchorY: 1.0
});
self.moveToPosition = function (targetX) {
tween(self, {
x: targetX
}, {
duration: 1000,
easing: tween.easeInOut
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87ceeb
});
/****
* Game Code
****/
// Game variables
var currentLevel = storage.currentLevel || 1;
var currentQuestion = 0;
var totalQuestions = 10;
var correctAnswers = 0;
var gameActive = false;
// Math problem generation
var mathProblems = [];
var currentProblem = null;
// Game objects
var character = null;
var castle = null;
var ground = null;
var path = null;
var questionPanel = null;
var questionText = null;
var progressText = null;
var answerButtons = [];
// Positions
var startX = 200;
var endX = 1800;
var characterY = 2400;
var pathY = 2450;
// Initialize game elements
function initializeGame() {
// Create ground
ground = game.addChild(LK.getAsset('ground', {
anchorX: 0,
anchorY: 0,
x: 0,
y: 2470
}));
// Create path
path = game.addChild(LK.getAsset('path', {
anchorX: 0,
anchorY: 0.5,
x: 124,
y: pathY
}));
// Create character
character = game.addChild(new Character());
character.x = startX;
character.y = characterY;
// Create castle
castle = game.addChild(new Castle());
castle.x = endX;
castle.y = characterY;
// Create question panel
questionPanel = game.addChild(LK.getAsset('questionPanel', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1000
}));
questionPanel.alpha = 0.9;
// Create question text
questionText = new Text2('', {
size: 80,
fill: 0x000000
});
questionText.anchor.set(0.5, 0.5);
questionText.x = 1024;
questionText.y = 800;
game.addChild(questionText);
// Create progress text
progressText = new Text2('', {
size: 50,
fill: 0x000000
});
progressText.anchor.set(0.5, 0.5);
progressText.x = 1024;
progressText.y = 700;
game.addChild(progressText);
// Create answer buttons
var buttonPositions = [{
x: 512,
y: 1100
}, {
x: 1536,
y: 1100
}, {
x: 512,
y: 1300
}, {
x: 1536,
y: 1300
}];
for (var i = 0; i < 4; i++) {
var button = new AnswerButton('', false, onAnswerSelected);
button.x = buttonPositions[i].x;
button.y = buttonPositions[i].y;
answerButtons.push(button);
game.addChild(button);
}
generateMathProblems();
startNextQuestion();
}
function generateMathProblems() {
mathProblems = [];
for (var i = 0; i < totalQuestions; i++) {
var problem = generateMathProblem(currentLevel);
mathProblems.push(problem);
}
}
function generateMathProblem(level) {
var num1, num2, operation, correctAnswer, question;
if (level === 1) {
// Addition and subtraction up to 20
num1 = Math.floor(Math.random() * 20) + 1;
num2 = Math.floor(Math.random() * 20) + 1;
operation = Math.random() < 0.5 ? '+' : '-';
if (operation === '+') {
correctAnswer = num1 + num2;
question = num1 + ' + ' + num2 + ' = ?';
} else {
if (num1 < num2) {
var temp = num1;
num1 = num2;
num2 = temp;
}
correctAnswer = num1 - num2;
question = num1 + ' - ' + num2 + ' = ?';
}
} else if (level === 2) {
// Addition, subtraction, multiplication up to 100
var ops = ['+', '-', '*'];
operation = ops[Math.floor(Math.random() * ops.length)];
if (operation === '*') {
num1 = Math.floor(Math.random() * 12) + 1;
num2 = Math.floor(Math.random() * 12) + 1;
correctAnswer = num1 * num2;
question = num1 + ' × ' + num2 + ' = ?';
} else {
num1 = Math.floor(Math.random() * 50) + 1;
num2 = Math.floor(Math.random() * 50) + 1;
if (operation === '+') {
correctAnswer = num1 + num2;
question = num1 + ' + ' + num2 + ' = ?';
} else {
if (num1 < num2) {
var temp = num1;
num1 = num2;
num2 = temp;
}
correctAnswer = num1 - num2;
question = num1 + ' - ' + num2 + ' = ?';
}
}
} else {
// All operations with larger numbers
var ops = ['+', '-', '*', '/'];
operation = ops[Math.floor(Math.random() * ops.length)];
if (operation === '/') {
correctAnswer = Math.floor(Math.random() * 20) + 1;
num2 = Math.floor(Math.random() * 12) + 1;
num1 = correctAnswer * num2;
question = num1 + ' ÷ ' + num2 + ' = ?';
} else if (operation === '*') {
num1 = Math.floor(Math.random() * 25) + 1;
num2 = Math.floor(Math.random() * 25) + 1;
correctAnswer = num1 * num2;
question = num1 + ' × ' + num2 + ' = ?';
} else {
num1 = Math.floor(Math.random() * 100) + 1;
num2 = Math.floor(Math.random() * 100) + 1;
if (operation === '+') {
correctAnswer = num1 + num2;
question = num1 + ' + ' + num2 + ' = ?';
} else {
if (num1 < num2) {
var temp = num1;
num1 = num2;
num2 = temp;
}
correctAnswer = num1 - num2;
question = num1 + ' - ' + num2 + ' = ?';
}
}
}
// Generate wrong answers
var wrongAnswers = [];
var attempts = 0;
while (wrongAnswers.length < 3 && attempts < 20) {
var wrongAnswer;
var variation = Math.floor(Math.random() * 10) + 1;
if (Math.random() < 0.5) {
wrongAnswer = correctAnswer + variation;
} else {
wrongAnswer = correctAnswer - variation;
}
if (wrongAnswer !== correctAnswer && wrongAnswer > 0 && wrongAnswers.indexOf(wrongAnswer) === -1) {
wrongAnswers.push(wrongAnswer);
}
attempts++;
}
// Ensure we have exactly 3 wrong answers
while (wrongAnswers.length < 3) {
var fallbackWrong = correctAnswer + Math.floor(Math.random() * 20) - 10;
if (fallbackWrong !== correctAnswer && fallbackWrong > 0 && wrongAnswers.indexOf(fallbackWrong) === -1) {
wrongAnswers.push(fallbackWrong);
}
}
return {
question: question,
correctAnswer: correctAnswer,
wrongAnswers: wrongAnswers
};
}
function startNextQuestion() {
if (currentQuestion >= totalQuestions) {
completeLevel();
return;
}
currentProblem = mathProblems[currentQuestion];
// Update question text
questionText.setText(currentProblem.question);
progressText.setText('Question ' + (currentQuestion + 1) + ' of ' + totalQuestions);
// Create answer options
var answers = [currentProblem.correctAnswer].concat(currentProblem.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;
}
// Set button texts and correct answer
for (var i = 0; i < 4; i++) {
answerButtons[i].children[1].setText(answers[i].toString());
answerButtons[i].isCorrect = answers[i] === currentProblem.correctAnswer;
answerButtons[i].resetStyle();
}
gameActive = true;
}
function onAnswerSelected(isCorrect) {
if (!gameActive) return;
gameActive = false;
// Show feedback on buttons
for (var i = 0; i < answerButtons.length; i++) {
if (answerButtons[i].isCorrect) {
answerButtons[i].setCorrectStyle();
} else {
answerButtons[i].setIncorrectStyle();
}
}
if (isCorrect) {
correctAnswers++;
LK.getSound('correct').play();
// Move character forward
var progress = correctAnswers / totalQuestions;
var targetX = startX + (endX - startX) * progress;
character.moveToPosition(targetX);
LK.setScore(LK.getScore() + 10 * currentLevel);
} else {
LK.getSound('incorrect').play();
}
// Wait before next question
LK.setTimeout(function () {
currentQuestion++;
startNextQuestion();
}, 2000);
}
function completeLevel() {
if (correctAnswers === totalQuestions) {
LK.getSound('victory').play();
// Flash screen with celebration color
LK.effects.flashScreen(0xffd700, 1500);
// Level up
currentLevel++;
storage.currentLevel = currentLevel;
LK.setTimeout(function () {
if (currentLevel <= 5) {
// Reset for next level
currentQuestion = 0;
correctAnswers = 0;
generateMathProblems();
character.x = startX;
startNextQuestion();
} else {
LK.showYouWin();
}
}, 2000);
} else {
// Not all questions answered correctly, restart level
LK.setTimeout(function () {
currentQuestion = 0;
correctAnswers = 0;
character.x = startX;
generateMathProblems();
startNextQuestion();
}, 2000);
}
}
// Initialize the game
initializeGame();
game.update = function () {
// Game update logic if needed
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,370 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var AnswerButton = Container.expand(function (answerText, isCorrect, onAnswerSelected) {
+ var self = Container.call(this);
+ var buttonGraphics = self.attachAsset('answerButton', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ var buttonText = new Text2(answerText, {
+ size: 60,
+ fill: 0x000000
+ });
+ buttonText.anchor.set(0.5, 0.5);
+ self.addChild(buttonText);
+ self.isCorrect = isCorrect;
+ self.onAnswerSelected = onAnswerSelected;
+ self.down = function (x, y, obj) {
+ self.onAnswerSelected(self.isCorrect);
+ };
+ self.setCorrectStyle = function () {
+ buttonGraphics.tint = 0x00ff00;
+ };
+ self.setIncorrectStyle = function () {
+ buttonGraphics.tint = 0xff0000;
+ };
+ self.resetStyle = function () {
+ buttonGraphics.tint = 0xffffff;
+ };
+ return self;
+});
+var Castle = Container.expand(function () {
+ var self = Container.call(this);
+ var castleGraphics = self.attachAsset('castle', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ return self;
+});
+var Character = Container.expand(function () {
+ var self = Container.call(this);
+ var characterGraphics = self.attachAsset('character', {
+ anchorX: 0.5,
+ anchorY: 1.0
+ });
+ self.moveToPosition = function (targetX) {
+ tween(self, {
+ x: targetX
+ }, {
+ duration: 1000,
+ easing: tween.easeInOut
+ });
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87ceeb
+});
+
+/****
+* Game Code
+****/
+// Game variables
+var currentLevel = storage.currentLevel || 1;
+var currentQuestion = 0;
+var totalQuestions = 10;
+var correctAnswers = 0;
+var gameActive = false;
+// Math problem generation
+var mathProblems = [];
+var currentProblem = null;
+// Game objects
+var character = null;
+var castle = null;
+var ground = null;
+var path = null;
+var questionPanel = null;
+var questionText = null;
+var progressText = null;
+var answerButtons = [];
+// Positions
+var startX = 200;
+var endX = 1800;
+var characterY = 2400;
+var pathY = 2450;
+// Initialize game elements
+function initializeGame() {
+ // Create ground
+ ground = game.addChild(LK.getAsset('ground', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 0,
+ y: 2470
+ }));
+ // Create path
+ path = game.addChild(LK.getAsset('path', {
+ anchorX: 0,
+ anchorY: 0.5,
+ x: 124,
+ y: pathY
+ }));
+ // Create character
+ character = game.addChild(new Character());
+ character.x = startX;
+ character.y = characterY;
+ // Create castle
+ castle = game.addChild(new Castle());
+ castle.x = endX;
+ castle.y = characterY;
+ // Create question panel
+ questionPanel = game.addChild(LK.getAsset('questionPanel', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 1024,
+ y: 1000
+ }));
+ questionPanel.alpha = 0.9;
+ // Create question text
+ questionText = new Text2('', {
+ size: 80,
+ fill: 0x000000
+ });
+ questionText.anchor.set(0.5, 0.5);
+ questionText.x = 1024;
+ questionText.y = 800;
+ game.addChild(questionText);
+ // Create progress text
+ progressText = new Text2('', {
+ size: 50,
+ fill: 0x000000
+ });
+ progressText.anchor.set(0.5, 0.5);
+ progressText.x = 1024;
+ progressText.y = 700;
+ game.addChild(progressText);
+ // Create answer buttons
+ var buttonPositions = [{
+ x: 512,
+ y: 1100
+ }, {
+ x: 1536,
+ y: 1100
+ }, {
+ x: 512,
+ y: 1300
+ }, {
+ x: 1536,
+ y: 1300
+ }];
+ for (var i = 0; i < 4; i++) {
+ var button = new AnswerButton('', false, onAnswerSelected);
+ button.x = buttonPositions[i].x;
+ button.y = buttonPositions[i].y;
+ answerButtons.push(button);
+ game.addChild(button);
+ }
+ generateMathProblems();
+ startNextQuestion();
+}
+function generateMathProblems() {
+ mathProblems = [];
+ for (var i = 0; i < totalQuestions; i++) {
+ var problem = generateMathProblem(currentLevel);
+ mathProblems.push(problem);
+ }
+}
+function generateMathProblem(level) {
+ var num1, num2, operation, correctAnswer, question;
+ if (level === 1) {
+ // Addition and subtraction up to 20
+ num1 = Math.floor(Math.random() * 20) + 1;
+ num2 = Math.floor(Math.random() * 20) + 1;
+ operation = Math.random() < 0.5 ? '+' : '-';
+ if (operation === '+') {
+ correctAnswer = num1 + num2;
+ question = num1 + ' + ' + num2 + ' = ?';
+ } else {
+ if (num1 < num2) {
+ var temp = num1;
+ num1 = num2;
+ num2 = temp;
+ }
+ correctAnswer = num1 - num2;
+ question = num1 + ' - ' + num2 + ' = ?';
+ }
+ } else if (level === 2) {
+ // Addition, subtraction, multiplication up to 100
+ var ops = ['+', '-', '*'];
+ operation = ops[Math.floor(Math.random() * ops.length)];
+ if (operation === '*') {
+ num1 = Math.floor(Math.random() * 12) + 1;
+ num2 = Math.floor(Math.random() * 12) + 1;
+ correctAnswer = num1 * num2;
+ question = num1 + ' × ' + num2 + ' = ?';
+ } else {
+ num1 = Math.floor(Math.random() * 50) + 1;
+ num2 = Math.floor(Math.random() * 50) + 1;
+ if (operation === '+') {
+ correctAnswer = num1 + num2;
+ question = num1 + ' + ' + num2 + ' = ?';
+ } else {
+ if (num1 < num2) {
+ var temp = num1;
+ num1 = num2;
+ num2 = temp;
+ }
+ correctAnswer = num1 - num2;
+ question = num1 + ' - ' + num2 + ' = ?';
+ }
+ }
+ } else {
+ // All operations with larger numbers
+ var ops = ['+', '-', '*', '/'];
+ operation = ops[Math.floor(Math.random() * ops.length)];
+ if (operation === '/') {
+ correctAnswer = Math.floor(Math.random() * 20) + 1;
+ num2 = Math.floor(Math.random() * 12) + 1;
+ num1 = correctAnswer * num2;
+ question = num1 + ' ÷ ' + num2 + ' = ?';
+ } else if (operation === '*') {
+ num1 = Math.floor(Math.random() * 25) + 1;
+ num2 = Math.floor(Math.random() * 25) + 1;
+ correctAnswer = num1 * num2;
+ question = num1 + ' × ' + num2 + ' = ?';
+ } else {
+ num1 = Math.floor(Math.random() * 100) + 1;
+ num2 = Math.floor(Math.random() * 100) + 1;
+ if (operation === '+') {
+ correctAnswer = num1 + num2;
+ question = num1 + ' + ' + num2 + ' = ?';
+ } else {
+ if (num1 < num2) {
+ var temp = num1;
+ num1 = num2;
+ num2 = temp;
+ }
+ correctAnswer = num1 - num2;
+ question = num1 + ' - ' + num2 + ' = ?';
+ }
+ }
+ }
+ // Generate wrong answers
+ var wrongAnswers = [];
+ var attempts = 0;
+ while (wrongAnswers.length < 3 && attempts < 20) {
+ var wrongAnswer;
+ var variation = Math.floor(Math.random() * 10) + 1;
+ if (Math.random() < 0.5) {
+ wrongAnswer = correctAnswer + variation;
+ } else {
+ wrongAnswer = correctAnswer - variation;
+ }
+ if (wrongAnswer !== correctAnswer && wrongAnswer > 0 && wrongAnswers.indexOf(wrongAnswer) === -1) {
+ wrongAnswers.push(wrongAnswer);
+ }
+ attempts++;
+ }
+ // Ensure we have exactly 3 wrong answers
+ while (wrongAnswers.length < 3) {
+ var fallbackWrong = correctAnswer + Math.floor(Math.random() * 20) - 10;
+ if (fallbackWrong !== correctAnswer && fallbackWrong > 0 && wrongAnswers.indexOf(fallbackWrong) === -1) {
+ wrongAnswers.push(fallbackWrong);
+ }
+ }
+ return {
+ question: question,
+ correctAnswer: correctAnswer,
+ wrongAnswers: wrongAnswers
+ };
+}
+function startNextQuestion() {
+ if (currentQuestion >= totalQuestions) {
+ completeLevel();
+ return;
+ }
+ currentProblem = mathProblems[currentQuestion];
+ // Update question text
+ questionText.setText(currentProblem.question);
+ progressText.setText('Question ' + (currentQuestion + 1) + ' of ' + totalQuestions);
+ // Create answer options
+ var answers = [currentProblem.correctAnswer].concat(currentProblem.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;
+ }
+ // Set button texts and correct answer
+ for (var i = 0; i < 4; i++) {
+ answerButtons[i].children[1].setText(answers[i].toString());
+ answerButtons[i].isCorrect = answers[i] === currentProblem.correctAnswer;
+ answerButtons[i].resetStyle();
+ }
+ gameActive = true;
+}
+function onAnswerSelected(isCorrect) {
+ if (!gameActive) return;
+ gameActive = false;
+ // Show feedback on buttons
+ for (var i = 0; i < answerButtons.length; i++) {
+ if (answerButtons[i].isCorrect) {
+ answerButtons[i].setCorrectStyle();
+ } else {
+ answerButtons[i].setIncorrectStyle();
+ }
+ }
+ if (isCorrect) {
+ correctAnswers++;
+ LK.getSound('correct').play();
+ // Move character forward
+ var progress = correctAnswers / totalQuestions;
+ var targetX = startX + (endX - startX) * progress;
+ character.moveToPosition(targetX);
+ LK.setScore(LK.getScore() + 10 * currentLevel);
+ } else {
+ LK.getSound('incorrect').play();
+ }
+ // Wait before next question
+ LK.setTimeout(function () {
+ currentQuestion++;
+ startNextQuestion();
+ }, 2000);
+}
+function completeLevel() {
+ if (correctAnswers === totalQuestions) {
+ LK.getSound('victory').play();
+ // Flash screen with celebration color
+ LK.effects.flashScreen(0xffd700, 1500);
+ // Level up
+ currentLevel++;
+ storage.currentLevel = currentLevel;
+ LK.setTimeout(function () {
+ if (currentLevel <= 5) {
+ // Reset for next level
+ currentQuestion = 0;
+ correctAnswers = 0;
+ generateMathProblems();
+ character.x = startX;
+ startNextQuestion();
+ } else {
+ LK.showYouWin();
+ }
+ }, 2000);
+ } else {
+ // Not all questions answered correctly, restart level
+ LK.setTimeout(function () {
+ currentQuestion = 0;
+ correctAnswers = 0;
+ character.x = startX;
+ generateMathProblems();
+ startNextQuestion();
+ }, 2000);
+ }
+}
+// Initialize the game
+initializeGame();
+game.update = function () {
+ // Game update logic if needed
+};
\ No newline at end of file
knight warrior. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
add a princess at the middle top of the castle
stone road. No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
Princess . No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
start button . No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
rectangle red color. In-Game asset. 2d. High contrast. No shadows. rectangle red
rectangle purple color . No background. Transparent background. Blank background. No shadows. 2d. In-Game asset. flat
rectangle orange color no background. In-Game asset. 2d. High contrast. No shadows. ractangle