Ballons game

I had made a simple game a few days back (with the help of @Ignatz) so I thought I would share with you’ll.
Here’s a video for the game
http://m.youtube.com/watch?v=hmX3cXXwlFI
And here’s the code

--# Main
-- do three tap finger to bring up buttons to exit no button mode
function setup()
    displayMode(FULLSCREEN_NO_BUTTONS)
    gameover = false
    Power = 0
    vy = HEIGHT + 6
    playing = false
    score = 0
    s = {}
    l = {}
    h = {}
    pow = {}
    p = 0
    lives = 5
    level = 1
    physics.pause()
    physics.gravity(0,0)
    bullets = {}
    misses = {}
    ball = {}
    for i = 1,5 do
        ball[i] = Ballons(level)
    end
    sound(DATA, "ZgNAUwAwQEBAYEpAvYbyPnuCCD8JSd8+WQBAf0BAQEBAbUBA")
    parameter.watch("#misses")
    parameter.watch("#ball")
    parameter.watch("score")
    parameter.watch("level")
end

function draw()
    if not playing then
    MainScreen()
    end
    if playing then
        background(255, 255, 255, 255)
        strokeWidth(2)
        stroke(81, 31, 31, 255)
        line(75,0,75,HEIGHT)
        physics.resume()
        

    if collision ~= nil then
        ball[collision[1]].ballon:destroy()
        table.remove(ball,collision[1])
        bullets[collision[2]]:destroy()
        table.remove(bullets,collision[2])
        collision=nil
        score = score + math.ceil(level/3)
    end

    pushStyle()
    fill(0, 0, 0, 255) -- drawing the bullets
    for i,b in pairs(bullets) do
        sprite("Documents:arrow",b.x - 37,b.y,74)
        if b.x<1 or b.x>WIDTH or b.y<1 or b.y>HEIGHT then
            table.remove(bullets,1,b)
            table.insert(misses,1)
        end
    end
    
    sprite("Documents:female",25,y - 34)
    for i,b in pairs(ball) do
        if b.ballon.y > HEIGHT + b.radius then
            table.insert(misses,1)
            table.remove(ball,i,ball[i])
        end
        if b.ballon.x > WIDTH + b.radius then
            table.remove(ball,i,ball[i])
        end
    end
    popStyle()

    for i,b in pairs(ball) do
        pushStyle()
        fill(b.colr)
        ellipse(b.ballon.x, b.ballon.y, b.radius*2)
    end
        popStyle()
    
    Misses()
    p = level*2
    table.insert(pow,1,p)
    table.insert(l,1,level)
    
    for i = 1,1000 do
        if level == 5*i then
            pushStyle()
            fontSize(25)
            font("MarkerFelt-Thin")
            fill(0, 203, 255, 255)
            text("improve arrow speed -- score cut =", WIDTH/2, HEIGHT/2)
            text(pow[1], WIDTH/2 + 179, HEIGHT/2)
            text("continue without the boost",WIDTH/2, HEIGHT/2-150)
            popStyle()
            PowerUp = true
            physics.pause()
        end
    end
    
    if #ball == 0 then
        level = level + 1
        table.remove(misses,1)
        for i = 1,5 + level/2  do
            ball[i] = Ballons(level)
        end  
    end
    
    pushStyle()
    fill(255, 28, 0, 255)
    lives = 5 - #misses
    table.insert(h,1,lives)
    table.insert(s,1,score)
    fontSize(20)
    text("Lives left   ", WIDTH - 103, HEIGHT - 50)
    text(h[1], WIDTH - 50, HEIGHT - 50)
    text("Score       ", WIDTH - 105, HEIGHT - 75)
    text(s[1], WIDTH - 50, HEIGHT - 75)
    text("Level        ", WIDTH - 103, HEIGHT - 100)
    text(l[1], WIDTH - 50, HEIGHT - 100)
    
    end
end

function touched(touch)
    x = touch.x
    y = touch.y
    if not playing then
        if touch.state == BEGAN then
            if x>=WIDTH/2 - 30 and x<=WIDTH/2 + 30 and y>=HEIGHT/2 - 105 and y<=HEIGHT/2 - 85 then
                playing = true
            end
        end
    end
    
    if x<=75 and playing then
        if touch.state == BEGAN then
            createBullet()
        end
    end
    
    if PowerUp then
        if touch.x>=WIDTH/2-200 and touch.x<=WIDTH/2 + 200 and
           touch.y<=HEIGHT/2 + 30 and touch.y>=HEIGHT/2 - 30 then
            PowerUpBullets()
        end
        
        if touch.x>=WIDTH/2-100 and touch.x<=WIDTH/2 + 100 and
           touch.y<=HEIGHT/2 - 120 and touch.y>=HEIGHT/2 - 180 then
            PowerUp = false
            level = level + 1
            physics.resume()
        end
    end
    
    if gameover then
        if touch.x>=WIDTH/2-100 and touch.x<=WIDTH/2 + 100 and
           touch.y<=vy - 10 and touch.y>=vy - 90 then
            if touch.state == BEGAN then
                setup()
            end
        end
    end
end

function collide(contact) 
    if contact.bodyA.info~=nil and contact.bodyB.info~=nil then
        for i,v in pairs(ball) do
            if v.ballon==contact.bodyA or v.ballon==contact.bodyB then
                for j,bb in pairs(bullets) do
                    if bb==contact.bodyA or bb==contact.bodyB then
                        sound(DATA, "ZgNAXS4wQEBAQEBA/9iePiN3mD64nKs+cgBAf0BAQEBAb0BA  ")
                        collision={i,j}
                    end
                end
            end
        end
    end      
