PicoPicoGameEngine

Quick Links

Source Code

g=ppgraph
se=ppsemml[1]

B=ppscreen:size()
Bw=B.width
Bh=B.height

floor=math.floor
rand=math.random
co=coroutine

mv={
  { 0, 0},
  { 1, 0},
  { 0, 1},
  {-1, 0},
  { 0,-1}
}

o=ppoffscreen.new()
o:create(128,32)

o.defchar
=function(o,i,d)
local t
local dx=(i-1)*8
t="0123456789ABCDEF"
local function f(o,dx,s,k)
  local c
  for y=0,7 do
    local r,n
    for i=0,1 do
    n=y*2+1+i
    r=s:sub(n,n)
    r=t:find(r)-1
    for x=0,3 do
      local p,z
      p=(r%2)*255
      r=floor(r/2)
      z=3-x+i*4
      c=o:pixel(z+dx,y)
      c[k]=p
      c.a=255
      o:pixel(z+dx,y,c)
    end
    end
  end
end
f(o,dx,d[1],"r")
f(o,dx,d[2],"g")
f(o,dx,d[3],"b")
end

t={
{"0000000000000000",
 "0000000000000000",
 "007E424242427E00"},
{"3C7EDBDBFFFFFF66",
 "0066424200420000",
 "3C7EDBDBFFC3FF66"},
{"3C7EDBDBFFFFFF66",
 "0066424200420000",
 "00664242007E0000"},
{"00664242007E0000",
 "3C7EDBDBFFC3FF66",
 "3C7EDBDBFFC3FF66"},
{"00664242007E0000",
 "3C7EDBDBFFC3FF66",
 "0066424200420000"}}

for i,d in ipairs(t)
do
  o:defchar(i,d)
end

o:hflip()
o:bind()

m=ppmap.new()
m:addLayer("layer")
m:mapSize(11,19)
m:clip(0,0,11,19)
m:texture(
  o:texture())
m:tileSize(8,8)
m:box(0,0,11,19,1)
m:scale(4,4)

for y=2,16,2 do
for x=2,8,2 do
 m:tile(x,y,1)
end
end

function mon(i,sp)
local clip=function(d)
 if d<=1 then
   d=d+4
 end
 if d>=6 then
   d=d-4
 end
 return d
end
local s=ppsprite.new()
s:texture(
  o:texture())
s:tileSize(8,8)
s:scale(4,4)
s:tile(i)
local r=pprect()
r.s=s
r.draw=function(s)
 s.s:pos(s)
 s.s:move(m:pos())
 s.s:draw()
end
r.sp=sp
r.d=2
r.check=function(s,d)
 local p=s:pos()
 p=p:move(16,16)/32
 p=p+pppoint(mv[d])
 return (m:tile(p)==0)
end
r.getdir=function(s,d)
 local t,q
 t=rand(0,2)-1
 q=d
 d=clip(q+t)
 if s:check(d)
 then
   return d
 end
 t=rand(0,1)
 if t==0 then
  t=-1
 end
 d=clip(q+t)
 if s:check(d)
 then
   return d
 end
 d=clip(q-t)
 if s:check(d)
 then
   return d
 end
 d=q
 if s:check(d)
 then
   return d
 end
 d=clip(d+2)
 return d
end
r.co=co.create(
function(s)
  while true do
  s.d=r:getdir(r.d)
  local l
  l=true
  while true do
  for i=1,s.sp do
    s:move(mv[s.d])
    if  (s.x%32)==0
    and (s.y%32)==0
    then
      l=false
      break
    end
  end
  if not l then
    break
  end
  co.yield()
  end
  end
end)
r.idle=function(s)
 co.resume(s.co,s)
end
return r
end

a=mon(2,1)
a:pos(32,32)

b=mon(3,2)
b:pos(32,32)

c=mon(4,3)
c:pos(32,32)

d=mon(5,4)
d:pos(32,32)

m:move(-16,-16)

function start()
  m:draw()
  a:idle()
  a:draw()
  b:idle()
  b:draw()
  c:idle()
  c:draw()
  d:idle()
  d:draw()
end