Page 1 of 3

Concerning Lua Coding...

PostPosted: Wed Aug 06, 2008 8:50 pm
by Yumiko
  Code:
keys = {
   { "C", "sin", "cos", "tan" },
   { "1/x", "x^2", "sqr", "/" },
   { "7", "8", "9", "*" },
   { "4", "5", "6", "-" },
   { "1", "2", "3", "+" },
   { "0", "+/-", ".", "=" } }

color = Color.new(0, 255, 0)

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

oldPad = Controls.read()
x = 1
y = 1
text = "0"
lastNumber = 0
deleteOnKey = true
lastOperation = ""
value = ""
number = 0

while true do
   pad = Controls.read()
   if pad ~= oldPad then
      if pad:triangle() then
         screen:save("calculator.tga")
      end
      if pad:left() then
         x = x - 1
         if x == 0 then
            x = 4
         end
      end
      if pad:right() then
         x = x + 1
         if x == 5 then
            x = 1
         end
      end
      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
      if pad:cross() then
         value = keys[y][x]
         number = tonumber(text)
         if value == "C" then
            text = "0"
            lastNumber = 0
            lastOperation = ""
            deleteOnKey = true
         elseif value == "1/x" then
            text = tostring(1/number)
         elseif value == "sin" then
            text = tostring(math.sin(number))
         elseif value == "cos" then
            text = tostring(math.cos(number))
         elseif value == "tan" then
            text = tostring(math.tan(number))
         elseif value == "x^2" then
            text = tostring(number*number)
         elseif value == "sqr" then
            text = tostring(math.sqrt(number))
         elseif value == "/" or value == "*" or value == "-" or value == "+" or value == "=" then
            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
            number = tonumber(text)
            if value == "=" then
               text = tostring(number)
               lastOperation = ""
            else
               lastNumber = number
               lastOperation = value
            end
            deleteOnKey = true
         elseif value == "+/-" then
            text = tostring(-number)
         else
            if deleteOnKey then
               text = value
               deleteOnKey = false
            else
               text = text .. value
            end
         end
      end
      if pad:start() then
         break
      end
      oldPad = pad
   end
   
   screen:clear()
   yk = 0
   w = 40
   h = 30
   drawRect(w, 0, w * 4, h-2)
   screen:print(w+4, 4, text, color)
   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
   screen.waitVblankStart()
   screen.flip()
end


Uhhm...Can anyone clearly elaborate what all of these functions specifically do?

PostPosted: Wed Aug 06, 2008 9:02 pm
by mice R nice
BWAWHAHA, crait

PostPosted: Wed Aug 06, 2008 9:03 pm
by roxfox64
It looks like a calculator. ;)

Would you like a specific command to be broken down?

PostPosted: Wed Aug 06, 2008 9:05 pm
by mice R nice
G3T S0M3!!!

It doesn't have enough number to be a calc

PostPosted: Wed Aug 06, 2008 9:10 pm
by AdventWolf
i see sine, cosine, and tangent.

PostPosted: Wed Aug 06, 2008 9:11 pm
by roxfox64
What are you talking about???

It has 0-9

PostPosted: Wed Aug 06, 2008 9:15 pm
by mice R nice
w0w I is blind, ma bad I didn't notice sin and cosine, blah

PostPosted: Wed Aug 06, 2008 9:38 pm
by Equ1ox
...............if i turn my head to the left and scroll down it looks like the batman sign lol

PostPosted: Wed Aug 06, 2008 9:45 pm
by mice R nice
That's hilariously true!

but where exactly do you want to go with this?

PostPosted: Wed Aug 06, 2008 9:51 pm
by Equ1ox
that i read coding in another way. idk i wish i had the attention span to learn but this is somthing to talk about in another thread. :P

PostPosted: Thu Aug 07, 2008 7:29 pm
by Yumiko
Yeah Rox, breaking it down would help me understand what these functions do :D

PostPosted: Thu Aug 07, 2008 7:37 pm
by roxfox64
You can call me T or Torrance or Fox, but not Rox.

PostPosted: Thu Aug 07, 2008 7:41 pm
by Yumiko
Okay then...?

PostPosted: Thu Aug 07, 2008 10:04 pm
by mice R nice
awkward

PostPosted: Fri Aug 08, 2008 4:15 pm
by crait
  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.

  Code:
x = 1
y = 1

Creates two variables called x and y and both are equal to 1.

  Code:
text = "0"

Makes a string called text.

  Code:
lastNumber = 0
deleteOnKey = true
lastOperation = ""
value = ""
number = 0

Creates more random variables to be used later on.

  Code:
while true do

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)
  Code:
      end

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.

  Code:
            deleteOnKey = true

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

  Code:
         end
      end

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.

  Code:
      oldPad = pad
   end

The variable oldPad is equal to the variable pad.

  Code:
   screen:clear()

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 :D

  Code:
   screen.waitVblankStart()
   screen.flip()
end

Ending crap. Flips screen... yadda yadda yadda. :D