hive.saysi.org

 

HIVE PICO-8 Tutorial

§7

7a: array tables, modulus & stretching sprites

eggcarton.p8

egg=1

eggcarton={1,2,3,4,"egg",{"whoa"}}

print(eggcarton[6][1],0) --prints "woah" in white

flipbook.p8

hrozgo={1,2,0,3,4,0}
sned={12,13,14,13}
wulf={32,34,36,48,50,52}

mydude_face=1
mydude_h=1
mydude_w=1
mydude_x=20
mydude_y=95
mydude_scale=1

function _draw()
  cls(3)
  sspr(mydude_face%16*8,mydude_face\16*8,
    mydude_w*8,mydude_h*8,
    mydude_x,mydude_y-mydude_scale*8*mydude_h,
    mydude_scale*8*mydude_w,
    mydude_scale*8*mydude_h
    )
end

function _update()
  if btn(➡️) then mydude_x+=.5*mydude_scale end --shift-r for character
  if btn(⬅️) then mydude_x-=.5*mydude_scale end --shift-l for character
  if btn(⬆️) then mydude_scale+=.1 end --shift-u for character
  if btn(⬇️) then mydude_scale-=.1 end --shift-d for character
  mydude_scale=mid(.1,12,mydude_scale)
end



Merit Badge Challenge 7a ★★

PIXEL BUDDY ANIMATION WORKSHOP



Merit Badge Challenge 7a+ ★★

PIXEL BUDDY Draw a character animation cycle. Print your character enlarged on the screen. Implement sspr with a modulus such that you pass it a sprite index and it will calculate spritesheet x and y it needs, just like in the video.





7b: drawing scrolling sine waves with for & time

flipbook.p8


hrozgo={1,2,0,3,4,0}
sned={12,13,14,13}
wulf={32,34,36,48,50,52}

mydude_face=1
mydude_h=1
mydude_w=1
mydude_x=20
mydude_y=95
mydude_scale=1

function _draw()
  cls(3)

  for wob=0,128 do
    local hill= sin(wob*.02+time()*.1)*5
    line(wob,128,wob,mydude_y-10-40/mydude_scale+hill,2)
  end

  sspr(mydude_face%16*8,mydude_face\16*8,
    mydude_w*8,mydude_h*8,
    mydude_x,mydude_y-mydude_scale*8*mydude_h,
    mydude_scale*8*mydude_w,
    mydude_scale*8*mydude_h
    )
end

function _update()
  if btn(➡️) then mydude_x+=.5*mydude_scale end --shift-r for character
  if btn(⬅️) then mydude_x-=.5*mydude_scale end --shift-l for character
  if btn(⬆️) then mydude_scale+=.1 end --shift-u for character
  if btn(⬇️) then mydude_scale-=.1 end --shift-d for character
  mydude_scale=mid(.1,12,mydude_scale)
end



Merit Badge Challenge 7b ★★★

SECONDARY ANIMATION WITH MATHS Attach timed sin/cos wave to a number variable and display it in a fun way alongside your earlier character.





7c: flipbook animation with modulus & time

flipbook.p8


--tab 0

function wrong_flipbook(t) --not very flipbooky
  return rnd(t)
end

function still_wrong_flipbook(t) --only good for hrozgo, not sned nor wulf
  local index=tim
  print(index,10,10,8)
  return t[index]
end

framerate=4
function flipbook_good_enough(t) --good, but not as graceful; requires a counter
  local index=(tim/framerate)%#t+1
  print(index,10,10,8)
  return t[index]
end

fr=10
function flipbook(a)
  local index=flr(t()*fr)%#a+1
  --print(index,10,10,8)
  return a[index]
end

--tab 1

hrozgo={1,2,0,3,4,0}
sned={12,13,14,13}
wulf={32,34,36,48,50,52}

mydude_face=1
mydude_h=1
mydude_w=1
mydude_x=20
mydude_y=95
mydude_scale=1

tim=0

function _draw()
  cls(3)

  mydude_face=flipbook(hrozgo)

  for wob=0,128 do
    local hill= sin(wob*.02+time()*.1)*5
    line(wob,128,wob,mydude_y-10-40/mydude_scale+hill,2)
  end

  sspr(mydude_face%16*8,mydude_face\16*8,
    mydude_w*8,mydude_h*8,
    mydude_x,mydude_y-mydude_scale*8*mydude_h,
    mydude_scale*8*mydude_w,
    mydude_scale*8*mydude_h
    )
