/**** * Plugins ****/ var tween = LK.import("@upit/tween.v1"); /**** * Classes ****/ var CalculatorButton = Container.expand(function () { var self = Container.call(this); self.buttonType = 'number'; self.value = ''; self.background = self.attachAsset('calculatorButton', { anchorX: 0.5, anchorY: 0.5 }); self.buttonText = new Text2('0', { size: 60, fill: 0xFFFFFF }); self.buttonText.anchor.set(0.5, 0.5); self.addChild(self.buttonText); self.setValue = function (value, type) { self.value = value; self.buttonType = type || 'number'; self.buttonText.setText(value.toString()); // Change background based on button type self.removeChild(self.background); if (type === 'operator') { self.background = self.attachAsset('operatorButton', { anchorX: 0.5, anchorY: 0.5 }); } else if (type === 'equals') { self.background = self.attachAsset('equalsButton', { anchorX: 0.5, anchorY: 0.5 }); } else if (type === 'clear') { self.background = self.attachAsset('clearButton', { anchorX: 0.5, anchorY: 0.5 }); } else { self.background = self.attachAsset('calculatorButton', { anchorX: 0.5, anchorY: 0.5 }); } self.addChildAt(self.background, 0); }; self.down = function (x, y, obj) { LK.getSound('buttonPress').play(); handleButtonPress(self.value, self.buttonType); }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0xf0f0f0 }); /**** * Game Code ****/ // Calculator state variables var displayValue = '0'; var firstOperand = null; var operator = null; var waitingForOperand = false; // Create calculator display var calculatorDisplay = game.attachAsset('display', { anchorX: 0.5, anchorY: 0.5, x: 1024, y: 300 }); // Create display text var displayText = new Text2('0', { size: 80, fill: 0x00ff00 }); displayText.anchor.set(1, 0.5); displayText.x = 850; displayText.y = 0; calculatorDisplay.addChild(displayText); // Create calculator buttons var calculatorButtons = []; var buttonLayout = [['C', '+', '', ''], ['7', '8', '9', ''], ['4', '5', '6', ''], ['1', '2', '3', ''], ['0', '', '', '=']]; for (var row = 0; row < buttonLayout.length; row++) { for (var col = 0; col < 4; col++) { var buttonValue = buttonLayout[row][col]; if (buttonValue !== '') { var button = new CalculatorButton(); var buttonType = 'number'; if (buttonValue === 'C') { buttonType = 'clear'; } else if (buttonValue === '+') { buttonType = 'operator'; } else if (buttonValue === '=') { buttonType = 'equals'; } button.setValue(buttonValue, buttonType); button.x = 400 + col * 320; button.y = 500 + row * 170; calculatorButtons.push(button); game.addChild(button); } } } function updateDisplay() { displayText.setText(displayValue); } function inputNumber(number) { if (waitingForOperand) { displayValue = number; waitingForOperand = false; } else { displayValue = displayValue === '0' ? number : displayValue + number; } updateDisplay(); } function inputOperator(nextOperator) { var inputValue = parseFloat(displayValue); if (firstOperand === null) { firstOperand = inputValue; } else if (operator) { var currentValue = firstOperand || 0; var newValue = calculate(currentValue, inputValue, operator); displayValue = newValue.toString(); firstOperand = newValue; updateDisplay(); } waitingForOperand = true; operator = nextOperator; } function calculate(firstOperand, secondOperand, operator) { if (operator === '+') { return firstOperand + secondOperand; } return secondOperand; } function performCalculation() { var inputValue = parseFloat(displayValue); if (firstOperand !== null && operator) { var newValue = calculate(firstOperand, inputValue, operator); displayValue = newValue.toString(); firstOperand = null; operator = null; waitingForOperand = true; updateDisplay(); } } function clearCalculator() { displayValue = '0'; firstOperand = null; operator = null; waitingForOperand = false; updateDisplay(); } function handleButtonPress(value, type) { if (type === 'number') { inputNumber(value); } else if (type === 'operator') { inputOperator(value); } else if (type === 'equals') { performCalculation(); } else if (type === 'clear') { clearCalculator(); } } // Initialize display updateDisplay(); game.update = function () { // Calculator logic is handled by button events };
===================================================================
--- original.js
+++ change.js
@@ -5,228 +5,175 @@
/****
* Classes
****/
-var AnswerButton = Container.expand(function () {
+var CalculatorButton = Container.expand(function () {
var self = Container.call(this);
- var background = self.attachAsset('answerButton', {
+ self.buttonType = 'number';
+ self.value = '';
+ self.background = self.attachAsset('calculatorButton', {
anchorX: 0.5,
anchorY: 0.5
});
- var answerText = new Text2('0', {
- size: 80,
+ self.buttonText = new Text2('0', {
+ size: 60,
fill: 0xFFFFFF
});
- answerText.anchor.set(0.5, 0.5);
- self.addChild(answerText);
- self.value = 0;
- self.isCorrect = false;
- self.background = background;
- self.answerText = answerText;
- self.setValue = function (value, correct) {
+ self.buttonText.anchor.set(0.5, 0.5);
+ self.addChild(self.buttonText);
+ self.setValue = function (value, type) {
self.value = value;
- self.isCorrect = correct;
- self.answerText.setText(value.toString());
- };
- self.setCorrectStyle = function () {
+ self.buttonType = type || 'number';
+ self.buttonText.setText(value.toString());
+ // Change background based on button type
self.removeChild(self.background);
- self.background = self.attachAsset('answerButtonCorrect', {
- anchorX: 0.5,
- anchorY: 0.5
- });
+ if (type === 'operator') {
+ self.background = self.attachAsset('operatorButton', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ } else if (type === 'equals') {
+ self.background = self.attachAsset('equalsButton', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ } else if (type === 'clear') {
+ self.background = self.attachAsset('clearButton', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ } else {
+ self.background = self.attachAsset('calculatorButton', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ }
self.addChildAt(self.background, 0);
};
- self.setWrongStyle = function () {
- self.removeChild(self.background);
- self.background = self.attachAsset('answerButtonWrong', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.addChildAt(self.background, 0);
- };
- self.resetStyle = function () {
- self.removeChild(self.background);
- self.background = self.attachAsset('answerButton', {
- anchorX: 0.5,
- anchorY: 0.5
- });
- self.addChildAt(self.background, 0);
- };
self.down = function (x, y, obj) {
- if (self.isCorrect) {
- self.setCorrectStyle();
- LK.getSound('correct').play();
- LK.setScore(LK.getScore() + 10 * streakMultiplier);
- currentStreak++;
- updateStreakMultiplier();
- correctAnswers++;
- generateNewProblem();
- } else {
- self.setWrongStyle();
- LK.getSound('wrong').play();
- wrongAnswers++;
- currentStreak = 0;
- updateStreakMultiplier();
- if (wrongAnswers >= maxWrongAnswers) {
- LK.showGameOver();
- } else {
- LK.setTimeout(function () {
- generateNewProblem();
- }, 1000);
- }
- }
+ LK.getSound('buttonPress').play();
+ handleButtonPress(self.value, self.buttonType);
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x4a90e2
+ backgroundColor: 0xf0f0f0
});
/****
* Game Code
****/
-var currentNum1 = 0;
-var currentNum2 = 0;
-var correctAnswer = 0;
-var answerButtons = [];
-var currentStreak = 0;
-var streakMultiplier = 1;
-var wrongAnswers = 0;
-var correctAnswers = 0;
-var maxWrongAnswers = 3;
-var difficultyLevel = 1;
-// Create background
-var background = game.attachAsset('background', {
- x: 0,
- y: 0
-});
-// Create problem display area
-var problemArea = game.attachAsset('problemArea', {
+// Calculator state variables
+var displayValue = '0';
+var firstOperand = null;
+var operator = null;
+var waitingForOperand = false;
+// Create calculator display
+var calculatorDisplay = game.attachAsset('display', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
- y: 400
+ y: 300
});
-// Create problem text
-var problemText = new Text2('5 + 3 = ?', {
- size: 120,
- fill: 0x333333
+// Create display text
+var displayText = new Text2('0', {
+ size: 80,
+ fill: 0x00ff00
});
-problemText.anchor.set(0.5, 0.5);
-problemArea.addChild(problemText);
-// Create answer buttons
-for (var i = 0; i < 4; i++) {
- var button = new AnswerButton();
- button.x = 300 + i % 2 * 450 + i % 2 * 150;
- button.y = 800 + Math.floor(i / 2) * 200;
- answerButtons.push(button);
- game.addChild(button);
+displayText.anchor.set(1, 0.5);
+displayText.x = 850;
+displayText.y = 0;
+calculatorDisplay.addChild(displayText);
+// Create calculator buttons
+var calculatorButtons = [];
+var buttonLayout = [['C', '+', '', ''], ['7', '8', '9', ''], ['4', '5', '6', ''], ['1', '2', '3', ''], ['0', '', '', '=']];
+for (var row = 0; row < buttonLayout.length; row++) {
+ for (var col = 0; col < 4; col++) {
+ var buttonValue = buttonLayout[row][col];
+ if (buttonValue !== '') {
+ var button = new CalculatorButton();
+ var buttonType = 'number';
+ if (buttonValue === 'C') {
+ buttonType = 'clear';
+ } else if (buttonValue === '+') {
+ buttonType = 'operator';
+ } else if (buttonValue === '=') {
+ buttonType = 'equals';
+ }
+ button.setValue(buttonValue, buttonType);
+ button.x = 400 + col * 320;
+ button.y = 500 + row * 170;
+ calculatorButtons.push(button);
+ game.addChild(button);
+ }
+ }
}
-// Create score display
-var scoreText = new Text2('Score: 0', {
- size: 60,
- fill: 0xFFFFFF
-});
-scoreText.anchor.set(0, 0);
-scoreText.x = 150;
-scoreText.y = 150;
-LK.gui.topLeft.addChild(scoreText);
-// Create streak display
-var streakText = new Text2('Streak: 0x', {
- size: 50,
- fill: 0xFFFFFF
-});
-streakText.anchor.set(1, 0);
-streakText.x = -50;
-streakText.y = 150;
-LK.gui.topRight.addChild(streakText);
-// Create wrong answers display
-var wrongText = new Text2('Wrong: 0/3', {
- size: 40,
- fill: 0xFFFFFF
-});
-wrongText.anchor.set(0.5, 0);
-wrongText.x = 0;
-wrongText.y = 220;
-LK.gui.top.addChild(wrongText);
-function generateNumbers() {
- if (difficultyLevel === 1) {
- // Single digit numbers
- currentNum1 = Math.floor(Math.random() * 9) + 1;
- currentNum2 = Math.floor(Math.random() * 9) + 1;
- } else if (difficultyLevel === 2) {
- // Double digit numbers
- currentNum1 = Math.floor(Math.random() * 90) + 10;
- currentNum2 = Math.floor(Math.random() * 90) + 10;
+function updateDisplay() {
+ displayText.setText(displayValue);
+}
+function inputNumber(number) {
+ if (waitingForOperand) {
+ displayValue = number;
+ waitingForOperand = false;
} else {
- // Triple digit numbers
- currentNum1 = Math.floor(Math.random() * 900) + 100;
- currentNum2 = Math.floor(Math.random() * 900) + 100;
+ displayValue = displayValue === '0' ? number : displayValue + number;
}
- correctAnswer = currentNum1 + currentNum2;
+ updateDisplay();
}
-function generateWrongAnswers() {
- var wrongAnswers = [];
- var attempts = 0;
- while (wrongAnswers.length < 3 && attempts < 50) {
- var wrongAnswer;
- var variation = Math.floor(Math.random() * 20) + 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++;
+function inputOperator(nextOperator) {
+ var inputValue = parseFloat(displayValue);
+ if (firstOperand === null) {
+ firstOperand = inputValue;
+ } else if (operator) {
+ var currentValue = firstOperand || 0;
+ var newValue = calculate(currentValue, inputValue, operator);
+ displayValue = newValue.toString();
+ firstOperand = newValue;
+ updateDisplay();
}
- return wrongAnswers;
+ waitingForOperand = true;
+ operator = nextOperator;
}
-function generateNewProblem() {
- // Update difficulty based on correct answers
- if (correctAnswers >= 10 && difficultyLevel < 2) {
- difficultyLevel = 2;
- } else if (correctAnswers >= 25 && difficultyLevel < 3) {
- difficultyLevel = 3;
+function calculate(firstOperand, secondOperand, operator) {
+ if (operator === '+') {
+ return firstOperand + secondOperand;
}
- generateNumbers();
- problemText.setText(currentNum1 + ' + ' + currentNum2 + ' = ?');
- var wrongAnswersList = generateWrongAnswers();
- var allAnswers = [correctAnswer].concat(wrongAnswersList);
- // Shuffle answers
- for (var i = allAnswers.length - 1; i > 0; i--) {
- var j = Math.floor(Math.random() * (i + 1));
- var temp = allAnswers[i];
- allAnswers[i] = allAnswers[j];
- allAnswers[j] = temp;
+ return secondOperand;
+}
+function performCalculation() {
+ var inputValue = parseFloat(displayValue);
+ if (firstOperand !== null && operator) {
+ var newValue = calculate(firstOperand, inputValue, operator);
+ displayValue = newValue.toString();
+ firstOperand = null;
+ operator = null;
+ waitingForOperand = true;
+ updateDisplay();
}
- // Set button values
- for (var i = 0; i < 4; i++) {
- answerButtons[i].setValue(allAnswers[i], allAnswers[i] === correctAnswer);
- answerButtons[i].resetStyle();
- }
+}
+function clearCalculator() {
+ displayValue = '0';
+ firstOperand = null;
+ operator = null;
+ waitingForOperand = false;
updateDisplay();
}
-function updateStreakMultiplier() {
- if (currentStreak >= 10) {
- streakMultiplier = 3;
- } else if (currentStreak >= 5) {
- streakMultiplier = 2;
- } else {
- streakMultiplier = 1;
+function handleButtonPress(value, type) {
+ if (type === 'number') {
+ inputNumber(value);
+ } else if (type === 'operator') {
+ inputOperator(value);
+ } else if (type === 'equals') {
+ performCalculation();
+ } else if (type === 'clear') {
+ clearCalculator();
}
}
-function updateDisplay() {
- scoreText.setText('Score: ' + LK.getScore());
- streakText.setText('Streak: ' + currentStreak + 'x' + streakMultiplier);
- wrongText.setText('Wrong: ' + wrongAnswers + '/' + maxWrongAnswers);
-}
-// Initialize first problem
-generateNewProblem();
+// Initialize display
+updateDisplay();
game.update = function () {
- // Game logic is handled by button events
+ // Calculator logic is handled by button events
};
\ No newline at end of file