Here’s another program anyone can take and improve on if they want. Can you tap the screen and push the yellow rectangle to the top of the screen using the balls. The program is trying to push the rectangle to the bottom of the screen. Whoever does it first, wins. Your ball will appear side to side where you touch but starting at the bottom of the screen. You can only have 3 balls on the screen at any one time.
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT_ANY)
function setup()
rectMode(CENTER)
fontSize(50)
size=25
count=0
str=""
tab1={}
tab2={}
e1=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT))
e2=physics.body(EDGE,vec2(WIDTH,0),vec2(WIDTH,HEIGHT))
e3=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
e3.info="bottom"
e4=physics.body(EDGE,vec2(0,HEIGHT),vec2(WIDTH,HEIGHT))
e4.info="top"
b=physics.body(POLYGON,vec2(-size*2,-size),vec2(size*2,-size),
vec2(size*2,size),vec2(-size*2,size))
b.x=WIDTH/2
b.y=HEIGHT/2
b.gravityScale=0
b.info="box"
end
function draw()
background(220, 170, 150, 255)
if str~="" then
fill(255)
text(str,WIDTH/2,HEIGHT/2)
count=count+1
if count>120 then
text("Tap screen to play again.",WIDTH/2,HEIGHT/2-100)
end
else
pushMatrix()
translate(b.x,b.y)
rotate(b.angle)
fill(255,255,0)
noStroke()
rect(0,0,size*4,size*2)
line(-size*2,-size,size*2,-size)
line(size*2,-size,size*2,size)
line(size*2,size,-size*2,size)
line(-size*2,size,-size*2,-size)
popMatrix()
fill(255,0,0)
for a,b in pairs(tab1) do
ellipse(b.x,b.y,40)
if b.info=="del" or b.y<0 then
b:destroy()
table.remove(tab1,a)
end
end
fill(0,255,0)
for a,b in pairs(tab2) do
ellipse(b.x,b.y,40)
if b.info=="del" or b.y>HEIGHT then
b:destroy()
table.remove(tab2,a)
end
end
count = count + 1
if count>20 then
count=0
create()
end
end
end
function create()
count=0
b1=physics.body(CIRCLE,20)
b1.x=b.x+math.random(-size*2,size*2)
if b1.x<=size then
b1.x=size
end
if b1.x>=WIDTH-size then
b1.x=WIDTH-size
end
b1.y=HEIGHT-30
b1.gravityScale=0
b1.info="ball"
b1.linearVelocity=vec2(0,-500)
table.insert(tab1,b1)
end
function touched(t)
if t.state==BEGAN then
if str~="" and count>150 then
restart()
end
if #tab2<=2 then
b2=physics.body(CIRCLE,20)
b2.x=t.x
if b2.x<=size then
b2.x=size
end
if b2.x>=WIDTH-size then
b2.x=WIDTH-size
end
b2.y=30
b2.gravityScale=0
b2.info="ball"
b2.linearVelocity=vec2(0,500)
table.insert(tab2,b2)
end
end
end
function collide(c)
if c.state==BEGAN then
if c.bodyA.info=="ball" then c.bodyA.info="del" end
if c.bodyB.info=="ball" then c.bodyB.info="del" end
if c.bodyA.info=="box" and c.bodyB.info=="bottom" then
str="You lose"
count=0
end
if c.bodyA.info=="bottom" and c.bodyB.info=="box" then
str="You lose"
count=0
end
if c.bodyA.info=="box" and c.bodyB.info=="top" then
str="You win"
count=0
end
if c.bodyA.info=="top" and c.bodyB.info=="box" then
str="You win"
count=0
end
end
end