PicoPicoGameEngine

Quick Links

Source Code

g=ppgraph
sc=ppscreen
B=sc:size()
Bw=B.width
Bh=B.height
pos=pppoint
sqrt=math.sqrt

se=ppsemml[1]

function add(a,b,r)
 local v
 v=pos()
 v.x=a.x+b.x*r
 v.y=a.y+b.y*r
 return v
end

function sub(a,b)
return add(a,b,-1)
end

function nml(p)
local q=pos()
local l
l=p.x*p.x+p.y*p.y
l=sqrt(l)
q.x=p.x/l
q.y=p.y/l
return q
end

function len(a)
local dx=a.x
local dy=a.y
return sqrt(dx*dx+dy*dy)
end

rand=math.random

o=ppoffscreen.new()
o:create(32,32)
o:circle(16,16,15)

o:pos(
 g:layout(
  o:size(),true,true,B))
o.hitmask=1
o.hitlength=10
o.hitcenter={16,16}

function spario()
local r=pprect()
r.x=rand(0,Bw)
r.y=rand(0,Bh)
r.d=pos()
r.d.x=rand(-10,10)/10
r.d.y=rand(-10,10)/10
r.q=rand(4,6)
r.hitmask=1
r.hitlength=16
r.hitcenter={-16,-16}
r.life=8
r.draw=function(s)
if not gameover then
 local d
 d=nml(sub(o+pos(16,16),s))
 s.d=s.d+pos(d)/8
 d=len(s.d)
 if d>s.q then
  s.d=nml(s.d)*s.q
 end
 r:move(s.d)
end
 g:circle(
  s-pos(16,16),16,g.red)
end
return r
end

t={}
for i=1,3 do
local p=spario()
p.y=-16
t[#t+1]=p
end

b={}
function bul(o,d)
local t=pprect(o)
t:move(16+d*2,16)
t.d=pppoint(d,-16)
t.hitmask=1
t.hitlength=1
t.draw=function(s)
if not gameover then
s:move(s.d)
end
g:circle(t,4,g.cyan)
end
b[#b+1]=t
return t
end

pt=pprect()
pt.hitmask=1
pt.hitlength=24
pt.set=function(s)
s.x=rand(0,Bw-32)
s.y=rand(0,Bh-32)
end
pt.draw=function(s)
g:circle(pt,16,g.yellow)
end
pt:set()

ct=0
sc=0
gameover=false

function start()
if not gameover then
 o:drag(pptouch(),B)
end
 o:draw()
 for i=1,#t do
  t[i]:draw()
 end
 local q={}
 for i=1,#b do
  b[i]:draw()
  if b[i].y>0 then
   q[#q+1]=b[i]
  end
 end
 pt:draw()
 pphitcheck(b,t,
   function(a,b)
     a.y=-16
     b.life=b.life-1
     if b.life<=0 then
       sc=sc+1
b.y=-16
       b.life=8
b.d.y=a.d.y
local p=spario()
p.d.y=a.d.y
t[#t+1]=p
p:pos(b)
     end
   end)
 pphitcheck({o},t,
   function(a,b)
if not gameover then
  se:play("o4cgcgcg")
end
     gameover=true
   end)
 pphitcheck({o},{pt},
   function(a,b)
     pt:set()
     sc=sc+1
     ct=ct+1
  se:play("o7c")
if ct>8 then
ct=5
local p=spario()
p.y=-16
t[#t+1]=p
end
   end)
 b=q
 g:print("SCORE "..sc)
 if gameover then
   g:pos(88,160)
   g:print("GAME OVER",g.red)
 end
end