Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Guess the number

Hi.
Here is a little "divertimento" of mine: guess the number. It is very simple, just for learning more about Codea.
Saludos ~O)

displayMode(FULLSCREEN)

function setup()
    t1,h1,w1=vec3(0,0,0) -- some variables' initialization
    t2,h2,w2=vec3(0,0,0) 
    hw=WIDTH/2           -- half width, half height
    hh=HEIGHT/2        
    eh=HEIGHT-400        -- set height of different options
    nh=HEIGHT-500        
    hhh=HEIGHT-600
    func=menu
    texts={}             -- text array
    texts[1]="Think a number between 1 and 15 and press GO (below)..."
    texts[2]="1   2   3   4   5"
    texts[3]="6   7   8   9   10"
    texts[4]="11   12   13   14   15"
    texts[5]="Is your number here?"
    texts[6]="1   3   5   7   9   11   13  15"
    texts[7]="2   3   6   7   10   11  14   15"
    texts[8]="4   5   6   7   12   13   14   15"
    texts[9]="8   9    10   11   12   13   14   15"
    texts[10]="Your number is "
end

function draw()
    font("Georgia")
    fontSize(48)           
    fill(255, 255, 255, 255)   
    textWrapWidth(WIDTH-100)
    func()
end

function menu()
    background(56, 110, 222, 255)      
    title=texts[1]
    textAlign(CENTER)
    text(title,hw,HEIGHT-65)
    text(texts[2],hw,HEIGHT-200)
    text(texts[3],hw,HEIGHT-300)
    text(texts[4],hw,HEIGHT-400)
    fill(0, 255, 47, 255)
    t3="Go!"
    text(t3,hw,hhh)  
    w3,h3=textSize(t3)   
    suma=0
end

function menu2()
    background(40,40,50)
    text(texts[5],hw,HEIGHT-35)
    if q==1 then text(texts[6],hw,HEIGHT-200)
    elseif q==2 then text(texts[7],hw,HEIGHT-200)
    elseif q==3 then text(texts[8],hw,HEIGHT-200)
    elseif q==4 then text(texts[9],hw,HEIGHT-200)
    end
    t1="Yes"
    text(t1,hw+300,eh)
    w1,h1=textSize(t1)     

    t2="No"            
    text(t2,hw+300,nh)
    w2,h2=textSize(t2)     

    t4="Restart"
    text(t4,hw+300,hhh)
    w4,h4=textSize(t4)
end

function menu3()
    background(220, 53, 26, 255)
    text(texts[10]..math.tointeger(suma).."!",hw-200,HEIGHT-400)
    t4="Restart"    
    text(t4,hw+300,hhh)
end

function touched(t)     -- looking for touched text
    if t.state==BEGAN then
        if func==menu and t.x>hw-w3/2 and t.x<hw+w3/2 and t.y>hhh-h3/2 and t.y<hhh+h3/2 then
            q=1
            func=menu2
        elseif func==menu2 and t.x>hw-w1/2+300 and t.x<hw+w1/2+300 and t.y>eh-h1/2 and t.y<eh+h1/2 then
            suma=suma+2^(q-1)
            q=q+1
            if q<5 then func=menu2 else 
            func=menu3 end
        elseif func==menu2 and t.x>hw-w2/2+300 and t.x<hw+w2/2+300 and t.y>nh-h2/2 and t.y<nh+h2/2 then
            q=q+1
            if q<5 then func=menu2 else 
              func=menu3 end
        elseif func ~= menu and t.x>hw-w4/2+300 and t.x<hw+w4/2+300 and t.y>hhh-h4/2 and t.y<hhh+h4/2 then
            func=menu
        end
    end  
end