end

function _update()
  --tim+=1

  if btn(➡️) then mydude_x+=.5*mydude_scale end --shift-r for character
  if btn(⬅️) then mydude_x-=.5*mydude_scale end --shift-l for character
  if btn(⬆️) then mydude_scale+=.1 end --shift-u for character
  if btn(⬇️) then mydude_scale-=.1 end --shift-d for character
  mydude_scale=mid(.1,12,mydude_scale)
end



Merit Badge Challenge 7c ★★★

THIEVERY & ART BREAK! Describe what each part of the last flipbook functrion does. Then steal the flipbook function! Just stone cold cut n paste it into your code! Make your pixel buddy move! Draw some friends for your pixel buddy.





7d: building tools & objectifying dudes with tables

flipbook.p8


--tab 0

fr=10
function flipbook(a)
  local index=flr(t()*fr)%#a+1
  --print(index,10,10,8)
  return a[index]
end

--tab 1

hrozgo={1,2,0,3,4,0}
sned={12,13,14,13}
wulf={32,34,36,48,50,52}

dudes={}

function dude_dispenser(face,x,y)
  local a={
    face=face,
    h=1,
    w=1,
    x=x,
    y=y,
    scale=1
  }
  add(dudes, a)
  return a
end

yetanotherone=dude_dispenser(666,40,95)
yetanotherone.w=2

mydude = dude_dispenser(1,20,95)
anotherdude= dude_dispenser(12,18,97)

--tim=0

function _draw()
  cls(3)

  mydude.face=flipbook(hrozgo)
  anotherdude.face=flipbook(sned)
  yetanotherone.face=flipbook(wulf)

  for wob=0,128 do
    local hill= sin(wob*.02+time()*.1)*5
    line(wob,128,wob,mydude.y-10-40/mydude.scale+hill,2)
  end

  foreach(dudes,draw_dude)
end

function draw_dude(dude)
  sspr(dude.face%16*8,dude.face\16*8,
    dude.w*8,dude.h*8,
    dude.x,dude.y-dude.scale*8*dude.h,
    dude.scale*8*dude.w,
    dude.scale*8*dude.h
    )
end

function _update()
  --tim+=1

  if btn(➡️) then mydude.x+=.5*mydude.scale end --shift-r for character
  if btn(⬅️) then mydude.x-=.5*mydude.scale end --shift-l for character
  if btn(⬆️) then mydude.scale+=.1 end --shift-u for character
  if btn(⬇️) then mydude.scale-=.1 end --shift-d for character
  mydude.scale=mid(.1,12,mydude.scale)
end



Merit Badge Challenge 7d ★★★

STRUCTURED PIXEL BUDDY WORKSHOP propose your "object" table to the HIVELORDS (see 7d+)



Merit Badge Challenge 7d+ ★★★★

STRUCTURED PIXEL BUDDY Update your character code to work with "object" tables. Now you're cooking. Put all your pixel buddies in a scene together. Define a specific mood to the scene, such that some kind of relationship between characters or narrative is implied.





7e: adding complexity to your tools

flipbook.p8

--tab 0

fr=10
function flipbook(a)
  local index=flr(t()*fr)%#a+1
  --print(index,10,10,8)
  return a[index]
end

--tab 1

hrozgo={1,2,0,3,4,0}
sned={12,13,14,13}
wulf={32,34,36,48,50,52}

dudes={}

function dude_dispenser(face,x,y)
  local a={
    face=face,
    h=1,
    w=1,
    x=x,
    y=y,
    scale=1
  }
  add(dudes, a)
  return a
end

--yetanotherone=dude_dispenser(666,40,95)
--yetanotherone.w=2

mydude = dude_dispenser(1,20,95)
--anotherdude= dude_dispenser(12,18,97)

shadow=1
ground=2
sky=3

--tim=0

function _draw()
  cls(sky)

  mydude.face=flipbook(hrozgo)
  --anotherdude.face=flipbook(sned)
  --yetanotherone.face=flipbook(wulf)

  for wob=0,128 do
    local hill= sin(wob*.02+time()*.1)*5
    line(wob,128,wob,mydude.y-10-40/mydude.scale+hill,ground)
  end

  foreach(dudes,draw_shad)
  foreach(dudes,draw_dude)
end

function draw_shad(dude)
  fillp(▒) --shift-b for character
  ovalfill(dude.x+.2*dude.scale,dude.y-dude.scale*8*0.08,
    dude.x+dude.scale*dude.w*8*.8,dude.y+dude.scale*8*0.08,shadow)
  fillp()
