AndroidLock

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"

@CNCode Here’s an example, but I’m not sure if this does everything you were doing.

EDIT: Added code for the textLock.


function setup()
    parameter.boolean("textLock", false)
    parameter.text("value","")
    parameter.text("passwrd",25896)

    rectMode(CENTER)    -- draw rectangles centered on x,y values
    size=130   -- size of rectangle
    str=""
end

function draw()
    background(40, 40, 50)
    local count=0   -- set count to 0
    for y=1,3 do    --  loop y from 1 to 3
        for x=1,3 do    -- loop x from 1 to 3
            count=count+1   -- add 1 to count
            fill(255,0,0)   -- set color to red
            for z=1,#value do  -- make touched squares green
                if count==tonumber(string.sub(value,z,z)) then
                    fill(0,255,0)
                end
            end
            rect(x*size,y*size,size,size)   -- draw square at x,y
            fill(255)
            if not textLock then    -- show square numbers
                text(count,x*size,y*size)
            end            
        end
    end
    text(str,WIDTH/2,HEIGHT-200)    -- display str contents
end

function touched(t)
    if t.state==MOVING then -- finger is moving on the screen
        str=""  -- clear str
        local count=0   -- set count to 0
        for y=1,3 do    -- loop y from 1 to 3
            for x=1,3 do    -- loop x from 1 to 3
                count=count+1   --add 1 to count
                -- determine which square is touched
                if t.x>x*size-size/2 and t.x<x*size+size/2 and
                        t.y>y*size-size/2 and t.y<y*size+size/2 then
                    -- cant be same number multiple times in a row
                    if count~=tonumber(string.sub(value,string.len(value))) then
                        value=value..count
                    end
                end
            end
        end
    end
    if t.state==ENDED then  -- when done, check password match
        str="password doesn't match"
        if value==passwrd then
            str="password match"
        end
        value=""
    end
end

@CNCode Welcome to the forum. You don’t need classes for your code as shown in my example. You did a lot of coding for just starting out, and I don’t think you asked for any help. That’s exactly how you’re going to learn, and you’re off to a great start. As you learn more, you’ll be able to shrink your code so there won’t be as much “pain in repetition”.

EDIT: Added code to the above code for textLock.

@dave1707 Thanks for the code, I will have a look at it. I joined the forum about a year ago, but the Codea app has just been sitting on my iPad, unused for a year. I did have some experience with python, but I guess that relative to everyone else, I am still new to Lua, Codea, and programming in general. I got some help from from my cousin, who is familiar with the C language. Your code is much neater that mine, as mine contains

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

massive it then else elseif blcoks like this. I dont exactly understand how you handle where the touches are, I think it is in this block of code:

function touched(t)
    if t.state==MOVING then
        str=""
        local count=0
        for y=1,3 do
            for x=1,3 do
                count=count+1
                if t.x>x*size-size/2 and t.x<x*size+size/2 and
                        t.y>y*size-size/2 and t.y<y*size+size/2 then
                    if count~=tonumber(string.sub(value,string.len(value))) then
                        value=value..count
                    end
                end
            end
        end
    end
    if t.state==ENDED then
        str="password doesn't match"
        if value==passwrd then
            str="password match"
        end
        value=""
    end
end

Though I do not really understand it. You lost me after the first “for” loop. Could you please give me a brief overview on this? Thanks
– CNCode

@CNCode The purpose of the double “for” loops is to draw the squares and check for touch. When the “for” loops are run, the x,y values will be 1,1 2,1 3,1 1,2 2,2 3,2 1,3 2,3 3,3. Since I set the size to 130, the size of the squares will be130 and I multiply the x,y values by 130 also. That give values of 130,130 260,130 390,130 130,260 260,260 260,390 130,390 260,390 390,390. rectMode is set for CENTER, so the squares are centered on the above x,y values. I use the same “for” loops in touched(), so I get the same center x,y values that were used in draw(). I subtract and add 1/2 the size of the square to get the edge values so I know when I’m inside a square. Once you understand what’s happening, it’s easier to code it than trying to explain it.

