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