Advertisement
Krystman

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

Mar 10th, 2017
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.06 KB | None | 0 0
  1. --goals
  2. -- 5. diffrent bricks
  3. --    - hardened brick
  4. --    - indestructable brick
  5. --    - exploding brick
  6. --    - powerup brick
  7.  
  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[1] = "hxixsxpxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxb"
  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.  brick_t={}
  105.  
  106.  j=0
  107.  -- b = normal brick
  108.  -- x = empty space
  109.  -- i = indestructable brick
  110.  -- h = hardened brick
  111.  -- s = sploding brick
  112.  -- p = powerup brick
  113.  
  114.  for i=1,#lvl do
  115.   j+=1
  116.   chr=sub(lvl,i,i)  
  117.   if chr=="b"
  118.   or chr=="i"
  119.   or chr=="h"
  120.   or chr=="s"
  121.   or chr=="p" then
  122.    last=chr
  123.    addbrick(j,chr)  
  124.   elseif chr=="x" then
  125.    last="x"
  126.   elseif chr=="/" then
  127.    j=(flr((j-1)/11)+1)*11
  128.   elseif chr>="1" and chr<="9" then
  129.    for o=1,chr+0 do
  130.     if last=="b"
  131.     or last=="i"
  132.     or last=="h"
  133.     or last=="s"
  134.     or last=="p" then
  135.         addbrick(j,last)
  136.     elseif last=="x" then
  137.      --nothing
  138.     end
  139.     j+=1
  140.    end
  141.    j-=1
  142.   end
  143.  end
  144. end
  145.  
  146. function addbrick(_i,_t)
  147.  add(brick_x,4+((_i-1)%11)*(brick_w+2))
  148.  add(brick_y,20+flr((_i-1)/11)*(brick_h+2))
  149.  add(brick_v,true)
  150.  add(brick_t,_t)
  151. end
  152.  
  153. function levelfinished()
  154.  if #brick_v == 0 then return true end
  155.  
  156.  for i=1,#brick_v do
  157.   if brick_v[i] == true then
  158.    return false
  159.   end
  160.  end
  161.  return true
  162. end
  163.  
  164. function serveball()
  165.  ball_x=pad_x+flr(pad_w/2)
  166.  ball_y=pad_y-ball_r
  167.  ball_dx=1
  168.  ball_dy=-1
  169.  ball_ang=1
  170.  chain=1
  171.  
  172.  sticky=true
  173.  --0.50
  174.  --1.30
  175. end
  176.  
  177. function setang(ang)
  178.  ball_ang=ang
  179.  if ang==2 then
  180.   ball_dx=0.50*sign(ball_dx)
  181.   ball_dy=1.30*sign(ball_dy)  
  182.  elseif ang==0 then
  183.   ball_dx=1.30*sign(ball_dx)
  184.   ball_dy=0.50*sign(ball_dy)
  185.  else
  186.   ball_dx=1*sign(ball_dx)
  187.   ball_dy=1*sign(ball_dy)
  188.  end
  189. end
  190.  
  191. function sign(n)
  192.  if n<0 then
  193.   return -1
  194.  elseif n>0 then
  195.   return 1
  196.  else
  197.   return 0
  198.  end
  199. end
  200.  
  201. function gameover()
  202.  mode="gameover"
  203. end
  204.  
  205. function levelover()
  206.  mode="levelover"
  207. end
  208.  
  209. function update_gameover()
  210.  if btnp(4) then
  211.   startgame()
  212.  end
  213. end
  214.  
  215. function update_levelover()
  216.  if btnp(4) then
  217.   nextlevel()
  218.  end
  219. end
  220.  
  221. function update_game()
  222.  local buttpress=false
  223.  local nextx,nexty,brickhit
  224.  
  225.  if btn(0) then
  226.   --left
  227.   pad_dx=-2.5
  228.   buttpress=true
  229.   --pad_x-=5
  230.   if sticky then
  231.    ball_dx=-1
  232.   end
  233.  end
  234.  if btn(1) then
  235.   --right
  236.   pad_dx=2.5
  237.   buttpress=true
  238.   --pad_x+=5
  239.   if sticky then
  240.    ball_dx=1
  241.   end
  242.  end
  243.  if sticky and btn(4) then
  244.   sticky=false
  245.  end
  246.  
  247.  if not(buttpress) then
  248.   pad_dx=pad_dx/1.3
  249.  end
  250.  pad_x+=pad_dx
  251.  pad_x=mid(0,pad_x,127-pad_w)
  252.  
  253.  if sticky then
  254.   ball_x=pad_x+flr(pad_w/2)
  255.   ball_y=pad_y-ball_r-1
  256.  else
  257.   --regular ball physics
  258.   nextx=ball_x+ball_dx
  259.   nexty=ball_y+ball_dy
  260.  
  261.   if nextx > 124 or nextx < 3 then
  262.    nextx=mid(0,nextx,127)
  263.    ball_dx = -ball_dx
  264.    sfx(0)
  265.   end
  266.   if nexty < 10 then
  267.    nexty=mid(0,nexty,127)
  268.    ball_dy = -ball_dy
  269.    sfx(0)
  270.   end
  271.  
  272.   -- check if ball hit pad
  273.   if ball_box(nextx,nexty,pad_x,pad_y,pad_w,pad_h) then
  274.    -- deal with collision
  275.    if deflx_ball_box(ball_x,ball_y,ball_dx,ball_dy,pad_x,pad_y,pad_w,pad_h) then
  276.     --ball hit paddle on the side
  277.     ball_dx = -ball_dx
  278.     if ball_x < pad_x+pad_w/2 then
  279.      nextx=pad_x-ball_r
  280.     else
  281.      nextx=pad_x+pad_w+ball_r
  282.     end
  283.    else
  284.     --ball hit paddle on the top/bottom
  285.     ball_dy = -ball_dy
  286.     if ball_y > pad_y then
  287.      --bottom
  288.      nexty=pad_y+pad_h+ball_r
  289.     else
  290.      --top
  291.      nexty=pad_y-ball_r
  292.      if abs(pad_dx)>2 then
  293.       --change angle
  294.       if sign(pad_dx)==sign(ball_dx) then
  295.        --flatten angle
  296.        setang(mid(0,ball_ang-1,2))
  297.       else
  298.        --raise angle
  299.        if ball_ang==2 then
  300.         ball_dx=-ball_dx
  301.        else
  302.         setang(mid(0,ball_ang+1,2))
  303.        end
  304.       end
  305.      end
  306.     end
  307.    end
  308.    sfx(1)
  309.    chain=1
  310.   end
  311.  
  312.   brickhit=false
  313.   for i=1,#brick_x do
  314.    -- check if ball hit brick
  315.    if brick_v[i] and ball_box(nextx,nexty,brick_x[i],brick_y[i],brick_w,brick_h) then
  316.     -- deal with collision
  317.     if not(brickhit) then
  318.      if deflx_ball_box(ball_x,ball_y,ball_dx,ball_dy,brick_x[i],brick_y[i],brick_w,brick_h) then
  319.       ball_dx = -ball_dx
  320.      else
  321.       ball_dy = -ball_dy
  322.      end
  323.     end
  324.     brickhit=true
  325.     sfx(2+chain)
  326.     brick_v[i]=false
  327.     points+=10*chain
  328.     chain+=1
  329.     chain=mid(1,chain,7)
  330.     if levelfinished() then
  331.      _draw()
  332.      levelover()
  333.     end
  334.    end
  335.   end
  336.   ball_x=nextx
  337.   ball_y=nexty
  338.  
  339.   if nexty > 127 then
  340.    sfx(2)
  341.    lives-=1
  342.    if lives<0 then
  343.     gameover()
  344.    else
  345.     serveball()
  346.    end
  347.   end
  348.  end
  349. end
  350.  
  351.  
  352. function _draw()
  353.  if mode=="game" then
  354.   draw_game()
  355.  elseif mode=="start" then
  356.   draw_start()
  357.  elseif mode=="gameover" then
  358.   draw_gameover()
  359.  elseif mode=="levelover" then
  360.   draw_levelover()
  361.  end
  362. end
  363.  
  364. function draw_start()
  365.  cls()
  366.  print("pico hero breakout",30,40,7)
  367.  print("press — to start",32,80,11)
  368. end
  369.  
  370. function draw_gameover()
  371.  rectfill(0,60,128,75,0)
  372.  print("game over",46,62,7)
  373.  print("press — to restart",27,68,6)
  374. end
  375.  
  376. function draw_levelover()
  377.  rectfill(0,60,128,75,0)
  378.  print("stage clear!",46,62,7)
  379.  print("press — to continue",27,68,6)
  380. end
  381.  
  382. function draw_game()
  383.  local i
  384.  
  385.  cls(1)
  386.  circfill(ball_x,ball_y,ball_r, 10)
  387.  if sticky then
  388.   -- serve preview
  389.   line(ball_x+ball_dx*4,ball_y+ball_dy*4,ball_x+ball_dx*6,ball_y+ball_dy*6,10)
  390.  end
  391.  
  392.  rectfill(pad_x,pad_y,pad_x+pad_w,pad_y+pad_h,pad_c)
  393.  
  394.  --draw bricks
  395.  for i=1,#brick_x do
  396.   if brick_v[i] then
  397.    if brick_t[i] == "b" then
  398.     brickcol = 14
  399.    elseif brick_t[i] == "i" then
  400.     brickcol = 5
  401.    elseif brick_t[i] == "h" then
  402.     brickcol = 15
  403.    elseif brick_t[i] == "s" then
  404.     brickcol = 10
  405.    elseif brick_t[i] == "p" then
  406.     brickcol = 12
  407.    end
  408.    rectfill(brick_x[i],brick_y[i],brick_x[i]+brick_w,brick_y[i]+brick_h,brickcol)
  409.   end
  410.  end
  411.  
  412.  rectfill(0,0,128,6,0)
  413.  if debug!="" then
  414.   print(debug,1,1,7)
  415.  else
  416.   print("lives:"..lives,1,1,7)
  417.   print("score:"..points,40,1,7)
  418.   print("chain:"..chain.."x",100,1,7)
  419.  end
  420. end
  421.  
  422. function ball_box(bx,by,box_x,box_y,box_w,box_h)
  423.  -- checks for a collion of the ball with a rectangle
  424.  if by-ball_r > box_y+box_h then return false end
  425.  if by+ball_r < box_y then return false end
  426.  if bx-ball_r > box_x+box_w then return false end
  427.  if bx+ball_r < box_x then return false end
  428.  return true
  429. end
  430.  
  431. function deflx_ball_box(bx,by,bdx,bdy,tx,ty,tw,th)
  432.     local slp = bdy / bdx
  433.     local cx, cy
  434.     if bdx == 0 then
  435.         return false
  436.     elseif bdy == 0 then
  437.         return true
  438.     elseif slp > 0 and bdx > 0 then
  439.         cx = tx - bx
  440.         cy = ty - by
  441.         return cx > 0 and cy/cx < slp
  442.     elseif slp < 0 and bdx > 0 then
  443.         cx = tx - bx
  444.         cy = ty + th - by
  445.         return cx > 0 and cy/cx >= slp
  446.     elseif slp > 0 and bdx < 0 then
  447.         cx = tx + tw - bx
  448.         cy = ty + th - by
  449.         return cx < 0 and cy/cx <= slp
  450.     else
  451.         cx = tx + tw - bx
  452.         cy = ty - by
  453.         return cx < 0 and cy/cx >= slp
  454.     end
  455. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement