Howdy, Stranger!

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

My code for a bouncing ball game.

edited January 2015 in Code Sharing Posts: 3
function setup()
    displayMode(FULLSCREEN)
    ball = physics.body(CIRCLE, 25)
    ball.type = STATIC
    ball.x = 100
    ball.y = 300

    rock = vec2((WIDTH/2) - 50, 200)

    flapbutton = vec2(WIDTH - 110, 100)

    flapx = 100
    flapy = 400

    GravScale = -1000

    flag = vec2(900, 235)

    level = 1

    GameRun = true
end

function draw()
    physics.gravity(0, GravScale)
    background(114, 199, 242, 255)    

    pushStyle()
    fill(63, 137, 76, 255)
    rect(-5, -5, WIDTH + 10, 207)
    popStyle()

    pushStyle()
    fill(172, 172, 172, 255)
    rect(WIDTH/2 -75, 15, 150, 100)
    popStyle()

    pushStyle()
    fill(0, 0, 0, 255)
    font("Futura-CondensedExtraBold")
    fontSize(40)
    text("LEVEL", WIDTH/2, 40)
    text(level, WIDTH/2, 90)
    popStyle()

    pushStyle()
    fill(255, 255, 255, 255)
    ellipse(ball.x, ball.y, 50)
    popStyle()

    sprite("Dropbox:#Flag", flag.x, flag.y, 50)

    sprite("Dropbox:#FlapButton", flapbutton.x, flapbutton.y, 150)

    ground = physics.body(EDGE, vec2(0,194), vec2(WIDTH,194))

    if ball.x >= flag.x - 50 and ball.x <= flag.x + 50 and ball.y >= flag.y -60 and ball.y <= flag.y  + 60 then
        GravScale = 0
        ball.type = STATIC
        GameRun = false
    end

    if ball.x >= rock.x -25 and ball.x <= rock.x + 125 and ball.y >= rock.y - 25 and ball.y <= rock.y + 425 then
        GravScale = 0
        ball.type = STATIC
        GameRun = false
    end

    if level == 1 then
        rect(rock.x,rock.y, 100, 400)
    end
end

function flapBall()
    ball.type = DYNAMIC
    ball.linearVelocity = vec2(flapx,flapy)
end


function touched(touch)
    if touch.state == BEGAN and GameRun == true then
        if touch.x >= flapbutton.x -75 and touch.x <= flapbutton.x + 75 and touch.y >= flapbutton.y - 75 and touch.y <= flapbutton.y + 75 then
            print("flap")
            flapBall()
        end
    end
end

I am going to put more levels and a start/game over screen eventually.
Any comments are appreciated

8-} 8-} 8-} 8-} 8-} 8-}

Comments

  • dave1707dave1707 Mod
    Posts: 7,650

    @JetMagri It's bad coding to create a physics body in draw. You're creating ground 60 times per second. It should be created once in setup().

  • Posts: 136

    Is this more from the class? I wish some of my younger tech classes had thought something more like codea instead of scratch and alice.

Sign In or Register to comment.