- Code:
keys = {
{ "C", "sin", "cos", "tan" },
{ "1/x", "x^2", "sqr", "/" },
{ "7", "8", "9", "*" },
{ "4", "5", "6", "-" },
{ "1", "2", "3", "+" },
{ "0", "+/-", ".", "=" } }
Creates a table called keys.
- Code:
color = Color.new(0, 255, 0)
Creates a color called new.
- Code:
function drawRect(x0, y0, w, h)
screen:drawLine(x0, y0, x0+w, y0, color)
screen:drawLine(x0, y0, x0, y0+h, color)
screen:drawLine(x0+w, y0, x0+w, y0+h, color)
screen:drawLine(x0+w, y0+h, x0, y0+h, color)
end
Creates a function called drawRect that looks like it draws a rectangle using some variables.
- Code:
oldPad = Controls.read()
Makes a variable called oldPad that reads the controls so you can see what you previously pressed.
Creates two variables called x and y and both are equal to 1.
Makes a string called text.
- Code:
lastNumber = 0
deleteOnKey = true
lastOperation = ""
value = ""
number = 0
Creates more random variables to be used later on.
So, this is a loop. As long as nothing dumb happens or this loop doesn't break....
- Code:
pad = Controls.read()
create a variable equal to the code...
- Code:
if pad ~= oldPad then
if the variable pad(look one line above) is not the same as old pad, and....
- Code:
if pad:triangle() then
if you're not pressing triangle then....
- Code:
screen:save("calculator.tga")
end
take a screenshot...
- Code:
if pad:left() then
x = x - 1
if x == 0 then
x = 4
end
However, if you're pressing left then the variable x is lowered by 1 but if x is 0 then it's changed to 4 (this is for a scrolling effect)
ends the "if pad:left() then" statement
- Code:
if pad:right() then
x = x + 1
if x == 5 then
x = 1
end
end
Same thing as above but reversed for the up button (the scrolling thing).
- Code:
if pad:up() then
y = y - 1
if y == 0 then
y = 6
end
end
if pad:down() then
y = y + 1
if y == 7 then
y = 1
end
end
The scrolling thing again but for the y variable and with left and right buttons.
- Code:
if pad:cross() then
value = keys[y][x]
number = tonumber(text)
if value == "C" then
text = "0"
lastNumber = 0
lastOperation = ""
deleteOnKey = true
If you press the cross button then the variable called value is equal to the keys variable times the y variable times the x variable (the keys variable could also be part of an equation) but if the value of the variable called value is equal to "C" then some variables are reset too...
- Code:
elseif value == "1/x" then
text = tostring(1/number)
but if the value of value is "1/x" then this code posts to the string of the string, text, 1 divided by the variable number
- Code:
elseif value == "sin" then
text = tostring(math.sin(number))
but if the value of value is "sin" then this code posts to the string of the string, text, sine the value of the variable number
- Code:
elseif value == "cos" then
text = tostring(math.cos(number))
see above
- Code:
elseif value == "tan" then
text = tostring(math.tan(number))
see above
- Code:
elseif value == "x^2" then
text = tostring(number*number)
see above
- Code:
elseif value == "sqr" then
text = tostring(math.sqrt(number))
see above
- Code:
elseif value == "/" or value == "*" or value == "-" or value == "+" or value == "=" then
but if the value of value is anything else then...
- Code:
if lastOperation == "/" then
text = tostring(lastNumber / number)
elseif lastOperation == "*" then
text = tostring(lastNumber * number)
elseif lastOperation == "-" then
text = tostring(lastNumber - number)
elseif lastOperation == "+" then
text = tostring(lastNumber + number)
end
This basically says make the string, text, equal to the variable lastNumber(insert selected symbol here)number.
Example:
lastNumber = 3
number= 5
selected symol is the /
The output would be ... 3/5
- Code:
number = tonumber(text)
This creates the output of the string called text and puts it in the form of the variable called number.
- Code:
if value == "=" then
text = tostring(number)
lastOperation = ""
If the value of value is equal to the equal sign ("="), then the string text has the value of whatever the value of the variable called number is.
lastOperation becomes nothing.
- Code:
else
lastNumber = number
lastOperation = value
end
But, if it's not equal to the equal sign, then lastNumber has the value of number and lastOperation has the value of value.
The variable deleteOnKey becomes true.
- Code:
elseif value == "+/-" then
text = tostring(-number)
If the value of value is "+/-" then the string, text, is inverted. If it were positive then it becomes negative and vise-versa.
- Code:
else
if deleteOnKey then
text = value
deleteOnKey = false
If deleteOnKey is true then the string called text has the value of he variable called value and then deleteOnKey becomes false.
- Code:
else
text = text .. value
end
Otherwise... text has the value of text and value
Ends some statements.
- Code:
if pad:start() then
break
end
If you press start then the while true do loop breaks and you don't do anything because the thing is not true. You just proceed to the next line.
The variable oldPad is equal to the variable pad.
Clears the screen.
- Code:
yk = 0
w = 40
h = 30
Sets up these variables.
- Code:
drawRect(w, 0, w * 4, h-2)
screen:print(w+4, 4, text, color)
Draws a rectangle at the given locations and prints out the text string in the same color as New
- Code:
for yindex,line in ipairs(keys) do
for xindex,key in ipairs(line) do
x0 = xindex * w
y0 = yindex * h
drawRect(x0, y0, w, h)
if xindex == x and yindex == y then
screen:fillRect(x0, y0, w, h, color)
foreground = Color.new(0, 0, 0)
else
foreground = color
end
screen:print(x0 + 5, y0 + 5, key, foreground)
end
end
Makes a pretty shape
![Very Happy :D](./images/smilies/icon_biggrin.gif)
- Code:
screen.waitVblankStart()
screen.flip()
end
Ending crap. Flips screen... yadda yadda yadda.
![Very Happy :D](./images/smilies/icon_biggrin.gif)