Comments

  • IgnatzIgnatz Mod
    Posts: 5,396

    ¬°Muy bien!
    (that is the limit of my Spanish)

  • dave1707dave1707 Mod
    Posts: 8,976

    @quezadav Nice game.

  • Posts: 226

    Thanks. It's intended for the amusement of math pupils and for Codea starters.
    Ignatz, we could exchange Spanish by Codea knowledge (I'd trade my Spanish :)) ). By the way: Your codea blog is my bible ^:)^

  • IgnatzIgnatz Mod
    Posts: 5,396

    I'm happy to help you. I admire anyone who can learn Codea in a language that is not their own. It was hard enough for me to learn, and I speak English!

  • Posts: 257

    it's magic. i like it. it think it works too if you replace number by cards.

    @ignatz yes it's difficult to understand when it doesn t exist any book about lua in french and i've learned german 4 years before english :((

  • Posts: 226

    @hpsoft, yeah, that's right, I use it to guess chemistry element's cards too.

  • Posts: 154

    Very nice and simple. Some programs are so addictive - this is one of them. Well done, @quezadav

  • Posts: 226

    Thanks @juce, I'm happy you liked it :-)

  • dave1707dave1707 Mod
    Posts: 8,976

    @quezadav Here's one of mine.

    displayMode(FULLSCREEN)
    supportedOrientations(PORTRAIT_ANY)
    
    function setup()
        state=1
        size=14
        pos=0
        tab1,tab2,tab3={},{},{}
        for z=1,size^2 do
            tab1[z]=z
            tab3[z]=0
        end
        for z=1,size^2 do
            v=math.random(#tab1)
            tab2[z]=tab1[v]
            table.remove(tab1,v)
        end
    end
    
    function draw()
        background(0)
        fill(255)
        if state==1 then
        text("I'll guess your number.",WIDTH/2,HEIGHT-20)
            text("Mentally choose a number, then press the circle above it.",WIDTH/2,HEIGHT-50)
            text("I'll then mix the numbers up again.",WIDTH/2,HEIGHT-80)
        end
        if state==2 then
            text("Find your number again, then press the circle above it.",WIDTH/2,HEIGHT-50)
        end
        if state==3 then
            text("Your number is in red.",WIDTH/2,HEIGHT-50)
            text("Tap the screen to pick another number.",WIDTH/2,HEIGHT-120)
        end
        for z=1,size do
            ellipse(z*50,HEIGHT-200,40)
        end
        c=0
        for x=1,size do
            for y=1,size do
                c=c+1
                fill(255)
                if c==pos then
                    fill(255,0,0)
                end
                text(tab2[c],x*50,HEIGHT-200-y*50)
            end
        end    
    end
    
    function next()
        tab1={}
        for z=1,size do
            tab1[z]=tab2[(col-1)*size+z]
            tab2[(col-1)*size+z]=0
        end
        for z=size^2,1,-1 do
            if tab2[z]==0 then
                table.remove(tab2,z)
            end
        end
        r=math.random(size)
        for z=1,size do
            v=math.random(#tab1)
            tab3[(z-1)*size+r]=tab1[v]
            table.remove(tab1,v)
        end
        for z=1,size^2 do
            if tab3[z]==0 then
                v=math.random(#tab2)
                tab3[z]=tab2[v]
                table.remove(tab2,v)
            end
        end
        tab2=tab3
    end
    
    function touched(t)
        if t.state==BEGAN then
            if state==3 then
                restart()
            end
            if t.y>HEIGHT-220 and t.y<HEIGHT-180 then
                for z=1,size do
                    if t.x>z*50-20 and t.x<z*50+20 then
                        col=z
                        if state==1 then
                            state=2
                            next()                        
                            return
                        elseif state==2 then
                            state=3
                            pos=r+(col-1)*size
                        end
                    end
                end
            end
        end    
    end
    
  • Posts: 226

    Wow @dave1707! I presented a Ford-T, you've shown a Ferrari :P

  • dave1707dave1707 Mod
    Posts: 8,976

    @quezadav This was from a simple idea that I expanded into a larger size.

Sign In or Register to comment.