A little game I made to test the new Physics Engine.
`
-- Bouncy
-- Herwig Van Marck
function setup()
print("Try hitting the blinking hearts")
print("Tap to flip the direction of gravity")
print("However, every flip costs you 1 point!")
start()
end
function start()
glow=0
targetsLeft=10
maxScore=readProjectData("maxScore") or 0
obj=physics.body(CIRCLE,25)
obj.x=200
obj.y=200
obj.restitution=0.60
obj.sleepingAllowed=false
walls={}
minx=0
maxx=WIDTH
miny=0
maxy=HEIGHT
score=0
createWall()
createTarget()
gameOver=nil
end
function createWall()
local points={}
local x=50
local y=50
local margin=50
minx=x
miny=y
for x2=x,WIDTH-margin,10 do
local v=vec2(x2,y)
y=math.abs(y+math.random(-10,10))
if (y>miny) then miny=y end
table.insert(points,v)
x=x2
end
maxx=x
x=WIDTH - x
for y2=y+10,HEIGHT -margin-30,10 do
local v=vec2(WIDTH - x,y2)
x=math.abs(x+math.random(-10,10))
if(WIDTH -x<maxx) then maxx=WIDTH -x end
table.insert(points,v)
y=y2
end
maxy=y
y=HEIGHT - y
for x2=WIDTH -x-10,margin,-10 do
local v=vec2(x2,HEIGHT - y-30)
y=math.abs(y+math.random(-10,10))
if (HEIGHT -y<maxy) then maxy=HEIGHT -y end
table.insert(points,v)
x=x2
end
for y2=HEIGHT -y-40,points[1].y,-10 do
local v=vec2(x,y2)
if (x>minx) then minx=x end
x=math.abs(x+math.random(-10,10))
table.insert(points,v)
end
local leftw=physics.body(CHAIN,true,unpack(points))
table.insert(walls,leftw)
end
function createTarget()
target=physics.body(CIRCLE, 25)
target.x=math.random(minx,maxx)
target.y=math.random(miny,maxy)
target.type=STATIC
target.sensor=true
end
function touched(touch)
if (touch.state==BEGAN) then
if (gameOver==nil) then
physics.gravity(-physics.gravity())
score = score - 1
else
start()
end
end
end
function collide(contact)
if ((contact.bodyA==target or contact.bodyB==target) and contact.state==BEGAN) then
sound(SOUND_HIT, 22690)
score = score + 10
local tmpTarget=target
targetsLeft = targetsLeft -1
if (targetsLeft>0) then
createTarget()
else
gameOver=true
if (score>maxScore) then
sound(DATA, "ZgBAQAA+QEBAQEBAAAAAAMBouj29s8w+VwBAf0BAQEBAQGZn")
end
end
tmpTarget:destroy()
end
end
function draw()
-- Set a dark background color
background(0, 0, 0, 255)
if (gameOver==nil) then
font("Arial-BoldMT")
textMode(CORNER)
fontSize(20)
fill(12, 0, 255, 255)
text("Score: "..score.." Targets:"..targetsLeft,0,HEIGHT -20)
-- This sets the line thickness
strokeWidth(5)
fill(226, 255, 0, 255)
ellipse(obj.x,obj.y,2*obj.radius)
for poly= 1,#walls do
local points = walls[poly].points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
end
if (glow>5) then
sprite("Small World:Heart Glow",target.position.x,target.position.y)
else
sprite("Small World:Heart Flat",target.position.x,target.position.y)
end
glow = (glow + 1) % 10
else
textMode(CENTER)
fontSize(100)
fill(12, 0, 255, 255)
text("Game over",WIDTH/2,HEIGHT/2)
fontSize(40)
if (maxScore<score) then
text("New max score "..score.." !",WIDTH/2,HEIGHT/2 - 70)
else
text("Score:"..score.." Max score:"..maxScore,WIDTH/2,HEIGHT/2 - 70)
end
text("touch to restart",WIDTH/2,HEIGHT/2 - 140)
end
end
`