Bouncy

A little game I made to test the new Physics Engine.

`
-- Bouncy
-- Herwig Van Marck
function setup()
    print("Try hitting the blinking hearts")
    print("Tap to flip the direction of gravity")
    print("However, every flip costs you 1 point!")
    start()
end

function start()
    glow=0
    targetsLeft=10
    maxScore=readProjectData("maxScore") or 0
    obj=physics.body(CIRCLE,25)
    obj.x=200
    obj.y=200
    obj.restitution=0.60
    obj.sleepingAllowed=false
    walls={}
    minx=0
    maxx=WIDTH
    miny=0
    maxy=HEIGHT
    score=0
    createWall()
    createTarget()
    gameOver=nil
end

function createWall()
    local points={}
    local x=50
    local y=50
    local margin=50
    
    minx=x
    miny=y
    for x2=x,WIDTH-margin,10 do
        local v=vec2(x2,y)
        y=math.abs(y+math.random(-10,10))
        if (y>miny) then miny=y end
        table.insert(points,v)
        x=x2
    end
    maxx=x
    x=WIDTH - x
    for y2=y+10,HEIGHT -margin-30,10 do
        local v=vec2(WIDTH - x,y2)
        x=math.abs(x+math.random(-10,10))
        if(WIDTH -x<maxx) then maxx=WIDTH -x end
        table.insert(points,v)
        y=y2
    end
    maxy=y
    y=HEIGHT - y
    for x2=WIDTH -x-10,margin,-10 do
        local v=vec2(x2,HEIGHT - y-30)
        y=math.abs(y+math.random(-10,10))
        if (HEIGHT -y<maxy) then maxy=HEIGHT -y end
        table.insert(points,v)
        x=x2
    end
    
    for y2=HEIGHT -y-40,points[1].y,-10 do
        local v=vec2(x,y2)
        if (x>minx) then minx=x end
        x=math.abs(x+math.random(-10,10))
        table.insert(points,v)
    end
    local leftw=physics.body(CHAIN,true,unpack(points))
    table.insert(walls,leftw)
end

function createTarget()
    target=physics.body(CIRCLE, 25)
    target.x=math.random(minx,maxx)
    target.y=math.random(miny,maxy)
    target.type=STATIC
    target.sensor=true
end

function touched(touch)
    if (touch.state==BEGAN) then
        if (gameOver==nil) then
            physics.gravity(-physics.gravity())
            score = score - 1
        else
            start()
        end
    end
end

function collide(contact)
    if ((contact.bodyA==target or contact.bodyB==target) and contact.state==BEGAN) then
        sound(SOUND_HIT, 22690)
        score = score + 10
        local tmpTarget=target
        targetsLeft = targetsLeft -1
        if (targetsLeft>0) then
            createTarget()
        else
            gameOver=true
            if (score>maxScore) then
                sound(DATA, "ZgBAQAA+QEBAQEBAAAAAAMBouj29s8w+VwBAf0BAQEBAQGZn")
            end
        end
        tmpTarget:destroy()
    end
end


function draw()
    -- Set a dark background color 
    background(0, 0, 0, 255)
    if (gameOver==nil) then
        font("Arial-BoldMT")
        textMode(CORNER)
        fontSize(20)
        fill(12, 0, 255, 255)
        text("Score: "..score.." Targets:"..targetsLeft,0,HEIGHT -20)
        -- This sets the line thickness
        strokeWidth(5)
        
        fill(226, 255, 0, 255)
        ellipse(obj.x,obj.y,2*obj.radius)
        for poly= 1,#walls do
            local points = walls[poly].points
            for j = 1,#points do
                a = points[j]
                b = points[(j % #points)+1]
                line(a.x, a.y, b.x, b.y)
            end
        end
        if (glow>5) then
            sprite("Small World:Heart Glow",target.position.x,target.position.y)
        else
            sprite("Small World:Heart Flat",target.position.x,target.position.y)
        end
        glow = (glow + 1) % 10
    else
        textMode(CENTER)
        fontSize(100)
        fill(12, 0, 255, 255)
        text("Game over",WIDTH/2,HEIGHT/2)
        fontSize(40)
        if (maxScore<score) then
            text("New max score "..score.." !",WIDTH/2,HEIGHT/2 - 70)
        else
            text("Score:"..score.." Max score:"..maxScore,WIDTH/2,HEIGHT/2 - 70)
        end
        text("touch to restart",WIDTH/2,HEIGHT/2 - 140)
    end
end


`

When I copy and paste your code it says there’s an error with the last line

@michael Sorry about that! I forgot to escape the ‘<’. It should work now. Thanks for bringing it to my attention!

Looks awsome and also a great example. I’ll try it when I get a chance. By the way, I noticed you used ```. It works better if you use a ~~~ at the top and bottom of the code. Just a little help.
Thanks!

@Zoyt I tried to find how you guys did put code in, but couldn’t find it. Also seems to save me the trouble of escaping special characters! :slight_smile: Thanks for the tip!

Nice use of the edge chain type. Cool game idea too! I got 75.

Nice work. My max score is 73

Thank you for giving us physics in Codea!

@Herwig:

I pasted your Bouncy code into a project hoping to teach myself a bit about physics and graphics. While I was fooling with it on the subway, a fellow straphanger became completely captivated by it. I explained the game, and explained Codea, and he seemed really intrigued. (Who knows, maybe I made a sale.) Anyway, just wanted to pass on a bit of praise for your game!

Good game! I got 69 so far :slight_smile:

Thanks guys!

Cooooool game this shod be in the app store :slight_smile:

Agreed

@Herwig would it be okay if I use your code in a commercial project? I already changed most of the game. I’ll give you credit.