end
--# MainScreen
function MainScreen()
    background(255, 255, 255, 255)
    pushStyle()
    font("MarkerFelt-Thin")
    fontSize(25)
    fill(0, 199, 255, 255)
    text("no more than 5 lives", WIDTH - 380, HEIGHT - 50)
    text("waste an arrow or miss a balloon and lose a life", WIDTH - 250, HEIGHT - 75)
    popStyle()
    pushStyle()
    fontSize(100)
    font("SnellRoundhand-Black")
    fill(192, 26, 18, 117)
    text("BALLONS", WIDTH/2, HEIGHT/2)
    popStyle()
    pushStyle()
    font("MarkerFelt-Wide")
    fontSize(30)
    fill(0, 198, 255, 255)
    text("Start", WIDTH/2,HEIGHT/2 - 95)
    popStyle()
    pushStyle()
    font("MarkerFelt-Thin")
    fontSize(25)
    fill(0, 199, 255, 255)
    text("Touch behined this line to shoot arrows", 350, HEIGHT - 250)
    strokeWidth(3)
    stroke(0, 200, 255, 255)
    lineCapMode(ROUND)
    line(160,HEIGHT-250,75,HEIGHT-250)
    line(75,HEIGHT-250,85,HEIGHT-245)
    line(75,HEIGHT-250,85,HEIGHT-255)
    popStyle()
    pushStyle()
    strokeWidth(2)
    stroke(81, 31, 31, 255)
    line(75,0,75,HEIGHT)
    popStyle()
end
--# Ballons
Ballons = class()

function Ballons:init(l)
    self.lev = l
    self.radius = math.random(25,50)
    self.ballon = physics.body(CIRCLE,self.radius)
    self.ballon.x = math.random(150,WIDTH - self.radius)
    self.ballon.y = math.random(-HEIGHT/2,-self.radius)
    self.ballon.linearVelocity = vec2(0,math.random(50,50 + 10*self.lev))
    self.ballon.sensor = true
    self.ballon.info="ball"
    self.colr = c or color(math.random(255),math.random(255),math.random(255),math.random(100,150))
end
--# CreateBullet
function createBullet()
    local bullet = physics.body(CIRCLE,5)
    bullet.x = x
    bullet.y = y
    bullet.linearVelocity = vec2(750 + Power,0)
    bullet.restitution = 0
    bullet.type = DYNAMIC
    bullet.info = "bullet"
    table.insert(bullets,bullet)
end
--# GameOver
function Misses()
    if #misses >= 5 then
        gameover = true
        vy = vy - 3
        pushStyle()
        fill(255, 0, 0, 255)
        font("HoeflerText-BlackItalic")
        fontSize(50)
        text("Game Over: Score", WIDTH/2, vy)
        if vy <= HEIGHT/2 then
            vy = HEIGHT/2
        end
        if vy >= HEIGHT then
            sound(DATA, "ZgBAKgBLQFRAQEBAAAAAAK5T+j46fh4/UwBAf0BAQEBAZEBA")
        end
        pushStyle()
        fill(255, 0, 0, 255)
        text(s[1] ,WIDTH/2 + 300, vy)
        popStyle()
        physics.pause()
        popStyle()
        pushStyle()
        fill(255, 0, 0, 255)
        fontSize(50)
        text("Restart", WIDTH/2 + 20, vy - 50)
        popStyle()
    end
end
--# PowerUpBullets
function PowerUpBullets()
    Power = 50*level
    PowerUp = false
    level = level + 1
    score = score - level*2 + 1
    physics.resume()
end

https://www.dropbox.com/s/dnkd3aczm2eg082/Photo%20Apr%2027%2C%2011%2046%2033%20PM.jpg (arrow) and
https://www.dropbox.com/s/757dijs5c7gx334/Photo%20Apr%2027%2C%2011%2042%2014%20PM.jpg (female)

Very impressive - and artistic, too

I look forward to your future work!

Thanks!!!

Yes looks good!

I occasionally get the error “cannot concinate global BodyA” is that a bug?

I know I’ve got it once or twice but I still don’t know why I get it. But usually when play I don’t get it.
Can you tell when exactly do you get the error

The first two seconds after i press play, but its a rare occurance

Nice. Can you provide us with the graphics as well?

How do you do that? I was thinking whether you’ll would get the sprites or not. But how should I share the images?

One way is to save your images to Dropbox in a new folder, log into Dropbox and go to that folder, click the share icon (a little chain), it will pop up asking who to share with, just cancel that dialog and grab the URL in the address bar. Post that URL for others to get your images.

Added the arrow and female’s images to first post

I am not able to get the error @CodeaNoob to be able to find out problem. Sorry!!

Thanks for the images. I find that I can get better scores now that I can see the archer.

How long did it take to code the game? I’m thinking of using this as a demo for my “App in a Weekend” challenge - http://twolivesleft.com/Codea/Talk/discussion/2729/codea-startup-challenge-code-an-app-in-a-weekend

Ill try to find the error again to gt the line its on

Thanks @CodeaNoob. @inoddy after you know how to code things properly it shouldn’t take more than a few hours but it took me two to three days cause i had some doubts and things weren’t workIng properly. So I spent some time asking for answers out here. And of course you should have the idea in your mind.

Really cool concept!
However, when I use the images, the archer is much too large.
Did I miss something? ~laughs

That shouldn’t be it’ll be as large as in the video. Try this. Open this discussion in safari or chrome then tap on the link for the archer then long tap on the image and select save image. Go to the balloons code and in the mains tab select the sprite female then select documents then select photo library and choose the archer you just downloaded. Hope that works

I got the archer too large - I just added a width argument to the drawing code - something like width 95 worked well