Ocawesome101

Flappy CC

Feb 7th, 2022 (edited)
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 KB | None | 0 0
  1. -- Flappy Bird clone
  2.  
  3. -- XXX scoring doesn't work at irregular frametimes (i.e. only 10/20 work)
  4. local ftTarget = 1/10
  5. local score = 0
  6.  
  7. local pillars = {}
  8.  
  9. if not term.isColor() then
  10.   error("this program requires an advanced computer", 0)
  11. end
  12.  
  13. local old
  14. if not term.current().isVisible then
  15.   old = term.redirect(window.create(term.current(), 1, 1, term.getSize()))
  16. end
  17.  
  18. -- CC maxes out at 20fps
  19. if ftTarget < 1/20 then
  20.   ftTarget = 1/20
  21. end
  22.  
  23. -- half the gap size in characters
  24. local gapsize = 4
  25. local width, height = term.getSize()
  26. local pillarCount = math.floor(width / 10)
  27.  
  28. local halfHeight = math.floor(height * 0.5)
  29. -- x is x-coordinate on screen
  30. -- height is the y-coordinate at which the center of the gap is
  31. local function drawPillar(x, y)
  32.   x = math.floor(x)
  33.   -- draw the top bit
  34.   local tby, bty = y - gapsize, y + gapsize
  35.   for i=1, height, 1 do
  36.     if i < tby or i > bty then
  37.       term.setCursorPos(x, i)
  38.       term.blit("  ", "5d", "5d")
  39.     end
  40.   end
  41. end
  42.  
  43. local birdX = math.floor(width / 2)
  44. local function drawBird(y)
  45.   term.setCursorPos(birdX, y)
  46.   -- flip/flip/noflip
  47.   term.blit(" \x84\x90", "3f1", "443")
  48.   term.setCursorPos(birdX, y+1)
  49.   -- flip/flip/noflip
  50.   term.blit("  \x81", "331", "443")
  51. end
  52.  
  53. local birdY, birdV = math.floor(height / 2), 0
  54. local gapH, gapL = math.ceil(height * 0.25 + 0.5), math.floor(height * 0.75)
  55. local pillarGap = math.ceil(width / pillarCount)
  56.  
  57. local function drawObjects()
  58.   term.current().setVisible(false)
  59.   term.setBackgroundColor(colors.lightBlue)
  60.   term.clear()
  61.   for i=1, #pillars, 1 do
  62.     drawPillar(pillars[i].x, pillars[i].gap)
  63.   end
  64.   drawBird(math.floor(birdY))
  65.   term.setTextColor(colors.black)
  66.   term.setCursorPos(1, 1)
  67.   term.write("Score: " .. tostring(score))
  68.   term.current().setVisible(true)
  69. end
  70.  
  71. local function tickObjects()
  72.   birdV = birdV - 5*ftTarget
  73.   birdY = birdY - (birdV * ftTarget * 10)
  74.   if #pillars < pillarCount then
  75.     if #pillars == 0 or pillars[#pillars].x < width - pillarGap then
  76.       pillars[#pillars+1] = {x = width, gap = math.random(gapH, gapL)}
  77.     end
  78.   end
  79.   for i=#pillars, 1, -1 do
  80.     pillars[i].x = pillars[i].x - 10*ftTarget
  81.     if pillars[i].x < -1 then
  82.       table.remove(pillars, i)
  83.     elseif pillars[i].x >= birdX and pillars[i].x <= birdX + 1 then
  84.       local tby, bty = pillars[i].gap - gapsize, pillars[i].gap + gapsize
  85.       if birdY < tby or birdY > bty then
  86.         birdV = 2
  87.         repeat
  88.           birdV = birdV - 5*ftTarget
  89.           birdY = birdY - (birdV * ftTarget * 10)
  90.           drawObjects()
  91.           os.sleep(ftTarget)
  92.         until birdY > height + 10
  93.         if old then term.redirect(old) end
  94.         term.setBackgroundColor(colors.black)
  95.         term.setTextColor(colors.white)
  96.         term.clear()
  97.         term.setCursorPos(1,1)
  98.         error("died; score " .. score, 0)
  99.       elseif pillars[i].x == birdX then
  100.         score = score + 1
  101.       end
  102.     end
  103.   end
  104. end
  105.  
  106. -- target 10fps
  107. local tid
  108. local function tickInput()
  109.   local sig, id, x, y = os.pullEvent()
  110.   if sig == "timer" and id == tid then
  111.     tid = os.startTimer(ftTarget)
  112.     tickObjects()
  113.   elseif sig == "mouse_click" or sig == "monitor_touch" or
  114.       (sig == "key" and id == keys.space) then
  115.     birdV = 2
  116.   end
  117. end
  118.  
  119. tickObjects()
  120. drawObjects()
  121. term.setTextColor(colors.black)
  122. term.setCursorPos(3, 3)
  123. term.write("press space, click, or tap")
  124. repeat
  125.   local sig, id = os.pullEventRaw()
  126. until (sig == "key" and id == keys.space) or sig == "mouse_click"
  127.   or sig == "monitor_touch"
  128. tid = os.startTimer(0.1)
  129. birdV = 2
  130.  
  131. while true do
  132.   tickInput()
  133.   drawBird(math.floor(birdY))
  134.   drawObjects()
  135. end
Add Comment
Please, Sign In to add comment