end

function draw_dude(dude)

  sspr(dude.face%16*8,dude.face\16*8,
    dude.w*8,dude.h*8,
    dude.x,dude.y-dude.scale*8*dude.h,
    dude.scale*8*dude.w,
    dude.scale*8*dude.h
    )
end

function _update()
  --tim+=1

  if btn(➡️) then mydude.x+=.5*mydude.scale end --shift-r for character
  if btn(⬅️) then mydude.x-=.5*mydude.scale end --shift-l for character
  if btn(⬆️) then mydude.scale+=.1 end --shift-u for character
  if btn(⬇️) then mydude.scale-=.1 end --shift-d for character
  mydude.scale=mid(.1,12,mydude.scale)
end



Merit Badge Challenge 7e ★★★★

PARTICLES WORKSHOP



Merit Badge Challenge 7e+ ★★★★★

PARTICLES In addition to drawing a shadow under each character, what other things could you do with the data you're keeping track of? Understanding the programming pattern of dispenser+object+set for the flipbooking dudes we're drawing on the screen, what other kinds of things can we simulate?





7f: state machines

flipbook.p8

--tab 0; flipbooks

fr=10
function flipbook(a)
  local index=(t()*fr)%#a+1
  --print(index,10,10,8)
  return a[index]
end

--tab 1; dude facts

hrozgo={1,2,0,3,4,0}
sned={12,13,14,13}
wulf={32,34,36,48,50,52}

dudes={}

function dude_dispenser(face,x,y)
  local a={
    face=face,
    h=1,
    w=1,
    x=x,
    y=y,
    scale=1
  }
  add(dudes, a)
  return a
end

function draw_shad(dude)
  fillp(▒) --shift-b for character
  ovalfill(dude.x+.2*dude.scale,dude.y-dude.scale*8*0.08,
    dude.x+dude.scale*dude.w*8*.8,dude.y+dude.scale*8*0.08,shadow)
  fillp()
end

function draw_dude(dude)

  sspr(dude.face%16*8,dude.face\16*8,
    dude.w*8,dude.h*8,
    dude.x,dude.y-dude.scale*8*dude.h,
    dude.scale*8*dude.w,
    dude.scale*8*dude.h
    )
end

--tab 2

--yetanotherone=dude_dispenser(666,40,95)
--yetanotherone.w=2

mydude = dude_dispenser(1,20,95)
mydude.w =2
--anotherdude= dude_dispenser(12,18,97)

shadow=1
ground=2
sky=3

printme=[[
once upon a time wulfgang
was compelled to go adventuring...
]]

--tim=0

state=2
states_u = {
function() --1

  if btn(➡️) then mydude.x+=.5*mydude.scale end --shift-r for character
  if btn(⬅️) then mydude.x-=.5*mydude.scale end --shift-l for character
  if btn(⬆️) then mydude.scale+=.1 end --shift-u for character
  if btn(⬇️) then mydude.scale-=.1 end --shift-d for character
  mydude.scale=mid(.1,12,mydude.scale)
end,function() --2
  if btn()>0 then state=1 end
end,function() --3
end,function() --4
end,function() --5
end,function() --6
end
}

states_d = {
function() --1
  cls(sky)
  mydude.face=flipbook(wulf)
  --anotherdude.face=flipbook(sned)
  --yetanotherone.face=flipbook(wulf)

  for wob=0,128 do
    local hill= sin(wob*.02+time()*.1)*5
    line(wob,128,wob,mydude.y-10-40/mydude.scale+hill,ground)
  end

  foreach(dudes,draw_shad)
  foreach(dudes,draw_dude)
end,function() --2
  cls(0)
  print(printme,4,50,10)
end,function() --3
end,function() --4
end,function() --5
end,function() --6
end
}

function _draw()
  states_d[state]()
end

function _update()
  states_u[state]()
end



Merit Badge Challenge 7f ★★★★★

ARTIFICIAL INTELLIGENCE What if each of your dudes was a state machine? Simulate a bunch of creatures that do their thing but also react to player interaction. Good luck!







Merit Badge Challenge † ★★★★★★

INTERSECTIONS Combine concepts covered in previous 19 merit badges in a novel way. Loteria? Group project?



Merit Badge Challenge ∞ ★★★★★★

HORIZONS Go beyond: Hotspots. Tlines & fake 3D. Physics. Coroutines.