My Little Game

--]]
--This is a small game I made and any suggestions and improvements would be greatly appreciated
--The Main Tab
-- Tilt Island 
-- By: Austin W.
supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
parameter.watch("Lives")
Lives = 3
GAME_PLAYING = 0
GAME_DEAD = 1
GAME_PAUSED = 2
state = GAME_PAUSED
function setup()
state = GAME_PAUSED
objectTable = {}
lastGenerated = ElapsedTime
interval = .16
num = 5  
table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
pColor = color(0, 0, 0, 255)
paddle = Paddle(WIDTH/2,5,80,15,pColor)
end
function draw()
spriteMode(CENTER)
sprite("Documents:Island Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
pushStyle()
fill(0, 0, 0, 255)
font("Copperplate-Light")
fontSize(30)
textMode(CENTER)
text("Tap Twice To Begin.", 500,250)
textAlign(CENTER)
fill(0, 0, 0, 255)
font("Copperplate-Light")
fontSize(50)
textMode(CENTER)
text("Tilt Island!", 500, 525)
textAlign(CENTER)
fill(0, 0, 0, 255)
font("Copperplate-Light")
fontSize(30)
textMode(CENTER)
text("Made By: Austin.",500,10)
textAlign(CENTER)
fill(0, 0, 0, 255)
font("Copperplate-Light")
fontSize(40)
textMode(CENTER)
text("Tilt the device to dodge the falling objects!", 500, 500)
textAlign(CENTER)
fill(0, 0, 0, 255)
font("Copperplate-Light")
fontSize(30)
textMode(CENTER)
text("Version 1.0.0",500,750)
textAlign(CENTER)
drawPaddle()
if state == GAME_PLAYING then
spriteMode(CENTER)
sprite("Documents:Island Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
pushStyle()
spriteMode(CENTER)
randomObjectGenerate()   
popStyle()       
score = ElapsedTime 
fill(255)
font("Futura-CondensedExtraBold")
fontSize(30)
textMode(CORNER)
score = ElapsedTime 
fill(255, 255, 255, 255)
font("Futura-CondensedExtraBold")
fontSize(30)
textMode(CORNER)
text("Score: "..score,10,HEIGHT-50)
fill(255)
font("Futura-CondensedExtraBold")
fontSize(30)
textMode(CORNER)
text("Lives: Coming Soon! "..Lives,910,725)
drawPaddle()
end
end
function randomObjectGenerate()
for k,v in pairs(objectTable) do 
v:draw()    
end
if ElapsedTime - lastGenerated > interval then
lastGenerated = ElapsedTime
num = num + 1
table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))  
end
for k,v in pairs(objectTable) do 
if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
table.remove(objectTable,k)
v.a:destroy()
v.a = nil
v=nil
end
end
end
function drawPaddle()
paddle:draw()
end
function touched(touch)
if touch.tapCount == 2 and touch.state == ENDED then
state = GAME_PLAYING
if touch.tapCount == 2 and touch.state == ENDED then
start = ElapsedTime
end
end
end
--]]

--The Paddle Tab
--]]
Paddle = class()
function Paddle:init(posx,posy,pWidth,pHeight,pColor)
self.pWidth = pWidth
self.pHeight = pHeight
self.posx = posx
self.posy = posy
pos = vec2(posx,posy)
size = vec2(pWidth,pHeight)
self.pColor = pColor
end
function Paddle:draw()
checkGrav()
fill(pColor)   
rect(pos.x,pos.y,self.pWidth,self.pHeight)
end
--This Function checks the Gravity
function checkGrav()
if pos.x >= 0 and pos.x <= WIDTH - size.x then
if Gravity.x > 0 then
pos.x = pos.x + (20 * Gravity.x)
end  
if Gravity.x < 0 then
pos.x = pos.x - (-20 * Gravity.x)
end    
end 
if pos.x < 0 then
pos.x = 0
end
if pos.x > WIDTH - size.x then
pos.x = WIDTH - size.x
end
end
--]]

 --]]
--The Object Tab
--]]
Objects = class()
function Objects:init(x,cat,id)
self.a = physics.body(CIRCLE,20)
self.a.x = x
self.a.y = HEIGHT
self.a.gravityScale = 1
self.a.restitution = 0.0
self.a.interpolate = true
self.asleepingAllowed = true
self.flag = true
self.category = cat
self.a.info = {}
self.a.info.id = id
end
function Objects:draw()
pushMatrix()
translate(self.a.x, self.a.y)
if self.category == 1 then
sprite("Planet Cute:Rock",0,0,50,88)
elseif self.category == 2 then
sprite("Small World:Rock",0,0,50,51)
elseif self.category == 3 then
sprite("Planet Cute:Brown Block",0,0,50,80)
elseif self.category == 4 then
sprite("Planet Cute:Plain Block",0,0,50,85)         
end
popMatrix()
end   
function Objects:touched(touch)
end
--]]

