/**** * Classes ****/ var CalculatorButton = Container.expand(function (label, type, callback) { var self = Container.call(this); var buttonType = type || 'number'; var buttonShape; if (buttonType === 'operator') { buttonShape = self.attachAsset('operatorButton', { anchorX: 0.5, anchorY: 0.5 }); } else if (buttonType === 'equals') { buttonShape = self.attachAsset('equalsButton', { anchorX: 0.5, anchorY: 0.5 }); } else if (buttonType === 'clear') { buttonShape = self.attachAsset('clearButton', { anchorX: 0.5, anchorY: 0.5 }); } else { buttonShape = self.attachAsset('calculatorButton', { anchorX: 0.5, anchorY: 0.5 }); } var buttonText = new Text2(label, { size: 80, fill: buttonType === 'number' ? "#000000" : "#ffffff" }); buttonText.anchor.set(0.5, 0.5); self.addChild(buttonText); self.callback = callback; self.down = function (x, y, obj) { buttonShape.alpha = 0.7; if (self.callback) { self.callback(); } }; self.up = function (x, y, obj) { buttonShape.alpha = 1.0; }; return self; }); var CurrencyDisplay = Container.expand(function (currencyCode, rate, countryName) { var self = Container.call(this); self.currencyCode = currencyCode; self.rate = rate; self.countryName = countryName; self.currencyLabel = new Text2(currencyCode + ' - ' + countryName + ':', { size: 32, fill: 0xFFFFFF }); self.currencyLabel.anchor.set(0, 0.5); self.addChild(self.currencyLabel); self.valueText = new Text2('0.00', { size: 32, fill: 0xFFFFFF }); self.valueText.anchor.set(1, 0.5); self.valueText.x = 500; self.addChild(self.valueText); self.updateValue = function (usdAmount) { var convertedAmount = usdAmount * self.rate; if (self.currencyCode === 'JPY') { self.valueText.setText(Math.round(convertedAmount).toString()); } else { self.valueText.setText(convertedAmount.toFixed(2)); } }; return self; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x2c3e50 }); /**** * Game Code ****/ // Calculator state var currentDisplay = '0'; var previousValue = 0; var currentOperation = null; var waitingForNewInput = false; // Currency exchange rates (mock data - in real app would fetch from API) var exchangeRates = { 'EUR': 0.85, 'GBP': 0.73, 'JPY': 110.25, 'CAD': 1.25, 'AUD': 1.35, 'CHF': 0.92, 'SGD': 1.35, 'KPW': 900.0, 'KRW': 1320.0, 'ILS': 3.7 }; // Country names for currencies var countryNames = { 'EUR': 'European Union', 'GBP': 'United Kingdom', 'JPY': 'Japan', 'CAD': 'Canada', 'AUD': 'Australia', 'CHF': 'Switzerland', 'SGD': 'Singapore', 'KPW': 'North Korea', 'KRW': 'South Korea', 'ILS': 'Palestine' }; // Display background var displayBg = game.addChild(LK.getAsset('displayBackground', { anchorX: 0.5, anchorY: 0 })); displayBg.x = 1024; displayBg.y = 200; // Main display text var displayText = new Text2(currentDisplay, { size: 80, fill: 0xFFFFFF }); displayText.anchor.set(1, 0.5); displayText.x = 1900; displayText.y = 300; game.addChild(displayText); // USD label var usdLabel = new Text2('USD', { size: 50, fill: 0xFFFFFF }); usdLabel.anchor.set(0, 0.5); usdLabel.x = 200; usdLabel.y = 300; game.addChild(usdLabel); // Currency conversion background var currencyBg = game.addChild(LK.getAsset('currencyBackground', { anchorX: 0.5, anchorY: 1 })); currencyBg.x = 1024; currencyBg.y = 2732; // Currency displays var currencyDisplays = []; var currencies = Object.keys(exchangeRates); for (var i = 0; i < currencies.length; i++) { var currency = currencies[i]; var display = new CurrencyDisplay(currency, exchangeRates[currency], countryNames[currency]); display.x = 100 + i % 2 * 950; display.y = 2732 - 380 + Math.floor(i / 2) * 90; currencyDisplays.push(display); game.addChild(display); } // Calculator functions function updateDisplay() { displayText.setText(currentDisplay); var numericValue = parseFloat(currentDisplay) || 0; for (var i = 0; i < currencyDisplays.length; i++) { currencyDisplays[i].updateValue(numericValue); } } function inputNumber(num) { if (waitingForNewInput) { currentDisplay = num; waitingForNewInput = false; } else { if (currentDisplay === '0') { currentDisplay = num; } else { currentDisplay += num; } } updateDisplay(); } function inputDecimal() { if (waitingForNewInput) { currentDisplay = '0.'; waitingForNewInput = false; } else if (currentDisplay.indexOf('.') === -1) { currentDisplay += '.'; } updateDisplay(); } function inputOperation(op) { if (currentOperation !== null && !waitingForNewInput) { calculate(); } previousValue = parseFloat(currentDisplay); currentOperation = op; waitingForNewInput = true; } function calculate() { var current = parseFloat(currentDisplay); var result = previousValue; switch (currentOperation) { case '+': result = previousValue + current; break; case '-': result = previousValue - current; break; case '×': result = previousValue * current; break; } currentDisplay = result.toString(); if (currentDisplay.length > 12) { currentDisplay = result.toExponential(6); } currentOperation = null; waitingForNewInput = true; updateDisplay(); } function clearAll() { currentDisplay = '0'; previousValue = 0; currentOperation = null; waitingForNewInput = false; updateDisplay(); } // Create calculator buttons var buttons = []; // Number buttons layout - bigger buttons in 4x4 grid var numberLayout = [['1', '2', '3', 'C'], ['4', '5', '6', '¢'], ['7', '8', '9', '.'], ['0', '', '', '']]; for (var row = 0; row < numberLayout.length; row++) { for (var col = 0; col < numberLayout[row].length; col++) { var buttonLabel = numberLayout[row][col]; if (buttonLabel === '') continue; // Skip empty positions var buttonType = 'number'; var callback; if (buttonLabel === '.') { callback = inputDecimal; } else if (buttonLabel === 'C') { buttonType = 'clear'; callback = clearAll; } else if (buttonLabel === '¢') { callback = function callback() { if (currentDisplay !== '0') { var value = parseFloat(currentDisplay) / 100; currentDisplay = value.toString(); updateDisplay(); } }; } else { callback = function (num) { return function () { inputNumber(num); }; }(buttonLabel); } var button = new CalculatorButton(buttonLabel, buttonType, callback); // Make buttons bigger and center them button.x = 400 + col * 300; button.y = 800 + row * 200; buttons.push(button); game.addChild(button); } } // Initialize display updateDisplay();
/****
* Classes
****/
var CalculatorButton = Container.expand(function (label, type, callback) {
var self = Container.call(this);
var buttonType = type || 'number';
var buttonShape;
if (buttonType === 'operator') {
buttonShape = self.attachAsset('operatorButton', {
anchorX: 0.5,
anchorY: 0.5
});
} else if (buttonType === 'equals') {
buttonShape = self.attachAsset('equalsButton', {
anchorX: 0.5,
anchorY: 0.5
});
} else if (buttonType === 'clear') {
buttonShape = self.attachAsset('clearButton', {
anchorX: 0.5,
anchorY: 0.5
});
} else {
buttonShape = self.attachAsset('calculatorButton', {
anchorX: 0.5,
anchorY: 0.5
});
}
var buttonText = new Text2(label, {
size: 80,
fill: buttonType === 'number' ? "#000000" : "#ffffff"
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.callback = callback;
self.down = function (x, y, obj) {
buttonShape.alpha = 0.7;
if (self.callback) {
self.callback();
}
};
self.up = function (x, y, obj) {
buttonShape.alpha = 1.0;
};
return self;
});
var CurrencyDisplay = Container.expand(function (currencyCode, rate, countryName) {
var self = Container.call(this);
self.currencyCode = currencyCode;
self.rate = rate;
self.countryName = countryName;
self.currencyLabel = new Text2(currencyCode + ' - ' + countryName + ':', {
size: 32,
fill: 0xFFFFFF
});
self.currencyLabel.anchor.set(0, 0.5);
self.addChild(self.currencyLabel);
self.valueText = new Text2('0.00', {
size: 32,
fill: 0xFFFFFF
});
self.valueText.anchor.set(1, 0.5);
self.valueText.x = 500;
self.addChild(self.valueText);
self.updateValue = function (usdAmount) {
var convertedAmount = usdAmount * self.rate;
if (self.currencyCode === 'JPY') {
self.valueText.setText(Math.round(convertedAmount).toString());
} else {
self.valueText.setText(convertedAmount.toFixed(2));
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Calculator state
var currentDisplay = '0';
var previousValue = 0;
var currentOperation = null;
var waitingForNewInput = false;
// Currency exchange rates (mock data - in real app would fetch from API)
var exchangeRates = {
'EUR': 0.85,
'GBP': 0.73,
'JPY': 110.25,
'CAD': 1.25,
'AUD': 1.35,
'CHF': 0.92,
'SGD': 1.35,
'KPW': 900.0,
'KRW': 1320.0,
'ILS': 3.7
};
// Country names for currencies
var countryNames = {
'EUR': 'European Union',
'GBP': 'United Kingdom',
'JPY': 'Japan',
'CAD': 'Canada',
'AUD': 'Australia',
'CHF': 'Switzerland',
'SGD': 'Singapore',
'KPW': 'North Korea',
'KRW': 'South Korea',
'ILS': 'Palestine'
};
// Display background
var displayBg = game.addChild(LK.getAsset('displayBackground', {
anchorX: 0.5,
anchorY: 0
}));
displayBg.x = 1024;
displayBg.y = 200;
// Main display text
var displayText = new Text2(currentDisplay, {
size: 80,
fill: 0xFFFFFF
});
displayText.anchor.set(1, 0.5);
displayText.x = 1900;
displayText.y = 300;
game.addChild(displayText);
// USD label
var usdLabel = new Text2('USD', {
size: 50,
fill: 0xFFFFFF
});
usdLabel.anchor.set(0, 0.5);
usdLabel.x = 200;
usdLabel.y = 300;
game.addChild(usdLabel);
// Currency conversion background
var currencyBg = game.addChild(LK.getAsset('currencyBackground', {
anchorX: 0.5,
anchorY: 1
}));
currencyBg.x = 1024;
currencyBg.y = 2732;
// Currency displays
var currencyDisplays = [];
var currencies = Object.keys(exchangeRates);
for (var i = 0; i < currencies.length; i++) {
var currency = currencies[i];
var display = new CurrencyDisplay(currency, exchangeRates[currency], countryNames[currency]);
display.x = 100 + i % 2 * 950;
display.y = 2732 - 380 + Math.floor(i / 2) * 90;
currencyDisplays.push(display);
game.addChild(display);
}
// Calculator functions
function updateDisplay() {
displayText.setText(currentDisplay);
var numericValue = parseFloat(currentDisplay) || 0;
for (var i = 0; i < currencyDisplays.length; i++) {
currencyDisplays[i].updateValue(numericValue);
}
}
function inputNumber(num) {
if (waitingForNewInput) {
currentDisplay = num;
waitingForNewInput = false;
} else {
if (currentDisplay === '0') {
currentDisplay = num;
} else {
currentDisplay += num;
}
}
updateDisplay();
}
function inputDecimal() {
if (waitingForNewInput) {
currentDisplay = '0.';
waitingForNewInput = false;
} else if (currentDisplay.indexOf('.') === -1) {
currentDisplay += '.';
}
updateDisplay();
}
function inputOperation(op) {
if (currentOperation !== null && !waitingForNewInput) {
calculate();
}
previousValue = parseFloat(currentDisplay);
currentOperation = op;
waitingForNewInput = true;
}
function calculate() {
var current = parseFloat(currentDisplay);
var result = previousValue;
switch (currentOperation) {
case '+':
result = previousValue + current;
break;
case '-':
result = previousValue - current;
break;
case '×':
result = previousValue * current;
break;
}
currentDisplay = result.toString();
if (currentDisplay.length > 12) {
currentDisplay = result.toExponential(6);
}
currentOperation = null;
waitingForNewInput = true;
updateDisplay();
}
function clearAll() {
currentDisplay = '0';
previousValue = 0;
currentOperation = null;
waitingForNewInput = false;
updateDisplay();
}
// Create calculator buttons
var buttons = [];
// Number buttons layout - bigger buttons in 4x4 grid
var numberLayout = [['1', '2', '3', 'C'], ['4', '5', '6', '¢'], ['7', '8', '9', '.'], ['0', '', '', '']];
for (var row = 0; row < numberLayout.length; row++) {
for (var col = 0; col < numberLayout[row].length; col++) {
var buttonLabel = numberLayout[row][col];
if (buttonLabel === '') continue; // Skip empty positions
var buttonType = 'number';
var callback;
if (buttonLabel === '.') {
callback = inputDecimal;
} else if (buttonLabel === 'C') {
buttonType = 'clear';
callback = clearAll;
} else if (buttonLabel === '¢') {
callback = function callback() {
if (currentDisplay !== '0') {
var value = parseFloat(currentDisplay) / 100;
currentDisplay = value.toString();
updateDisplay();
}
};
} else {
callback = function (num) {
return function () {
inputNumber(num);
};
}(buttonLabel);
}
var button = new CalculatorButton(buttonLabel, buttonType, callback);
// Make buttons bigger and center them
button.x = 400 + col * 300;
button.y = 800 + row * 200;
buttons.push(button);
game.addChild(button);
}
}
// Initialize display
updateDisplay();