/**** * 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 = 300 + col * 420; button.y = 500 + row * 220; 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 };
/****
* 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 = 300 + col * 420;
button.y = 500 + row * 220;
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
};