PicoPicoGameEngine

Quick Links

Source Code

g=ppgraph
B=ppscreen:size()
M=pprect(B)
M:inset(-16,-16)
rand=math.random
Bw=B.width-16
Bh=B.height-16
se=ppsemml[1]
o={}

score=0

function calcdir(s)
  local q
  local d=pppoint()
  q=rand(0,360)
  q=q*math.pi/180
  d.x=math.sin(q)
  d.y=math.cos(q)
  local sp=rand(1,4)
  return d*sp/16
end

function obj(x,y)
  local t
  t=pprect(0,0,16,16)
  t.d=calcdir(t)
  t:pos(x,y)
  t.t=false
  t.idle=function(s)
    if s.f>0 then
      s.f=s.f-1
    end
    s:move(s.d)
    if s.x>Bw then
      s.d.x=-s.d.x
    end
    if s.y>Bh then
      s.d.y=-s.d.y
    end
    if s.x<0 then
      s.d.x=-s.d.x
    end
    if s.y<0 then
      s.d.y=-s.d.y
    end
    if s.t then
      g:fill(s,g.yellow)
    elseif s.f>0 then
      g:box(s,g.blue)
    else
      g:box(s)
    end
  end
  t.hitmask=1
  t.hitrect=t:size()
  t.f=0
  o[#o+1]=t
  return t
end

for x=0,Bw,16 do
  obj(x,0)
  obj(x,Bh)
end
for y=16,Bh-16,16 do
  obj(0,y)
  obj(Bw,y)
end
o[rand(1,#o)].t=true

m=ppoffscreen.new()
m:create(16,16)
m:fill(m:size(),g.cyan)
p=g:layout(
 m:size(),true,true,B)
m:pos(p)
m.hitmask=1
m.hitrect=m:size()

miss=false

hitfunc=function(a,b)
  if a.t then
    a.t=false
    o[rand(1,#o)].t=true
    score=score+1
    se:play("o7c")
    a.f=120
  else
    if a.f>0 then
      return
    end
    miss=true
    se:play("o5cgcgcg")
  end
end

function start()
  for i,s in ipairs(o)
  do
    s:idle()
  end
  if miss==false then
  pphitcheck(o,{m},
    hitfunc)
  m:drag(pptouch(),M)
  m:draw()
  else
    g:pos(
    (B.width-9*16)/2,
    (B.height-16)/2)
    g:print("GAME OVER",
     g.red)
  end
  g:pos(0,0)
  g:print(score)
end