Help with text boxes and login page for trivia app

I’m new, so I apologize in advance for my lack of knowledge. I am trying to create a general trivia app with text boxes for answers. I’d also like to have a login page and leaderboard for the amount of time it takes to answer all questions. Are there any games like this with codes already written that I can emulate? Any help on login pages, or questions with text answers would be great!
Thanks!
Amy

Hi, Amy, no apologies necessary. We all start off knowing nothing about Codea.

I’ve edited your title so it will be clearer to forum users. There are plenty of past discussions about text boxes so I’m sure someone will help you with this.

@Stowell5005 Here’s something I found searching the forum. Not sure if this is what you’re after. In the table.insert lines of code, set up the question and the answer. You can have as many questions and answers as you want.

EDIT: Code changed to include updates.


displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)

function setup()
    fontSize(30)
    textMode(CORNER)
    showKeyboard()
    ans="_________"
    col=255
    func=login
    tab={}
    table.insert(tab,{a="What is the largest planet.",b="Jupiter"})
    table.insert(tab,{a="How much is 5 + 8",b="13"})
    table.insert(tab,{a="How many inches in a foot.",b="12"})
    table.insert(tab,{a="What is the largest ocean.",b="Pacific"})
end

function draw()
    background(40,40,50)
    fill(255)
    func()
end

function login()
    text("Enter your name",250,HEIGHT-200)
    text(ans,500,HEIGHT-200)    
end

function quest()
    fill(255)
    text("Press return to check your answer or for the next question.",100,HEIGHT-50)
    text(tab[question].a.." "..ans,200,HEIGHT-250)
    text(string.format("%s, your time so far is %.2f seconds.",name,ElapsedTime-et),200,HEIGHT-140)
    fill(col)
    text(str,300,HEIGHT-300)

end

function score()
    text(string.format("%s  Your time was  %d minute(s)  and  %.2f seconds.",name,math.floor(total/60),total%60),100,HEIGHT-200)
    text("Press return to start again.",100,HEIGHT-300)
end

function keyboard(k)
    if k==BACKSPACE then
        ans=string.sub(ans,1,string.len(ans)-1)
        return
    end
    if k==RETURN then
        if func==login then
            name=ans
            func=quest
            ans="_________"
            str=""
            question=1
            et=ElapsedTime
            return
        end
        if func==score then
            func=login
            return
        end
        if func==quest then
            if str=="CORRECT" then
                ans="_________"
                str=""
                question=question+1
                if question>#tab then
                    total=ElapsedTime-et
                    func=score
                end
                return
            end
            if str=="INCORRECT" then
                ans="_________"
                str=""
                return
            end
            if string.lower(ans)==string.lower(tab[question].b) then
                col=color(0,255,0)
                str="CORRECT"
            else
                col=color(255,0,0)
                str="INCORRECT"
            end
            return
        end
    end
    if ans=="_________" then
        ans=""
    end
    ans=ans..k  
end

function touched(t)
    if not isKeyboardShowing() then
        showKeyboard()
    end
end

Thank you so much! That is exactly what I am looking for in the question/answer part of my app. Any ideas on how to create a login page?

@Stowell5005 - programming this will require some knowledge of Lua (the language behind Codea) and tables (which provide arrays in Lua) in particular.

You will need one table for user names, passwords and scores, and another for questions and answers (or several, if you prefer).

Creating a login screen can be done with code similar to that above.

If you haven’t done so, already, I suggest looking at the wiki page above to get a feel for Lua. Just ask if you get stuck.

@Stowell5005 - I should have added that to create a separate login screen, all you need is an “if” test in the draw function that decides whether to draw the login or the questions screen. eg you could have a variable called state, and set it to 1 for login and 2 for questions, 3 for end of questions, and 4 for leaderboard, then the draw function can look at it and draw whichever screen applies.

@Stowell5005 I made changes to the code above. Do those fit your needs better.

The user and all related content has been deleted.