I am new to Lua and made this simple program. essentially, you put in a password in the parameters, then you have to draw the password for it to unlock. this is similar to the way that the Android phone unlocks. Unfortunately, I do not know how to use classes, so you could imagine the pain in repetition. Could someone please give me tips on how to improve the code? Thanks.
-- AndroidLock
-- Neil AKA cncode
function setup()
red = color(255, 0, 0, 255) -- Set color variables, makes code readable.
green = color(0, 255, 0, 255) --Look at above comment
colors = {red, red, red, red, red, red, red, red, red} -- This is the colors of the tiles.
Locked = red -- When it is locked, it is red. There is code later to change it to green if unlocked.
slideInput = "" -- make a blank variable for the input
parameter.boolean("textLock", false)
parameter.watch("slideInput")
parameter.text("password", "25258")
end
function touchWithin(x, y) -- Tell if a touch is within a certian area, I would use classes, however I don't know how.
if x<CurrentTouch.x and CurrentTouch.x<(x+100) and y<CurrentTouch.y and CurrentTouch.y<y+100 then
return true
end
end
function touchInSquare() -- tells if a touch has moved over a square.
if touchWithin(100, 100) then
return 1
elseif touchWithin(200, 100) then
return 2
elseif touchWithin(300, 100) then
return 3
elseif touchWithin(100, 200) then
return 4
elseif touchWithin(200, 200) then
return 5
elseif touchWithin(300, 200) then
return 6
elseif touchWithin(100, 300) then
return 7
elseif touchWithin(200, 300) then
return 8
elseif touchWithin(300, 300) then
return 9
else return false
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(52, 52, 52, 255)
if slideInput == password then -- self explainatory
Locked = green
text("Unlocked", 550, 430)
else
Locked = red
text("Locked", 550, 430)
end
fill(colors[1]) -- THIS TOOK FOREVER
rect(100, 100, 100, 100) -- I had to make a rectangle for each of the squares and fill it with the color.
fill(colors[2])
rect(200, 100, 100, 100)
fill(colors[3])
rect(300, 100, 100, 100)
fill(colors[4])
rect(100, 200, 100, 100)
fill(colors[5])
rect(200, 200, 100, 100)
fill(colors[6])
rect(300, 200, 100, 100)
fill(colors[7])
rect(100, 300, 100, 100)
fill(colors[8])
rect(200, 300, 100, 100)
fill(colors[9])
rect(300, 300, 100, 100)
fill(Locked)
rect(500, 200, 100, 200)
if not textLock then -- To make it look impressive, there is a text lock paramater to hide the text on the rectangles.
fill(green)
text("1", 125, 125)
text("2", 225, 125)
text("3", 325, 125)
text("4", 125, 225)
text("5", 225, 225)
text("6", 325, 225)
text("7", 125, 325)
text("8", 225, 325)
text("9", 325, 325)
fill(red)
end
end
function touched(touch)
if CurrentTouch.state == 2 then -- If the user lets go of the screen, make all rectangels red.
colors = {red, red, red, red, red, red, red, red, red}
elseif CurrentTouch.state == 0 then -- When the user first touches the screen, reset the previous input.
slideInput = ""
elseif touchInSquare() ~= false then
number = touchInSquare() -- set temporary variable(number) to the square that was touched.
colors[number] = green -- Make that corrosponds to the number rectange green
if string.sub(slideInput, -1, -1) ~= tostring(number) then -- If the previous number in "slideInput" was equal to the same number, do not do the following code.
slideInput = slideInput .. tostring(number) -- Update "slideInput"
end --Upsidedown
end -- Staircases
end --Made from "end"