Advertisement
Zegrento7

Flappy Bird clone for Computercraft

Apr 3rd, 2015
1,170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. ---------CONFIG----------
  2.  
  3. -- The side the monitor (of any size) is on
  4. monitorside = 'top'
  5.  
  6. -- The side the jump/retry button is on
  7. -- Set it to 'touch' to use the screen
  8. -- as a button
  9. buttonside = 'touch'
  10.  
  11. -- If this side receives rs signal when
  12. -- the game over screen is visible, the
  13. -- program quits
  14. quitside = 'back'
  15.  
  16. -- The size of the space between the top
  17. -- and bottom pipes. The smaller the
  18. -- harder the game gets
  19. holesize = 3
  20.  
  21. -- The height the bird jumps with each press.
  22. jumpsize = 3
  23.  
  24. -- The time spent sleeping between each
  25. -- frame. The smaller the harder
  26. waittime = 0.4
  27. -------------------------
  28.  
  29. function setup()
  30.   print('FlappyCraft by Zegrento7')
  31.   mon = peripheral.wrap(monitorside)
  32.   scrw, scrh = mon.getSize()
  33.   holesize = holesize - 1 --Dirty workaround
  34. end
  35.  
  36. function reset()
  37.   bird = 1
  38.   pipes = {{scrw, math.random(1, scrh - holesize)}}
  39.   jump = 0
  40.   score = 0
  41.   time = 0
  42.   tch = false
  43. end
  44.  
  45. function checkButton()
  46.   if buttonside == 'touch' then
  47.     if tch then
  48.       tch = false
  49.       return true
  50.     else
  51.       return false
  52.     end
  53.   else
  54.     return rs.getInput(buttonside)
  55.   end
  56. end
  57.  
  58. function clearscr()
  59.   mon.setBackgroundColor(colors.black)
  60.   mon.clear()
  61. end
  62.  
  63. function gameover()
  64.   clearscr()
  65.   mon.setCursorPos(scrw / 2 - 5, scrh / 2)
  66.   mon.write('Game over!')
  67.   mon.setCursorPos(scrw / 2 - 5, scrh / 2 + 1)
  68.   mon.write('Score: ' .. score)
  69.   return nil
  70. end
  71.  
  72. function drawbird()
  73.   mon.setBackgroundColor(colors.yellow)
  74.   mon.setCursorPos(2, bird)
  75.   mon.write(' ')
  76. end
  77.  
  78. function drawpipes()
  79.   mon.setBackgroundColor(colors.green)
  80.   for p = 1, #pipes do
  81.     local x, h = pipes[p][1], pipes[p][2]
  82.     for y = 1, scrh do
  83.       if y < h or y > h + holesize then
  84.         mon.setCursorPos(x, y)
  85.         mon.write(' ')
  86.       end
  87.     end
  88.   end
  89. end
  90.  
  91. function loop()
  92.   while true do
  93.     if jump > 0 then
  94.       if bird == 1 then
  95.         jump = 0
  96.       else
  97.         bird = bird - 1
  98.         jump = jump - 1
  99.       end
  100.     else
  101.       bird = bird + 1
  102.     end
  103.    
  104.     if bird > scrh then return gameover() end
  105.  
  106.     x, h = pipes[1][1], pipes[1][2]
  107.     if x == 3 then
  108.       if bird < h or bird > h + holesize then return gameover() end
  109.     end
  110.  
  111.     if x == 1 then
  112.       for p = 1, #pipes - 1 do
  113.         pipes[p] = pipes[p + 1]
  114.       end
  115.       pipes[#pipes] = nil
  116.     elseif x == 2 then
  117.       score = score + 1
  118.     end
  119.  
  120.     for p = 1, #pipes do
  121.       pipes[p][1] = pipes[p][1] - 1
  122.     end
  123.  
  124.     if time > 0 and time % scrh == 0 then
  125.       pipes[#pipes + 1] = {scrw, math.random(1, scrh - holesize)}
  126.     end
  127.  
  128.     if checkButton() then jump = 3 end
  129.  
  130.     clearscr()
  131.     drawpipes()
  132.     drawbird()
  133.    
  134.     time = time + 1
  135.    
  136.     sleep(waittime)
  137.   end
  138. end
  139.  
  140. function checkTouch()
  141.   while true do
  142.     os.pullEvent('monitor_touch')
  143.     tch = true
  144.   end
  145. end
  146.  
  147. function main()
  148.   while true do
  149.     reset()
  150.     loop()
  151.     tch = false --Another dirty workaround
  152.     os.sleep(1) --And another. Yuck, I hate myself
  153.     while not checkButton() do
  154.       if rs.getInput(quitside) then return end
  155.       sleep(0.2)
  156.     end
  157.   end
  158. end
  159.  
  160. setup()
  161.  
  162. if buttonside == 'touch' then
  163.   parallel.waitForAny(checkTouch, main)
  164. else
  165.   main()
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement