Forum    News    Downloads    Saved Games


Concerning Lua Coding...

<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Wed Aug 06, 2008 8:50 pm

Concerning Lua Coding...

  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?
由美子
<<

mice R nice

User avatar

Brewery Master
Brewery Master

Posts: 1369

Joined: June 30 2007

Location: Caught in a mouse trap... DARN

Thanks given: 0

Thanks received: 0

Post Wed Aug 06, 2008 9:02 pm

BWAWHAHA, crait
Image
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Wed Aug 06, 2008 9:03 pm

It looks like a calculator. ;)

Would you like a specific command to be broken down?
5.00M33-6
Image
Image Veemon
Image
Image
<<

mice R nice

User avatar

Brewery Master
Brewery Master

Posts: 1369

Joined: June 30 2007

Location: Caught in a mouse trap... DARN

Thanks given: 0

Thanks received: 0

Post Wed Aug 06, 2008 9:05 pm

G3T S0M3!!!

It doesn't have enough number to be a calc
Image
<<

AdventWolf

User avatar

Brew Guru
Brew Guru

Posts: 5958

Joined: September 19 2007

Location: Pandora

Thanks given: 5 times

Thanks received: 10 times

Post Wed Aug 06, 2008 9:10 pm

i see sine, cosine, and tangent.
Image
Image
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Wed Aug 06, 2008 9:11 pm

What are you talking about???

It has 0-9
5.00M33-6
Image
Image Veemon
Image
Image
<<

mice R nice

User avatar

Brewery Master
Brewery Master

Posts: 1369

Joined: June 30 2007

Location: Caught in a mouse trap... DARN

Thanks given: 0

Thanks received: 0

Post Wed Aug 06, 2008 9:15 pm

w0w I is blind, ma bad I didn't notice sin and cosine, blah
Image
<<

Equ1ox

User avatar

Experienced Brewer
Experienced Brewer

Posts: 454

Joined: January 30 2008

Location: At a Gears of War Tournament.

Thanks given: 0

Thanks received: 0

Post Wed Aug 06, 2008 9:38 pm

...............if i turn my head to the left and scroll down it looks like the batman sign lol
The original Equ1ox since 1997.
Image
<<

mice R nice

User avatar

Brewery Master
Brewery Master

Posts: 1369

Joined: June 30 2007

Location: Caught in a mouse trap... DARN

Thanks given: 0

Thanks received: 0

Post Wed Aug 06, 2008 9:45 pm

That's hilariously true!

but where exactly do you want to go with this?
Image
<<

Equ1ox

User avatar

Experienced Brewer
Experienced Brewer

Posts: 454

Joined: January 30 2008

Location: At a Gears of War Tournament.

Thanks given: 0

Thanks received: 0

Post Wed Aug 06, 2008 9:51 pm

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
The original Equ1ox since 1997.
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Thu Aug 07, 2008 7:29 pm

Yeah Rox, breaking it down would help me understand what these functions do :D
由美子
<<

roxfox64

User avatar

Brew Guru
Brew Guru

Posts: 2287

Joined: June 12 2007

Location: Smyrna, GA Current Status: Its true... I am a fur.

Thanks given: 0

Thanks received: 2 times

Post Thu Aug 07, 2008 7:37 pm

You can call me T or Torrance or Fox, but not Rox.
5.00M33-6
Image
Image Veemon
Image
Image
<<

Yumiko

User avatar

Mega Brewer
Mega Brewer

Posts: 892

Joined: May 25 2008

Location: California, US Firmware: 3.90M33-3

Thanks given: 2 times

Thanks received: 2 times

Post Thu Aug 07, 2008 7:41 pm

Okay then...?
由美子
<<

mice R nice

User avatar

Brewery Master
Brewery Master

Posts: 1369

Joined: June 30 2007

Location: Caught in a mouse trap... DARN

Thanks given: 0

Thanks received: 0

Post Thu Aug 07, 2008 10:04 pm

awkward
Image
<<

crait

User avatar

Brewology Administrator
Brewology Administrator

Posts: 6488

Joined: August 11 2006

Location: Narnia!

Thanks given: 195 times

Thanks received: 51 times

Post Fri Aug 08, 2008 4:15 pm

  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
Next

Return to PSP Help

Who is online

Users browsing this forum: No registered users and 100 guests

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for blacklist.org.