Howdy, Stranger!

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

Setting Up a Game

Hello!

I have been using codea for a while now and have produced a few small projects. But I want to embark on making a full fledged game, with a homescreen and levels etc etc. I am not sure how to go about this, for example in xCode I can switch between different viewControllers which make it clear what gameState is running.

Is the best way to do this in Codea, is to add a bunch of if statements inside draw() and let them activate depending on which gameState I am currently in?

Thank you

Tagged:

Comments

  • dave1707dave1707 Mod
    Posts: 7,807

    @gogizgaz This isn’t a game, but the idea might be something like what you’re after. I move around to different screens that do different things with just 2 statements in draw. When you go into a web site, tap Done in the upper left of the screen to return back to the program.

    displayMode(FULLSCREEN)
    
    function setup() 
        func=menu    -- start screen
    
        buttonTab={}    -- buttons table
    
        -- buttons that show on the menu screen
        table.insert(buttonTab,button(350,600,100,50,"Screen 1",menu,screen1))
        table.insert(buttonTab,button(350,500,100,50,"Screen 2",menu,screen2))
        table.insert(buttonTab,button(350,400,100,50,"Screen 3",menu,screen3))
        table.insert(buttonTab,button(700,50,100,50,"EXIT",menu,close))
    
        -- buttons that show on screen1
        table.insert(buttonTab,button(350,600,150,50,"Google",screen1,url1))
        table.insert(buttonTab,button(350,500,150,50,"twolivesleft",screen1,url2))
        table.insert(buttonTab,button(350,100,150,50,"Menu",screen1,menu))
    
        -- buttons that show on screen2
        table.insert(buttonTab,button(350,600,150,50,"map quest",screen2,url3))
        table.insert(buttonTab,button(350,100,150,50,"Menu",screen2,menu))
    
        -- buttons that show on screen3
        table.insert(buttonTab,button(350,600,150,50,"amazon",screen3,url4))    
        table.insert(buttonTab,button(350,100,150,50,"Menu",screen3,menu))
    end
    
    function draw()
        background(40, 40, 50)
        func()    -- call the function set by the button
    end
    
    function touched(t)    -- check if a button was pressed
        if t.state==BEGAN then
            for a,b in ipairs(buttonTab) do
                if b:touched(t) then
                    break
                end                
            end
        end
    end
    
    function menu()
        background(58, 97, 136, 255)
        fontSize(30)
        text("Menu Screen",350,700)
        drawButtonTab()
    end
    
    function screen1()
        background(101, 43, 43, 255)
        fontSize(30)
        text("Screen 1",350,700)
        drawButtonTab()
    end
    
    function screen2()
        background(110, 165, 74, 255)
        fontSize(30)
        text("Screen 2",350,700)
        drawButtonTab()
    end
    
    function screen3()
        background(150, 144, 44, 255)
        fontSize(30)
        text("Screen 3",350,700)
        drawButtonTab()
    end
    
    function url1()
        openURL('http://www.google.com',true)
        func=screen1    -- return to screen1
    end
    
    function url2()
        openURL('http://twolivesleft.com',true)
        func=screen1    -- return to screen2
    end
    
    function url3()
        openURL('http://www.mapquest.com',true)
        func=screen2    -- return to screen3
    end
    
    function url4()
        openURL('http://www.amazon.com',true)
        func=screen3    -- return to screen4
    end
    
    function drawButtonTab()    -- draw selected buttons from button table
        fontSize(20)
        for a,b in ipairs(buttonTab) do
            b:draw()
        end
    end    
    
    button=class()
    
    function button:init(x,y,w,h,name,screen,func)
        self.x=x    -- x position
        self.y=y    -- y position
        self.w=w    -- width
        self.h=h    -- height
        self.name=name    -- name to put on the button 
        self.screen=screen -- screen to draw the button on
        self.func=func    -- function to call when the button is pressed
    end
    
    function button:draw()
        if func==self.screen then    -- draw the button
            sprite("Cargo Bot:Dialogue Button",self.x,self.y,120)
            fill(255, 0, 0, 255)
            text(self.name,self.x,self.y)
        end
    end
    
    function button:touched(t)
        if func==self.screen then    -- only check current screen button 
            if t.x>self.x-self.w/2 and t.x<self.x+self.w/2 and
                t.y>self.y-self.h/2 and t.y<self.y+self.h/2 then
                    func=self.func
                    return true
            end
            return false
        end
    end
    
  • Thanks this is a great way to do it!

Sign In or Register to comment.