another useless game.

Here’s a game that came to mind while I was working on something else. Since I don’t play games, I didn’t spend a lot of time on this, so it’s bare minimum code . The object is to balance the square and rectangle on the circle for as long as you can. Move the circle left or right to change the balance position. The maximum seconds are saved as global data. The game restarts when the square and rectangle fall off the screen. I managed to get 23 seconds while testing the code.


-- Balance the rectangle and square
-- by dave1707 4/1/14

displayMode(FULLSCREEN)

function setup()
    fontSize(25)
    hs=readGlobalData("balance")
    if hs==nil then
        hs=0
    end
    tm=os.date("*t")
    st=tm.min*60+tm.sec
    dx=0
    
    -- define screen edges
    e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT*5))
    e2=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT*5))
    e3=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
    
    -- define circle
    cir=physics.body(CIRCLE,10)
    cir.type=KINEMATIC
    cir.x=WIDTH/2
    cir.y=400
    
    -- define square
    sqr=physics.body(POLYGON,vec2(-10,-10),vec2(-10,10),vec2(10,10),vec2(10,-10))
    sqr.x=WIDTH/2-100
    sqr.y=HEIGHT-50
      
    -- define rectangle
    rec=physics.body(POLYGON,vec2(-200,0),vec2(-200,10),vec2(200,10),vec2(200,0))
    rec.x=WIDTH/2
    rec.y=500
end


function draw()
    background(40,40,50)
    fill(255)
    text("Balance",WIDTH/2,HEIGHT-50)
    text("Max seconds  "..hs,WIDTH/2,HEIGHT-90)
    tm=os.date("*t")
    et=tm.min*60+tm.sec
    text("Current seconds "..et-st,WIDTH/2,HEIGHT-130)
    text("To balance, slide finger left or right to move blue circle.",WIDTH/2,120)
    text("Game will restart when the rectangle and square are dropped.",WIDTH/2,80)
    
    -- draw blue circle
    fill(0,71,255)
    stroke(0, 71, 255)
    strokeWidth(2)    
    ellipse(cir.x,cir.y,20)  
    
    -- draw red rectangle
    pushMatrix() 
    translate(rec.x,rec.y)
    rotate(rec.angle)
    fill(255,0,0) 
    stroke(255,0,0)
    drawLines(rec)
    popMatrix()
    
    -- draw green square
    pushMatrix()
    translate(sqr.x,sqr.y)
    rotate(sqr.angle)
    fill(0,255,0) 
    stroke(0,255,0)
    drawLines(sqr)
    popMatrix()
    
    -- stop circle from getting close to sides
    if cir.x<80 then
        cir.linearVelocity=vec2(20,0)
        dx=0
        end
    if cir.x>WIDTH-80 then
        cir.linearVelocity=vec2(-20,0)
        dx=0
    end
    
    -- restart game when pieces are off screen
    if rec.y<-500 and sqr.y<-500 then
        tm=os.date("*t")
        et=tm.min*60+tm.sec
        et=et-st
        if et>hs then   -- save max seconds
            saveGlobalData("balance",et)
        end
        restart()   -- restart game
    end
end

function drawLines(pts)
    for z=1,#pts.points do  -- get points
        p1=pts.points[z]
        p2=pts.points[z%#pts.points+1]
        line(p1.x,p1.y,p2.x,p2.y)   -- draw lines
    end
end

function touched(t)
    if dx~=nil then
        if t.state==MOVING then -- move blue circle
            dx=dx+t.deltaX
            cir.linearVelocity=vec2(dx,dy)  
        end
    end
end

@dave1707, isn’t the iPad Air the only one capable of 64 bit math?

@JakAttak I think anything on Codea 2.0 is 64 bit.

Changed the seconds calculation to use os.date instead of os.time. It wasn’t working the way I wanted when used on iPads with 32 bit math routines. That would be pre Codea 2.0 versions.

@JakAttak I get double precision out of print() now on an iPad 3

I like it. I don’t think it is useless, just fun. Nice job on this one, @dave1707 :-bd

Hey may I extend from this code(I am not going to use it to make money or publish to the appstore I just want to add stuff to it for my own use and I want to put some features in my game)

@AnotherBoringUser Any code posted on this forum is for anyone’s use unless otherwise noted in the code. You can do what you want with this code. Just a suggestion, share your code on the forum so others can see it when you’re done.

slapping, why say useless