Advertisement
nonogamer9

Flappy Bird For OpenComputers

May 15th, 2024 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.88 KB | Gaming | 0 0
  1. local maxX, maxY = term.getSize()  -- Define maxX and maxY globally
  2.  
  3. local birdY, score, pipes = maxY / 2, 0, {}
  4. local pipeGap = 12
  5. local gravity = 0.3  -- Adjusted gravity
  6. local jumpStrength = -7  -- Adjusted jumping strength
  7. local pipeSpawnRate = 35
  8. local pipeWidth = 5
  9. local birdCharacter = ">"
  10. local pipeCharacter = "#"
  11.  
  12. local compatibilityMode = false
  13.  
  14. local function newPipe()
  15.     local gapStart = math.random(2, maxY - pipeGap - 1)
  16.     local pipeX = maxX
  17.     pipes[#pipes + 1] = { pipeX, gapStart, gapStart + pipeGap }
  18. end
  19.  
  20. local function resetPipes()
  21.     pipes = {}
  22.     for _ = 1, 3 do
  23.         newPipe()
  24.     end
  25. end
  26.  
  27. local function drawBird()
  28.     term.setCursorPos(5, math.floor(birdY))
  29.     if compatibilityMode then
  30.         term.setTextColor(colors.white)
  31.     else
  32.         term.setBackgroundColor(colors.yellow)
  33.         term.setTextColor(colors.orange)
  34.     end
  35.     term.write(birdCharacter)
  36. end
  37.  
  38. local function drawGround()
  39.     if compatibilityMode then
  40.         term.setTextColor(colors.white)
  41.     else
  42.         term.setBackgroundColor(colors.green)
  43.     end
  44.     for x = 1, maxX do
  45.         term.setCursorPos(x, maxY)
  46.         term.write("=")
  47.     end
  48. end
  49.  
  50. local function drawPipe(pipe)
  51.     if compatibilityMode then
  52.         term.setTextColor(colors.white)
  53.     else
  54.         term.setBackgroundColor(colors.green)
  55.         term.setTextColor(colors.green)
  56.     end
  57.     for y = 1, pipe[2] - 1 do
  58.         term.setCursorPos(pipe[1], y)
  59.         term.write(pipeCharacter)
  60.     end
  61.     for y = pipe[3] + 1, maxY do
  62.         term.setCursorPos(pipe[1], y)
  63.         term.write(pipeCharacter)
  64.     end
  65. end
  66.  
  67. local function drawScore()
  68.     if compatibilityMode then
  69.         term.setTextColor(colors.white)
  70.     else
  71.         term.setBackgroundColor(colors.black)
  72.         term.setTextColor(colors.white)
  73.     end
  74.     term.setCursorPos(5, 2)
  75.     term.write("Score: " .. score)
  76. end
  77.  
  78. local function gameOver()
  79.     local text = "GAME OVER"
  80.     term.setCursorPos(math.ceil((maxX - #text) / 2), maxY / 2)
  81.     term.write(text)
  82.     term.setCursorPos(math.ceil((maxX - 12) / 2), maxY / 2 + 1)
  83.     term.write("Score: " .. score)
  84.     os.pullEvent("key")
  85.     term.setBackgroundColor(colors.black)
  86.     term.clear()
  87.     term.setCursorPos(1, 1)
  88.     error()
  89. end
  90.  
  91. local function checkCollision()
  92.     local birdX = 5
  93.     if birdY >= maxY - 1 or birdY <= 0 then
  94.         return true
  95.     end
  96.     if #pipes == 0 then  -- Check if pipes table is empty
  97.         return false
  98.     end
  99.     for _, pipe in ipairs(pipes) do
  100.         if birdX >= pipe[1] and birdX <= pipe[1] + pipeWidth then
  101.             if birdY < pipe[2] or birdY > pipe[3] then
  102.                 return true
  103.             end
  104.         end
  105.     end
  106.     return false
  107. end
  108.  
  109. local function promptCompatibilityMode()
  110.     term.setBackgroundColor(colors.black)
  111.     term.clear()
  112.     term.setCursorPos(1, 1)
  113.     print("Enable compatibility mode? (y/n)")
  114.     local input
  115.     repeat
  116.         input = string.lower(io.read())
  117.     until input == "y" or input == "n"
  118.     compatibilityMode = input == "y"
  119. end
  120.  
  121. local function draw()
  122.     maxX, maxY = term.getSize()  -- Calculate maxX and maxY dynamically
  123.     if compatibilityMode then
  124.         term.setBackgroundColor(colors.black)
  125.         term.clear()
  126.     else
  127.         term.setBackgroundColor(colors.lightBlue)
  128.         term.clear()
  129.     end
  130.     drawGround()
  131.     for _, pipe in ipairs(pipes) do
  132.         drawPipe(pipe)
  133.     end
  134.     drawBird()
  135.     drawScore()
  136. end
  137.  
  138. term.setBackgroundColor(colors.lightBlue)
  139. term.clear()
  140.  
  141. promptCompatibilityMode()
  142.  
  143. resetPipes()
  144.  
  145. local id = os.startTimer(0.1)
  146. local isJumping = false
  147. local gameStarted = false  -- Flag to indicate if the game has started
  148.  
  149. while true do
  150.     local event = { os.pullEvent() }
  151.     if event[1] == "timer" and event[2] == id then
  152.         id = os.startTimer(0.1)
  153.         if gameStarted then
  154.             for i = #pipes, 1, -1 do
  155.                 pipes[i][1] = pipes[i][1] - 1
  156.                 if pipes[i][1] <= -pipeWidth then
  157.                     table.remove(pipes, i)
  158.                 end
  159.             end
  160.             if #pipes > 0 and pipes[#pipes][1] == maxX - pipeSpawnRate then
  161.                 newPipe()
  162.             end
  163.             for _, pipe in ipairs(pipes) do
  164.                 if pipe[1] == 5 then
  165.                     score = score + 1
  166.                 end
  167.             end
  168.             if checkCollision() then
  169.                 draw()
  170.                 gameOver()
  171.             end
  172.             if isJumping then
  173.                 birdY = math.max(2, birdY + jumpStrength)
  174.                 isJumping = false
  175.             else
  176.                 birdY = math.min(maxY - 1, birdY + gravity)
  177.             end
  178.         else
  179.             gameStarted = true
  180.         end
  181.         draw()
  182.     elseif (event[1] == "key" and event[2] == keys.space) or (event[1] == "mouse_click" and event[2] == 1) then
  183.         isJumping = true
  184.     end
  185. end
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement