PicoPicoGameEngine

Quick Links

Source Code

g=ppgraph
floor=math.floor
B=ppscreen:size()
Bw=B.width
Bh=B.height

tex=pptex:load("main.png")

function obj(x,y)
local r
r=pprect()
r.t=1
r.n={}
r.x=x
r.y=y
r.s=0.1
r.init=function(s)
end
r.idle=function(s)
end
r.draw=function(s)
 s.t=s.t+s.s
 if s.t>=#s.n+1 then
   s.t=1
 end
 g:put(s.x,s.y,
   s.n[floor(s.t)],tex)
end
r.copy=function(s,x,y)
 local t
 t=obj(x,y)
 t.n=s.n
 t.s=s.s
 t.init=s.init
 t.idle=s.idle
 t.draw=s.draw
 t:init()
 return t
end
return r
end

function drawbg()
 g:fill(B,g.cyan)
 g:fill(0,200,Bw,Bh-200,g.green)
 g:fill(0,400,Bw,Bh-400,{0,128,0,255})
end

c=obj(0,0)
c.n={2,3}
c.init=function(s)
 s.h=0
 s.dy=0
end
c.draw=function(s)
 s.t=s.t+s.s
 if s.t>=#s.n+1 then
   s.t=1
 end
 s.h=s.h+s.dy
 s.dy=s.dy+0.4
 if s.h>0 then
   s.dy=0
   s.h=0
 end
 g:alpha(128)
 g:put(s.x+cur.x,
       s.y+cur.y+16,7,tex)
 g:alpha(255)
 g:put(s.x+cur.x,
       s.y+cur.y+s.h,
       s.n[floor(s.t)],tex)
end

cur=ppsprite.new()
jump=ppbutton("JUMP")
jump:pos(120,400)

man={}
man[#man+1]=c:copy(160-16,240)
man[#man+1]=c:copy(160-48,240-32)
man[#man+1]=c:copy(160+48,240+32)

c=obj(0,0)
c.n={4,5}
c.s=0.025
c.idle=function(s)
 s:move(0.2,0)
 if s.x>Bw then
   s.x=-32
 end
end

cl={}
cl[#cl+1]=c:copy(60,80)
cl[#cl+1]=c:copy(160,50)
cl[#cl+1]=c:copy(240,60)
cl[#cl+1]=c:copy(280,40)

c=obj(0,0)
c.n={6}
c.s=0.025
c.init=function(s)
 s.h=0
 s.dy=-8
 s.r=0
end
c.idle=function(s)
 s:move(1,0)
 if s.x>Bw then
   s.x=-32
 end
 s.h=s.h+s.dy
 s.dy=s.dy+0.4
 if s.h>0 then
   s.dy=-8
 end
 s.r=s.r+0.05
 if s.r>2*math.pi then
   s.r=s.r-2*math.pi
 end
end
c.draw=function(s)
g:alpha(128)
g:put(s.x,s.y+16,7,tex)
g:alpha(255)
g:pivot(s.x+16,s.y+s.h+16)
g:rotate(s.r)
g:put(s.x,s.y+s.h,
       s.n[1],tex)
g:rotate(0)
end

rk={}
rk[#rk+1]=c:copy(0,200)
rk[#rk+1]=c:copy(100,240)
rk[#rk+1]=c:copy(160,200)
rk[#rk+1]=c:copy(240,240)
rk[#rk+1]=c:copy( 80,300)
rk[#rk+1]=c:copy(140,280)

c=obj(0,0)
c.n={8}
c.s=0.025
c.idle=function(s)
 s:move(1,0)
 if s.x>Bw then
   s.x=-32
 end
end

y=180
rk[#rk+1]=c:copy(0-50,y)
rk[#rk+1]=c:copy(50,y)
rk[#rk+1]=c:copy(110,y)
rk[#rk+1]=c:copy(190,y)
rk[#rk+1]=c:copy( 30,y)
rk[#rk+1]=c:copy(90,y)

function makeot(d)
local r={}
r.d=d
r.reset=function(s)
s.ot={}
for i=1,s.d do
local t={}
s.ot[i]=t
end
for i=1,s.d-1 do
s.ot[i].next=s.ot[i+1]
end
end
r.add=function(s,a,ay)
local y
y=math.floor(ay)
if y<=1 then
 y=1
end
if y>=s.d then
 y=s.d
end
a.next=s.ot[y].next
s.ot[y].next=a
end
r.draw=function(s)
local n
n=s.ot[1]
while true do
if n.draw then
 n:draw()
end
n=n.next
if n==nil then
  break
end
end
end
return r
end
ot=makeot(Bh)

function start()
 cur:drag(pptouch())
 drawbg()
 ot:reset()
 for i,o in ipairs(cl)
 do
   o:idle()
   ot:add(o,o.y)
 end
 for i,o in ipairs(rk)
 do
   o:idle()
   ot:add(o,o.y)
 end
 for i,o in ipairs(man)
 do
   o:idle()
   ot:add(o,o.y+cur.y)
 end
 ot:draw()
 if jump:idle(pptouch())
 then
   for i,o in ipairs(man)
   do
     o.dy=-8
   end
 end
 jump:draw()
end