Sorry about the format, Forums Did it.

I will try to repost

I’ve edited the original post to format the code. Please read the FAQ for formatting tips.

Thx :slight_smile:

Hi @Codenoob,

Split off the classes and stripped out duplication on your code. Main code given below. Don’t need to keep redefining text characteristics etc. Also, your text is black against a black background (as you load your background image from your DropBox - which we can’t access - when you post put an image in which is in the sprite packs).

After all that couldn’t get the paddle to capture the falling objects.


-- CodeNoob Tilt Island By: Austin W.
--This is a small game I made and any suggestions and improvements 
-- would be greatly appreciated

supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)
parameter.watch("Lives")
Lives = 3
GAME_PLAYING = 0
GAME_DEAD = 1
GAME_PAUSED = 2
state = GAME_PAUSED

function setup()
    state = GAME_PAUSED
    objectTable = {}
    lastGenerated = ElapsedTime
    interval = .16
    num = 5  
    table.insert(objectTable,Objects(math.random(1,WIDTH-    1),math.random(1,4),num))
    pColor = color(0, 0, 0, 255)
    paddle = Paddle(WIDTH/2,5,80,15,pColor)
end

function draw()
    spriteMode(CENTER)
    sprite("Dropbox:Background_H",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    pushStyle()
    fill(0, 0, 0, 255)
    font("Copperplate-Light")
    fontSize(30)
    textMode(CENTER)
    text("Tap Twice To Begin.", 500,250)
    textAlign(CENTER)
    fontSize(50)
    text("Tilt Island!", 500, 525)
    fontSize(30)
    text("Made By: Austin.",500,10)
    fontSize(40)
    text("Tilt the device to dodge the falling objects!", 500, 500)
    fontSize(30)
    text("Version 1.0.0",500,750)
    drawPaddle()
    if state == GAME_PLAYING then
        spriteMode(CENTER)
        sprite("Dropbox:Background_H",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
        pushStyle()
            spriteMode(CENTER)
            randomObjectGenerate()   
        popStyle()       
        score = ElapsedTime 
        fill(255)
        font("Futura-CondensedExtraBold")
        fontSize(30)
        textMode(CORNER)
        score = ElapsedTime 
        fill(255, 255, 255, 255)
        text("Score: "..score,10,HEIGHT-50)
        fill(255)
        text("Lives: Coming Soon! "..Lives,910,725)
        drawPaddle()
    end
end

function randomObjectGenerate()
    for k,v in pairs(objectTable) do 
    v:draw()    
    end

    if ElapsedTime - lastGenerated > interval then
        lastGenerated = ElapsedTime
        num = num + 1     
        table.insert(objectTable,Objects(math.random(1,WIDTH- 1), math.random(1,4), num))  
    end
    for k,v in pairs(objectTable) do 
        if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
        table.remove(objectTable,k)
        v.a:destroy()
        v.a = nil
        v=nil
        end
    end
end

function drawPaddle()
    paddle:draw()
end

function touched(touch)
    if touch.tapCount == 2 and touch.state == ENDED then
        state = GAME_PLAYING
        if touch.tapCount == 2 and touch.state == ENDED then
            start = ElapsedTime
        end
    end
end
--



--This Function checks the Gravity
function checkGrav()
    if pos.x >= 0 and pos.x <= WIDTH - size.x then
        if Gravity.x > 0 then
            pos.x = pos.x + (20 * Gravity.x)
        end  
        if Gravity.x < 0 then
            pos.x = pos.x - (-20 * Gravity.x)
        end    
    end 
    if pos.x < 0 then
        pos.x = 0
    end
    if pos.x > WIDTH - size.x then
        pos.x = WIDTH - size.x
    end
end

Hope that helps.

Bri_G

P.s. anyone know how to colour code these listings?

:slight_smile:

.@Bri_G You can colour code the listing by encapsulating the code using pre lang = “lua” tag instead of the three ~.

For example:

function checkGrav()
    if pos.x >= 0 and pos.x <= WIDTH - size.x then
        if Gravity.x > 0 then
            pos.x = pos.x + (20 * Gravity.x)
        end  
        if Gravity.x < 0 then
            pos.x = pos.x - (-20 * Gravity.x)
        end    
    end 
    if pos.x < 0 then
        pos.x = 0
    end
    if pos.x > WIDTH - size.x then
        pos.x = WIDTH - size.x
    end
end

```

Hi @Reefwing,

function thankYou()
    for 1, millions do
          Print("Thanks Reefwing!!!")
    end
end

```

Just a test, didn't work on the preview but will post anyway. looked at the HTML source but can't see why this shouldn't format OK. Looked at the source and the browser is putting HTML friendly tags in for the < and > symbols. What's the trick?

Bri_G

 >:-)

Hi @CodeaNoob I also got rid of duplicate code you didn’t need. I also changed some of your sprites because we don’t have access to any sprites you have in your Documents or Dropbox. I also added code to detect when one of the falling objects collides with the paddle. See Paddle:collide() and the if statement I added in randomObjectGenerate(). You will have to modify Paddle:collide() to account for the width of the paddle and the widths of the different objects falling. I gave it some widths and heights just to give you a starting point.


supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    parameter.watch("Lives")
    Lives = 3
    GAME_PLAYING = 0
    GAME_DEAD = 1
    GAME_PAUSED = 2
    state = GAME_PAUSED
    objectTable = {}
    lastGenerated = ElapsedTime
    interval = .16
    num = 5  
    table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
    pColor = color(0, 0, 0, 255)
    paddle = Paddle(WIDTH/2,5,80,15,pColor)
end

function draw()
    spriteMode(CENTER)
    sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    pushStyle()    
    fill(0, 0, 0, 255)
    textMode(CENTER)
    textAlign(CENTER)
    font("Copperplate-Light")
    fontSize(30)
    text("Tap Twice To Begin.", 500,250)
    text("Version 1.0.0",500,750)
    text("Made By: Austin.",500,10)
    fontSize(50)
    text("Tilt Island!", 500, 525)
    fontSize(40)
    text("Tilt the device to dodge the falling objects!", 500, 500)
    if state == GAME_PLAYING then
        spriteMode(CENTER)
        sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
        pushStyle()
        randomObjectGenerate()   
        popStyle()       
        score = ElapsedTime 
        fill(255)
        font("Futura-CondensedExtraBold")
        fontSize(30)
        textMode(CORNER)
        score = ElapsedTime 
        fill(255, 255, 255, 255)
        text("Score: "..score,10,HEIGHT-50)
        text("Lives: Coming Soon! "..Lives,750,725)
    end
    drawPaddle()
end

function randomObjectGenerate()
    for k,v in pairs(objectTable) do 
        v:draw()    
    end
    if ElapsedTime - lastGenerated > interval then
        lastGenerated = ElapsedTime
        num = num + 1
        table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))  
    end
    for k,v in pairs(objectTable) do 
        if paddle:collide(v.a.x,v.a.y) and v.a.info.id > 0 then
            sound(SOUND_HIT, 46295)
            v.a.info.id=0
        end
        if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
            table.remove(objectTable,k)
            v.a:destroy()
            v.a = nil
            v=nil
       end
    end
