starter game11

Here’s another starter game. The object is to catch the positive balls and let the negative balls fall off the screen. If you catch a negative ball or a positive ball falls off the screen, your score goes down. Your score goes up when you catch a positive ball. The screen is green as long as you score matches the current high score. The screen turns red when you start to lose points. You win when you reach a score of 1000 or more. The game starts with a ball dropping every 3 seconds. As your score goes up, the balls will drop at a faster rate. A ball every 1/2 second is the fastest. To move the bucket, just slide your finger anywhere left or right. This is for anyone who wants to add to it or just see how things are done.


displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)

function setup()
    btab={} -- ball table
    createBall()
    dtab={} -- dots table
    createDots()
    speed=3
    time={3,2.5,2,1.5,1,.5}
    currHS=0
    score=0
    px=300
    et=0
end

function draw()
    -- determine background color
    if score<currHS then
        background(100,0,0)
    else
        background(0,100,0)
    end
    fill(255)
    fontSize(30)
    text("Score  "..score.."           Curr High  "..currHS,WIDTH/2,60)
    -- draw dots
    for a,b in pairs(dtab) do
        ellipse(b.x,b.y,4)
    end
    -- draw balls and values
    fontSize(17)
    for a,b in pairs(btab) do
        fill(255)
        ellipse(b.x,b.y,36)
        fill(255,0,0)
        text(b.info,b.x,b.y)
        if b.x<0 then    -- off left screen, enter right screer
            b.x=WIDTH
        end
        if b.x>WIDTH then    -- off right screen, enter left screen
            b.x=0
        end
    end
    if score>=1000 then
        fontSize(80)
        text("You won",WIDTH/2,HEIGHT-300)
    else
        fontSize(30)
        text("New ball every "..speed.." seconds",WIDTH/2,HEIGHT-30)
        -- create a new ball
        if ElapsedTime-et>speed then
            createBall()
            et=ElapsedTime
        end
        -- check for ball caught or off screen
        for a,b in pairs(btab) do
            if b.y>70 and b.y<130 and b.x>px-50 and b.x<px+50 then
                score = score + b.info
                destroyBall(a,b)    -- remove ball
                if b.info>0 then
                    sound(SOUND_JUMP, 46230)    -- positive ball
                    else
                    sound(SOUND_JUMP, 21150)    -- negative ball
                end
                elseif b.y<0 then    -- off bottom of screen
                if b.info>0 then
                    score = score - b.info  -- positive ball
                    sound(SOUND_JUMP, 21150)
                end
                destroyBall(a,b)    -- remove ball
            end
        end 
        -- draw bucket
        sprite("Cargo Bot:Level Select Frame",px,110,110,50)
        -- save current high score
        if score>currHS then
            currHS=score
        end
        -- determine ball drop delay based on score
        s=math.floor(score/100)+1
        if s<1 then
            s=1
        end
        if s>6 then
            s=6 
        end 
        speed=time[s]   
    end   
end

function touched(t)
    px=px+t.deltaX*2    -- move bucket left or right
    if px<0 then    -- check if off left
        px=0
    end
    if px>WIDTH then    -- check if off right
        px=WIDTH
    end
end

function createBall()   -- add ball to table
    c=physics.body(CIRCLE,18)   
    c.x=math.random(WIDTH)
    c.y=1100  
    c.restitution=.8   
    c.info=math.random(-50,50)
    table.insert(btab,c) 
end

function destroyBall(a,b)   -- remove ball from table
    b:destroy()
    b=nil
    table.remove(btab,a)
end

function createDots()   -- add dots to table
    for x=0,16 do
        for y=1,9 do
            d=physics.body(CIRCLE,2)
            d.x=5+x*50
            d.y=1000-y*80 
            d.gravityScale=0  
            d.type=STATIC
            table.insert(dtab,d)
        end
    end 
    for x=0,15 do
        for y=1,8 do
            d=physics.body(CIRCLE,2)
            d.x=5+x*50+20
            d.y=960-y*80 
            d.gravityScale=0  
            d.type=STATIC
            table.insert(dtab,d)
        end
    end  
end
    

Really cool game. I could see someone turn it into an education app. Good luck and good job!

@dave1707,
Another excellent starter game! As always, small, but fully working.

One minor suggestion for next time:
If you share the source using the copy-project feature of Codea (long press on the project icon, then choose “Copy” from the menu), then it is also a little bit eaiser to try out your program: the reader can copy the code from here, then long press “Add Project” button and choose “Paste into project” from the menu. This creates a new project, with all the tabs from your project already populated and ready to run.

@juce - what you say is correct, but I’m puzzled why you mention it for this app, which only has one tab and can be copied normally. (Also, I’m sure dave1707 would use the long press if he needed to, as he is one of the longest users of Codea and knows all the tricks - it’s just that he likes to use short projects to help new users).

@Ignatz,
yeah, in retrospect, i’m not sure if my comment was really necessary. Re-reading it now - it doesn’t quite come across the way it was intended. Hopefully, dave1707 didn’t hate it.

The only reason i wanted to mention that particular way of sharing the source, is because it makes it easiest to try the code. If the code in the post is in full “project format” - with the tab markers (like “–# Main”) - and so for, even if it is just a single tab, it’s so easy to paste it into a new project. Otherwise, you need to make the new project, select the default placeholder code, paste the copied code over it. Of course, it’s not a big deal, at all. Tiny little conveniences, that’s all :slight_smile:

@juce no, I didn’t hate it. It does make it easier, but I don’t think new users know about the “Paste into project” function. But then, if it had a tab marker, it could be saved either way. I just never thought about it. Of my 300+ projects, only about 3 of them have more than 1 tab, so I never think about adding the tab markers.

@dave1707, you are right. I didn’t think of the relative obscurity of the “Paste into project” feature. But it’s totally true. Ironically, i had been using Codea for a year or so, and didn’t know about it. (Discovered it by accident)