@dave1707 thanks!

@dave1707 I’ll post code in another forum later.

@dave1707 , I understood it the first time I read it, but now I don’t. I am now embarking on a different project that involves squares that change size and position, and if I remade the code I used for this project, it may be well over 300 lines. I think your code may be the solution to my problems, but I have no clue how to understand it, yet alone implement it. Could you please give a line by line explanation? Thanks.

@CNCode I added comments to my code above if you still want to see it. Here is simpler code to draw multiple squares. The number of rows and columns and size can be changed. It also turns the square you touch to green.


displayMode(FULLSCREEN)

function setup()
    rectMode(CENTER)
    xSize=14    -- number a columns
    ySize=19    -- number of rows
    size=50     -- size of squares
    sqr=vec2(0,0)   -- x,y value of touched square
end

function draw()
    background(40, 40, 50)
    for y=1,ySize do    -- loop from 1 to ySize
        for x=1,xSize do    -- loop from 1 to xSize
            fill(255)   -- set color to white
            if sqr.x==x and sqr.y==y then   -- square that was touched
                fill(0,255,0)   -- make it green
            end
            rect(x*size,y*size,size,size)   -- draw white or green square
        end
    end
end

function touched(t)
    if t.state==BEGAN then -- screen touched
        for y=1,ySize do    -- loop y from 1 to ySize
            for x=1,xSize do    -- loop x from 1 to xSize
                -- determine which square is touched
                if t.x>x*size-size/2 and t.x<x*size+size/2 and
                        t.y>y*size-size/2 and t.y<y*size+size/2 then
                    sqr=vec2(x,y)  -- save x,y value of touched square                
                end
            end
        end
    end
end

@dave1707 Thanks, As you can see in my code, I used a table to manage the touches. How would you translate the vec2 to the table? (1, 1) would be 1 and (1, 2) would be 2 and
3:(1, 3), 4:(2, 1) and so on. How would I do that?

@CNCode I’m not sure what code you’re referring to, but here’s an example to do what I think you’re asking.


function setup()
    tab={}
    
    -- fill table
    for y=1,3 do
        for x=1,3 do
            table.insert(tab,vec2(y,x))           
        end
    end
    
    -- print contents of table    
    for z=1,#tab do
        print(tab[z])
    end
end

-- AndroidLock


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")
    parameter.action("newPasscode", function()  password = slideInput end) -- work on
end



function touchWithin(x, y, width, height) -- Tell if a touch is within a certian area.
    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.
    x = 1
    for i = 1, 3 do
        for e = 1, 3 do
            if touchWithin(e*100, i*100) then
                return x
            end
            x = x + 1 
        end
    end
    return false
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
        fill(green)
        text("Unlocked", 550, 430)
    else
        Locked = red
        fill(red)
        text("Locked", 550, 430)
    end
    x = 1
    for i = 1, 3 do
        for e = 1, 3 do
            fill(colors[x])
            rect(e*100, i*100, 100, 100)
            x = x + 1
        end
    end
    fill(Locked)
    rect(500, 200, 100, 200)
    if not textLock then -- To make it look more impressive, there is a text lock paramater to hide the text on the rectangles. 
        fill(green)
        x = 1
        for i = 1, 3 do
            for e = 1, 3 do
                text(tostring(x), e*100+25, i*100+25)
                x = x + 1 
            end
        end
        fill(red)
    end
end

function touched(touch)
    if touch.state == ENDED then -- If the user lets go of the screen, make all rectangels red.
        colors = {red, red, red, red, red, red, red, red, red}
    elseif touch.state == BEGAN 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 the corrosponding 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 -- Amazing
    end -- Staircase
end --Made from "end"

mutch neater code

@CNCode - if you’d like some help with the Lua language, including classes, tables etc, I have an ebook on it, here

https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa

The user and all related content has been deleted.

Nat, this is his own thread. He can post an update if he wants.

The user and all related content has been deleted.

Enough now. You were posting on many old threads, not just your own.

The user and all related content has been deleted.