end

function drawPaddle()
    paddle:draw()
end

function touched(touch)
    if touch.tapCount == 2 and touch.state == ENDED then
        state = GAME_PLAYING
        if touch.tapCount == 2 and touch.state == ENDED then
            start = ElapsedTime
        end
    end
end

--The Paddle Tab

Paddle = class()

function Paddle:init(posx,posy,pWidth,pHeight,pColor)
    self.pWidth = pWidth
    self.pHeight = pHeight
    self.posx = posx
    self.posy = posy
    pos = vec2(posx,posy)
    size = vec2(pWidth,pHeight)
    self.pColor = pColor
end

function Paddle:draw()
    checkGrav()
    fill(pColor)   
    rect(pos.x,pos.y,self.pWidth,self.pHeight)
end

function Paddle:collide(x,y)
    if x>pos.x and x<pos.x+self.pWidth and y>self.posy and y<self.posy+self.pHeight*2 then
        return true
    end
    return false
end

--This Function checks the Gravity
function checkGrav()
    if pos.x >= 0 and pos.x <= WIDTH - size.x then
        if Gravity.x > 0 then
            pos.x = pos.x + (20 * Gravity.x)
        end  
        if Gravity.x < 0 then
            pos.x = pos.x - (-20 * Gravity.x)
        end    
    end 
    if pos.x < 0 then
        pos.x = 0
    end
    if pos.x > WIDTH - size.x then
        pos.x = WIDTH - size.x
    end
