One for the kids. One of these balls deletes any other ball it touches. Spot which ball it is, and touch it. You get a point for each of the other balls you save from being deleted.
displayMode(FULLSCREEN)
function setup()
physics.gravity(0,0)
start()
end
function start()
balls={}
if high==nil then high,score,total=0,0,0 else score=0 end
for i=1,100 do --create some random balls
local s=math.random(20,40)
local c=color(math.random(0,255),math.random(0,255),math.random(0,255))
local b=physics.body(CIRCLE,s/2)
b.x,b.y=math.random(100,WIDTH-100),math.random(100,HEIGHT-100)
b.linearVelocity=vec2(math.random(-300,300),math.random(-300,300))
b.restitution=1
b.info=i
balls[i]={b=b,c=c,s=s,i=i}
end
killer=balls[math.random(1,100)]
killer.b.info="k"
if edges then return end
edges={physics.body(EDGE,vec2(1,1),vec2(WIDTH,1)),physics.body(EDGE,vec2(1,1),vec2(1,HEIGHT)),
physics.body(EDGE,vec2(1,HEIGHT),vec2(WIDTH,HEIGHT)),physics.body(EDGE,vec2(WIDTH,1),vec2(WIDTH,HEIGHT))}
fill(43, 48, 138, 255) fontSize=24 textMode(CORNER)
end
function draw()
background(220)
for i,b in pairs(balls) do fill(b.c) ellipse(b.b.x,b.b.y,b.s) end
fill(0) text("High score "..high,50,HEIGHT-100)
text("Total score "..total,50,HEIGHT-125) text("Time "..math.floor(ElapsedTime),50,HEIGHT-150)
end
function collide(c)
if ElapsedTime<2 or c.bodyA==nil or c.bodyB==nil then return end
if c.bodyA.info=="k" then kill(c.bodyB.info) elseif c.bodyB.info=="k" then kill(c.bodyA.info) end
end
function kill(u)
for i,b in pairs(balls) do
if b.i==u then b.b:destroy() table.remove(balls,i) break end
end
end
function touched(t)
if t.state==BEGAN then
if vec2(t.x,t.y):dist(vec2(killer.b.x,killer.b.y))<killer.s then
score=table.maxn(balls)-1 total=total+score if score>high then high=score end
for i=1,#balls do balls[i].b:destroy() end
collectgarbage()
start()
end
end
end