Advertisement
Redxone

[PICO8] Simple game thing.

Aug 18th, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. x = 127/2
  2. y = 127/2
  3. xvel = 0
  4. yvel = 0
  5. spd  = 3
  6. left  = 0
  7. right = 1
  8. up    = 2
  9. down  = 3
  10. friction = 1.5
  11. spdup    = spd/6
  12. scrsize  = 127
  13. radius   = 3
  14. moved    = false
  15. bump     = false
  16. score    = 0
  17. targets = {}
  18. tarsize = 2
  19. tarmax  = 10
  20. -- game loop functions
  21. function collide(x,y,r,xx,yy,rr)
  22.    return (x < xx+rr  and
  23.            x+r > xx ) and
  24.           (y < yy+rr  and
  25.            y+r > yy )
  26. end
  27.  
  28. function _init()
  29.     cls()
  30. end
  31. function negcap()
  32.     if(xvel < 0) xvel = 0
  33.     if(yvel < 0) yvel = 0
  34. end
  35. function _update()
  36.      circfill(x,y,8,0)
  37.      moved = false
  38.         if(btn(up))    yvel -= spdup moved=true
  39.         if(btn(down))  yvel += spdup moved=true
  40.         if(btn(left))  xvel -= spdup moved=true
  41.         if(btn(right)) xvel += spdup moved=true
  42.         -- friction
  43.         if(not moved)then
  44.     yvel /= friction
  45.     xvel /= friction
  46.         end
  47.         if(xvel > spd) xvel = spd
  48.         if(yvel > spd) yvel = spd
  49.         if(xvel < -spd) xvel = -spd
  50.         if(yvel < -spd) yvel = -spd
  51.         x += xvel
  52.         y += yvel
  53.         -- x collision
  54.         if(x > scrsize-radius)then
  55.             x = scrsize-radius
  56.             if(moved) sfx(0)
  57.         elseif(x <= radius)then
  58.             x = radius
  59.             if(moved) sfx(0)
  60.         end
  61.         -- y collision
  62.         if(y > scrsize-radius)then
  63.             y = scrsize-radius
  64.             if(moved) sfx(0)
  65.         elseif(y <= radius)then
  66.             y = radius
  67.             if(moved) sfx(0)
  68.         end
  69.         -- draw velocities
  70.         rectfill(1,1,128,10,1)
  71.         print("score:  " .. score,1,1,8)
  72.        
  73.         -- game loop logic
  74.         spawn() -- spawn targets
  75.         -- target collision
  76.         for i = 1, #targets do
  77.         -- check each target for collision
  78.         if(collide(x,y,radius,
  79.                  targets[i].x,
  80.                  targets[i].y,
  81.                  tarsize)
  82.       )then
  83.          del(targets,targets[i])
  84.          xvel = xvel/2
  85.          yvel = yvel/2
  86.          score += 200  
  87.          sfx(1)
  88.          break  
  89.       else
  90.         -- clear target
  91.         circfill(
  92.             targets[i].x,
  93.             targets[i].y,tarsize,0)
  94.            -- move target
  95.          targets[i].y += 1
  96.          if(targets[i].y > scrsize)then
  97.            targets[i].y = rnd(scrsize-10)+10
  98.            targets[i].x = rnd(scrsize)
  99.          end
  100.       end
  101.      end
  102. end
  103.  
  104. function _draw()
  105.         -- draw light green circle.
  106.      circfill(x,y,radius,11)
  107.      for i = 1, #targets do
  108.         -- render each target
  109.         circfill(
  110.           targets[i].x,
  111.           targets[i].y,
  112.           tarsize,
  113.           targets[i].c)      
  114.      end
  115. end
  116.  
  117.  
  118. -- spawn in targets
  119.  
  120. function spawn()
  121.  -- randomly spawn
  122.     local spn = rnd(10)
  123.     local rx  = rnd(scrsize)
  124.     local ry  = rnd(scrsize-10)+10
  125.     if(flr(spn) == 5 and #targets < tarmax )then
  126.         targets[#targets+1] = {
  127.             x = rx,
  128.             y = ry,
  129.             c = rnd(14)+1
  130.         }
  131.     end
  132. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement