Krystman

Pico-8 Hero - Breakout - End of Ep. 17

Mar 5th, 2017
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.51 KB | None | 0 0
  1. --goals
  2. -- 4. levels
  3.  
  4. --    level finished detection
  5. --    next level screen
  6.  
  7. -- 5. diffrent bricks
  8. -- 6. (powerups)
  9. -- 7. juicyness
  10. --     arrow anim
  11. --     text blinking
  12. --     particles
  13. --     screenshake
  14. -- 8. high score
  15.  
  16. function _init()
  17.  cls()
  18.  mode="start"
  19.  level=""
  20.  debug=""
  21.  levelnum = 1
  22.  levels={}
  23.  levels[1] = "x5b"
  24.  levels[2] = "x4b"
  25.  --levels[2] = "bxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxb"
  26. end
  27.  
  28. function _update60()
  29.  if mode=="game" then
  30.   update_game()
  31.  elseif mode=="start" then
  32.   update_start()
  33.  elseif mode=="gameover" then
  34.   update_gameover()
  35.  elseif mode=="levelover" then
  36.   update_levelover()
  37.  end
  38. end
  39.  
  40. function update_start()
  41.  if btnp(4) then
  42.   startgame()
  43.  end
  44. end
  45.  
  46. function startgame()
  47.  mode="game"
  48.  ball_r=2
  49.  ball_dr=0.5
  50.  
  51.  pad_x=52
  52.  pad_y=120
  53.  pad_dx=0
  54.  pad_w=24
  55.  pad_h=3
  56.  pad_c=7
  57.  
  58.  brick_w=9
  59.  brick_h=4
  60.  
  61.  levelnum = 1
  62.  level = levels[levelnum]
  63.  buildbricks(level)
  64.  --brick_y=20
  65.  
  66.  lives=3
  67.  points=0
  68.  
  69.  sticky=true
  70.  
  71.  chain=1 --combo chain multiplier
  72.  
  73.  serveball()
  74. end
  75.  
  76. function nextlevel()
  77.  mode="game"
  78.  pad_x=52
  79.  pad_y=120
  80.  pad_dx=0
  81.  
  82.  levelnum+=1
  83.  if levelnum > #levels then
  84.   -- we've beaten the gane
  85.   -- we need some kind of special
  86.   -- screen here
  87.   mode="start"
  88.   return
  89.  end
  90.  level=levels[levelnum]
  91.  buildbricks(level)
  92.  
  93.  sticky=true
  94.  chain=1
  95.  
  96.  serveball()
  97. end
  98.  
  99. function buildbricks(lvl)
  100.  local i,j,o,chr,last
  101.  brick_x={}
  102.  brick_y={}
  103.  brick_v={}
  104.  
  105.  j=0
  106.  for i=1,#lvl do
  107.   j+=1
  108.   chr=sub(lvl,i,i)  
  109.   if chr=="b" then
  110.    last="b"
  111.    add(brick_x,4+((j-1)%11)*(brick_w+2))
  112.    add(brick_y,20+flr((j-1)/11)*(brick_h+2))
  113.    add(brick_v,true)
  114.   elseif chr=="x" then
  115.    last="x"
  116.   elseif chr=="/" then
  117.    j=(flr((j-1)/11)+1)*11
  118.   elseif chr>="1" and chr<="9" then
  119.    for o=1,chr+0 do
  120.     if last=="b" then
  121.      add(brick_x,4+((j-1)%11)*(brick_w+2))
  122.      add(brick_y,20+flr((j-1)/11)*(brick_h+2))
  123.      add(brick_v,true)
  124.     elseif last=="x" then
  125.      --nothing
  126.     end
  127.     j+=1
  128.    end
  129.    j-=1
  130.   end
  131.  end
  132. end
  133.  
  134. function levelfinished()
  135.  if #brick_v == 0 then return true end
  136.  
  137.  for i=1,#brick_v do
  138.   if brick_v[i] == true then
  139.    return false
  140.   end
  141.  end
  142.  return true
  143. end
  144.  
  145. function serveball()
  146.  ball_x=pad_x+flr(pad_w/2)
  147.  ball_y=pad_y-ball_r
  148.  ball_dx=1
  149.  ball_dy=-1
  150.  ball_ang=1
  151.  chain=1
  152.  
  153.  sticky=true
  154.  --0.50
  155.  --1.30
  156. end
  157.  
  158. function setang(ang)
  159.  ball_ang=ang
  160.  if ang==2 then
  161.   ball_dx=0.50*sign(ball_dx)
  162.   ball_dy=1.30*sign(ball_dy)  
  163.  elseif ang==0 then
  164.   ball_dx=1.30*sign(ball_dx)
  165.   ball_dy=0.50*sign(ball_dy)
  166.  else
  167.   ball_dx=1*sign(ball_dx)
  168.   ball_dy=1*sign(ball_dy)
  169.  end
  170. end
  171.  
  172. function sign(n)
  173.  if n<0 then
  174.   return -1
  175.  elseif n>0 then
  176.   return 1
  177.  else
  178.   return 0
  179.  end
  180. end
  181.  
  182. function gameover()
  183.  mode="gameover"
  184. end
  185.  
  186. function levelover()
  187.  mode="levelover"
  188. end
  189.  
  190. function update_gameover()
  191.  if btnp(4) then
  192.   startgame()
  193.  end
  194. end
  195.  
  196. function update_levelover()
  197.  if btnp(4) then
  198.   nextlevel()
  199.  end
  200. end
  201.  
  202. function update_game()
  203.  local buttpress=false
  204.  local nextx,nexty,brickhit
  205.  
  206.  if btn(0) then
  207.   --left
  208.   pad_dx=-2.5
  209.   buttpress=true
  210.   --pad_x-=5
  211.   if sticky then
  212.    ball_dx=-1
  213.   end
  214.  end
  215.  if btn(1) then
  216.   --right
  217.   pad_dx=2.5
  218.   buttpress=true
  219.   --pad_x+=5
  220.   if sticky then
  221.    ball_dx=1
  222.   end
  223.  end
  224.  if sticky and btn(4) then
  225.   sticky=false
  226.  end
  227.  
  228.  if not(buttpress) then
  229.   pad_dx=pad_dx/1.3
  230.  end
  231.  pad_x+=pad_dx
  232.  pad_x=mid(0,pad_x,127-pad_w)
  233.  
  234.  if sticky then
  235.   ball_x=pad_x+flr(pad_w/2)
  236.   ball_y=pad_y-ball_r-1
  237.  else
  238.   --regular ball physics
  239.   nextx=ball_x+ball_dx
  240.   nexty=ball_y+ball_dy
  241.  
  242.   if nextx > 124 or nextx < 3 then
  243.    nextx=mid(0,nextx,127)
  244.    ball_dx = -ball_dx
  245.    sfx(0)
  246.   end
  247.   if nexty < 10 then
  248.    nexty=mid(0,nexty,127)
  249.    ball_dy = -ball_dy
  250.    sfx(0)
  251.   end
  252.  
  253.   -- check if ball hit pad
  254.   if ball_box(nextx,nexty,pad_x,pad_y,pad_w,pad_h) then
  255.    -- deal with collision
  256.    if deflx_ball_box(ball_x,ball_y,ball_dx,ball_dy,pad_x,pad_y,pad_w,pad_h) then
  257.     --ball hit paddle on the side
  258.     ball_dx = -ball_dx
  259.     if ball_x < pad_x+pad_w/2 then
  260.      nextx=pad_x-ball_r
  261.     else
  262.      nextx=pad_x+pad_w+ball_r
  263.     end
  264.    else
  265.     --ball hit paddle on the top/bottom
  266.     ball_dy = -ball_dy
  267.     if ball_y > pad_y then
  268.      --bottom
  269.      nexty=pad_y+pad_h+ball_r
  270.     else
  271.      --top
  272.      nexty=pad_y-ball_r
  273.      if abs(pad_dx)>2 then
  274.       --change angle
  275.       if sign(pad_dx)==sign(ball_dx) then
  276.        --flatten angle
  277.        setang(mid(0,ball_ang-1,2))
  278.       else
  279.        --raise angle
  280.        if ball_ang==2 then
  281.         ball_dx=-ball_dx
  282.        else
  283.         setang(mid(0,ball_ang+1,2))
  284.        end
  285.       end
  286.      end
  287.     end
  288.    end
  289.    sfx(1)
  290.    chain=1
  291.   end
  292.  
  293.   brickhit=false
  294.   for i=1,#brick_x do
  295.    -- check if ball hit brick
  296.    if brick_v[i] and ball_box(nextx,nexty,brick_x[i],brick_y[i],brick_w,brick_h) then
  297.     -- deal with collision
  298.     if not(brickhit) then
  299.      if deflx_ball_box(ball_x,ball_y,ball_dx,ball_dy,brick_x[i],brick_y[i],brick_w,brick_h) then
  300.       ball_dx = -ball_dx
  301.      else
  302.       ball_dy = -ball_dy
  303.      end
  304.     end
  305.     brickhit=true
  306.     sfx(2+chain)
  307.     brick_v[i]=false
  308.     points+=10*chain
  309.     chain+=1
  310.     chain=mid(1,chain,7)
  311.     if levelfinished() then
  312.      _draw()
  313.      levelover()
  314.     end
  315.    end
  316.   end
  317.   ball_x=nextx
  318.   ball_y=nexty
  319.  
  320.   if nexty > 127 then
  321.    sfx(2)
  322.    lives-=1
  323.    if lives<0 then
  324.     gameover()
  325.    else
  326.     serveball()
  327.    end
  328.   end
  329.  end
  330. end
  331.  
  332.  
  333. function _draw()
  334.  if mode=="game" then
  335.   draw_game()
  336.  elseif mode=="start" then
  337.   draw_start()
  338.  elseif mode=="gameover" then
  339.   draw_gameover()
  340.  elseif mode=="levelover" then
  341.   draw_levelover()
  342.  end
  343. end
  344.  
  345. function draw_start()
  346.  cls()
  347.  print("pico hero breakout",30,40,7)
  348.  print("press \151 to start",32,80,11)
  349. end
  350.  
  351. function draw_gameover()
  352.  rectfill(0,60,128,75,0)
  353.  print("game over",46,62,7)
  354.  print("press \151 to restart",27,68,6)
  355. end
  356.  
  357. function draw_levelover()
  358.  rectfill(0,60,128,75,0)
  359.  print("stage clear!",46,62,7)
  360.  print("press \151 to continue",27,68,6)
  361. end
  362.  
  363. function draw_game()
  364.  local i
  365.  
  366.  cls(1)
  367.  circfill(ball_x,ball_y,ball_r, 10)
  368.  if sticky then
  369.   -- serve preview
  370.   line(ball_x+ball_dx*4,ball_y+ball_dy*4,ball_x+ball_dx*6,ball_y+ball_dy*6,10)
  371.  end
  372.  
  373.  rectfill(pad_x,pad_y,pad_x+pad_w,pad_y+pad_h,pad_c)
  374.  
  375.  --draw bricks
  376.  for i=1,#brick_x do
  377.   if brick_v[i] then
  378.    rectfill(brick_x[i],brick_y[i],brick_x[i]+brick_w,brick_y[i]+brick_h,14)
  379.   end
  380.  end
  381.  
  382.  rectfill(0,0,128,6,0)
  383.  if debug!="" then
  384.   print(debug,1,1,7)
  385.  else
  386.   print("lives:"..lives,1,1,7)
  387.   print("score:"..points,40,1,7)
  388.   print("chain:"..chain.."x",100,1,7)
  389.  end
  390. end
  391.  
  392. function ball_box(bx,by,box_x,box_y,box_w,box_h)
  393.  -- checks for a collion of the ball with a rectangle
  394.  if by-ball_r > box_y+box_h then return false end
  395.  if by+ball_r < box_y then return false end
  396.  if bx-ball_r > box_x+box_w then return false end
  397.  if bx+ball_r < box_x then return false end
  398.  return true
  399. end
  400.  
  401. function deflx_ball_box(bx,by,bdx,bdy,tx,ty,tw,th)
  402.     local slp = bdy / bdx
  403.     local cx, cy
  404.     if bdx == 0 then
  405.         return false
  406.     elseif bdy == 0 then
  407.         return true
  408.     elseif slp > 0 and bdx > 0 then
  409.         cx = tx - bx
  410.         cy = ty - by
  411.         return cx > 0 and cy/cx < slp
  412.     elseif slp < 0 and bdx > 0 then
  413.         cx = tx - bx
  414.         cy = ty + th - by
  415.         return cx > 0 and cy/cx >= slp
  416.     elseif slp > 0 and bdx < 0 then
  417.         cx = tx + tw - bx
  418.         cy = ty + th - by
  419.         return cx < 0 and cy/cx <= slp
  420.     else
  421.         cx = tx + tw - bx
  422.         cy = ty - by
  423.         return cx < 0 and cy/cx >= slp
  424.     end
  425. end
Add Comment
Please, Sign In to add comment