end

--The Object Tab

Objects = class()

function Objects:init(x,cat,id)
    self.a = physics.body(CIRCLE,20)
    self.a.x = x
    self.a.y = HEIGHT
    self.a.gravityScale = 1
    self.a.restitution = 0.0
    self.a.interpolate = true
    self.asleepingAllowed = true
    self.flag = true
    self.category = cat
    self.a.info = {}
    self.a.info.id = id
end

function Objects:draw()
    pushMatrix()
    translate(self.a.x, self.a.y)
    if self.category == 1 then
        sprite("Planet Cute:Rock",0,0,50,88)
    elseif self.category == 2 then
        sprite("Small World:Rock",0,0,50,51)
    elseif self.category == 3 then
        sprite("Planet Cute:Brown Block",0,0,50,80)
    elseif self.category == 4 then
        sprite("Planet Cute:Plain Block",0,0,50,85)         
    end
    popMatrix()
end  
 
function Objects:touched(touch)
end

Hi @dave1707,

That’s saved me some time. I intended looking further but was distracted by my lack of ability to fancy format code!!!

Thanks for that.

Bri_G

:slight_smile:

Hi All,

Looks like the lua formatting works but not immediately. It also looks like @QuantumTroll has a handle on this using CSS.

Bri_G

:slight_smile:

dave1707,
Thank You! I’ve been needing that collision help!
But is there any way to just take away 1 life for 1 collision instead of every second?
Any help is appreciated.
-CodeaNoob

I’m not sure how you were going to do your scoring, but a suggestion would be to start the game with maybe 10 or 20 lives and subtract a life every time the paddle is hit. When you don’t have any lives left, the game is over and the elapsed time is your score. The more objects you avoid, the higher the elapsed time. In the if statement I added in randomObjectGenerate(), just subtract 1 from Lives and check for 0. Once 0, set a flag to perform some sort of end of game function.

dave1707,
I did that, except every second the paddle is colliding, a life is taken away, so every object is a life loss of around 3.
Thanks,
CodeaNoob

I made too many changes to just show them so it was easier to just send the whole thing. I set Lives to 10, fixed the score to show (ElapsedTime - start). I subtract 1 from Lives each time the paddle is hit, and when Lives reaches 0, I set state to GAME_OVER and display the score on your startup screen where you can start the next game.


supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
    parameter.watch("Lives")
    Lives = 10
    GAME_PLAYING = 0
    GAME_DEAD = 1
    GAME_PAUSED = 2
    state = GAME_PAUSED
    objectTable = {}
    lastGenerated = ElapsedTime
    interval = .16
    num = 5  
    table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))
    pColor = color(0, 0, 0, 255)
    paddle = Paddle(WIDTH/2,5,80,15,pColor)
end

function draw()
    spriteMode(CENTER)
    sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
    pushStyle()    
    fill(0, 0, 0, 255)
    textMode(CENTER)
    textAlign(CENTER)
    font("Copperplate-Light")
    fontSize(30)
    text("Tap Twice To Begin.", 500,250)
    text("Version 1.0.0",500,750)
    text("Made By: Austin.",500,10)
    fontSize(50)
    text("Tilt Island!", 500, 525)
    fontSize(40)
    text("Tilt the device to dodge the falling objects!", 500, 500)
    if state == GAME_PLAYING then
        spriteMode(CENTER)
        sprite("SpaceCute:Background",WIDTH/2,HEIGHT/2,WIDTH,HEIGHT)
        pushStyle()
        randomObjectGenerate()   
        popStyle()       
        score = ElapsedTime 
        fill(255)
        font("Futura-CondensedExtraBold")
        fontSize(30)
        textMode(CORNER)
        score = ElapsedTime - start
        fill(255, 255, 255, 255)
        text("Score: "..string.format("%.1f",score),10,HEIGHT-50)
        text("Lives: "..Lives,800,725)
    end
    if state==GAME_OVER then
        fill(255)
        text("Score: "..string.format("%.1f",score),WIDTH/2,400)
    end
    drawPaddle()
end

function randomObjectGenerate()
    for k,v in pairs(objectTable) do 
        v:draw()    
    end
    if ElapsedTime - lastGenerated > interval then
        lastGenerated = ElapsedTime
        num = num + 1
        table.insert(objectTable,Objects(math.random(1,WIDTH-1),math.random(1,4),num))  
    end
    for k,v in pairs(objectTable) do 
        if paddle:collide(v.a.x,v.a.y) and v.a.info.id > 0 then
            sound(SOUND_HIT, 46295)
            v.a.info.id=0
            Lives=Lives-1
            if Lives<1 then
                state=GAME_OVER
            end
        end
        if v.a.y < 0 or (v.a.x > WIDTH or v.a.x< 0) or v.a.info.id == 0 then
            table.remove(objectTable,k)
            v.a:destroy()
            v.a = nil
            v=nil
       end
    end
end

function drawPaddle()
    paddle:draw()
end

function touched(touch)
    if touch.tapCount == 2 and touch.state == ENDED then
        state = GAME_PLAYING
        if touch.tapCount == 2 and touch.state == ENDED then
            start = ElapsedTime
            Lives=10
        end
    end
end

--The Paddle Tab

Paddle = class()

function Paddle:init(posx,posy,pWidth,pHeight,pColor)
    self.pWidth = pWidth
    self.pHeight = pHeight
    self.posx = posx
    self.posy = posy
    pos = vec2(posx,posy)
    size = vec2(pWidth,pHeight)
    self.pColor = pColor
end

function Paddle:draw()
    checkGrav()
    fill(pColor)   
    rect(pos.x,pos.y,self.pWidth,self.pHeight)
end

function Paddle:collide(x,y)
    if x>pos.x and x<pos.x+self.pWidth and y>self.posy and y<self.posy+self.pHeight*2 then
        return true
    end
    return false
end

--This Function checks the Gravity
function checkGrav()
    if pos.x >= 0 and pos.x <= WIDTH - size.x then
        if Gravity.x > 0 then
            pos.x = pos.x + (20 * Gravity.x)
        end  
        if Gravity.x < 0 then
            pos.x = pos.x - (-20 * Gravity.x)
        end    
    end 
    if pos.x < 0 then
        pos.x = 0
    end
    if pos.x > WIDTH - size.x then
        pos.x = WIDTH - size.x
    end
end

--The Object Tab

Objects = class()

function Objects:init(x,cat,id)
    self.a = physics.body(CIRCLE,20)
    self.a.x = x
    self.a.y = HEIGHT
    self.a.gravityScale = 1
    self.a.restitution = 0.0
    self.a.interpolate = true
    self.asleepingAllowed = true
    self.flag = true
    self.category = cat
    self.a.info = {}
    self.a.info.id = id
end

function Objects:draw()
    pushMatrix()
    translate(self.a.x, self.a.y)
    if self.category == 1 then
        sprite("Planet Cute:Rock",0,0,50,88)
    elseif self.category == 2 then
        sprite("Small World:Rock",0,0,50,51)
    elseif self.category == 3 then
        sprite("Planet Cute:Brown Block",0,0,50,80)
    elseif self.category == 4 then
        sprite("Planet Cute:Plain Block",0,0,50,85)         
    end
    popMatrix()
end  
 
function Objects:touched(touch)
end

dave1707,
THX!!!
U r the bestest!

I am having a Highscore bug, can anyone add Highscores successfully here? I’m sorry for all the demands, but I am a Noob to Codea, Hence my name :confused:

. @CodeaNoob Add the function showHighScore() anywhere in your code and add the line of code showHighScore() in the draw function in the if state==GAME_OVER section. See below for example.


-- find this if statement in the draw function.

    if state==GAME_OVER then
        fill(255)
        text("Score: "..string.format("%.1f",score),WIDTH/2,400)
        showHighScore()      ------- add this line to your code
    end
    drawPaddle()



-- add this function anywhere in your code

function showHighScore()
    pushStyle()
    fill(255)
    fontSize(40)
    font("Courier")
    hs=score
    for x=1,10 do
        str=string.format("TiltIsland%02d",x)
        sc=readLocalData(str)
        if sc==nil then
            saveLocalData(str,0)
            if hs>0 then
                saveLocalData(str,hs)
                hs=0
            end
        elseif hs==tonumber(sc) then
                break
        elseif hs>tonumber(sc) then
                saveLocalData(str,hs)
                hs=tonumber(sc)
        end
    end    
    for x=1,10 do
        str=string.format("TiltIsland%02d",x)
        sc=readLocalData(str) 
        str=string.format("%2d) %5.1f",x,sc)
        text(str,WIDTH-200,500-x*40)
    end
    popStyle()
end

Ah, See I tried to add Bit Invader style of Highscores, and that didn’t work. Thx again for all your help:)