Advertisement
nonogamer9

Flappy Bird For ComputerCraft

May 15th, 2024 (edited)
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.75 KB | Gaming | 0 0
  1. local function slowPrint(text, delay)
  2.     for i = 1, #text do
  3.         term.setTextColor(colors.gray)
  4.         term.write(text:sub(i, i))
  5.         sleep(delay)
  6.     end
  7.     print()  -- Move to the next line after printing the text
  8. end
  9.  
  10. local function init()
  11.     local maxX, maxY = term.getSize()  -- Define maxX and maxY for pocket computers
  12.  
  13.     local function isPocketComputer()
  14.         return not peripheral.isPresent("top") and not peripheral.isPresent("bottom") and not peripheral.isPresent("front") and not peripheral.isPresent("back") and not peripheral.isPresent("left") and not peripheral.isPresent("right")
  15.     end
  16.  
  17.     local function promptMonitorSize()
  18.         term.setBackgroundColor(colors.black)
  19.         term.clear()
  20.         term.setCursorPos(1, 1)
  21.         print("Are you using a smaller, normal, or larger monitor? (s/n/l)")
  22.         local input
  23.         repeat
  24.             input = string.lower(io.read())
  25.         until input == "s" or input == "n" or input == "l"
  26.         return input
  27.     end
  28.  
  29.     local function promptPocketComputer()
  30.         term.clear()
  31.         term.setCursorPos(1, 1)
  32.         print("Are you using a pocket computer? (y/n)")
  33.         local input
  34.         repeat
  35.             input = string.lower(io.read())
  36.         until input == "y" or input == "n"
  37.         return input == "y"
  38.     end
  39.  
  40.     local function promptDifficulty()
  41.         print("Select difficulty: 1 (easy), 2 (normal), 3 (hard)")
  42.         local difficulty
  43.         repeat
  44.             difficulty = tonumber(io.read())
  45.         until difficulty == 1 or difficulty == 2 or difficulty == 3
  46.         return difficulty
  47.     end
  48.  
  49.     local function setGameParameters(isPocket, monitorSize, difficulty)
  50.         local birdY, score, pipes, pipeGap, gravity, jumpStrength, pipeSpawnRate, pipeWidth, birdCharacter, pipeCharacter, bgColor
  51.         if monitorSize == "l" then
  52.             birdY, score, pipes = maxY / 2, 0, {}
  53.             pipeGap = 15  -- Adjusted gap size for larger monitor
  54.             gravity = 0.4
  55.             jumpStrength = -5
  56.             pipeSpawnRate = 40  -- Adjusted spawn rate for larger monitor
  57.             pipeWidth = 5
  58.             birdCharacter = ">"
  59.             pipeCharacter = "#"
  60.             bgColor = colors.lightBlue
  61.         elseif monitorSize == "n" then
  62.             birdY, score, pipes = maxY / 2, 0, {}
  63.             pipeGap = 12
  64.             gravity = 0.6
  65.             jumpStrength = -5
  66.             pipeSpawnRate = 35
  67.             pipeWidth = 5
  68.             birdCharacter = ">"
  69.             pipeCharacter = "#"
  70.             bgColor = colors.lightBlue
  71.         elseif monitorSize == "s" then
  72.             birdY, score, pipes = maxY / 2, 0, {}
  73.             pipeGap = 6
  74.             gravity = 0.6
  75.             jumpStrength = -4
  76.             pipeSpawnRate = 20
  77.             pipeWidth = 3
  78.             birdCharacter = ">"
  79.             pipeCharacter = "#"
  80.             bgColor = colors.lightBlue
  81.             if isPocket then
  82.                 pipeGap = 15
  83.                 gravity = 0.6
  84.                 jumpStrength = -2
  85.                 pipeSpawnRate = 40
  86.                 pipeWidth = 5
  87.             end
  88.         elseif isPocket then
  89.             birdY, score, pipes = maxY / 2, 0, {}
  90.             pipeGap = 15
  91.             gravity = 0.6
  92.             jumpStrength = -2
  93.             pipeSpawnRate = 40
  94.             pipeWidth = 5
  95.             birdCharacter = ">"
  96.             pipeCharacter = "#"
  97.             bgColor = colors.lightBlue
  98.         else
  99.             birdY, score, pipes = maxY / 2, 0, {}
  100.             pipeWidth = 5
  101.             birdCharacter = ">"
  102.             pipeCharacter = "#"
  103.             bgColor = colors.lightBlue
  104.  
  105.             if difficulty == 1 then
  106.                 pipeGap = 18
  107.                 gravity = 0.8
  108.                 jumpStrength = -4
  109.                 pipeSpawnRate = 30
  110.             elseif difficulty == 2 then
  111.                 pipeGap = 14
  112.                 gravity = 0.6
  113.                 jumpStrength = -5
  114.                 pipeSpawnRate = 35
  115.             elseif difficulty == 3 then
  116.                 pipeGap = 8
  117.                 gravity = 0.4
  118.                 jumpStrength = -6
  119.                 pipeSpawnRate = 50
  120.             end
  121.         end
  122.  
  123.         return birdY, score, pipes, pipeGap, gravity, jumpStrength, pipeSpawnRate, pipeWidth, birdCharacter, pipeCharacter, bgColor
  124.     end
  125.  
  126.     local birdY, score, pipes, pipeGap, gravity, jumpStrength, pipeSpawnRate, pipeWidth, birdCharacter, pipeCharacter
  127.  
  128.     local monitorSize = promptMonitorSize()
  129.     local isPocket = false
  130.     local difficulty = 2 -- Default to normal difficulty
  131.  
  132.     if monitorSize == "l" or monitorSize == "n" then
  133.         difficulty = promptDifficulty()
  134.     elseif monitorSize == "s" then
  135.         isPocket = promptPocketComputer()
  136.     end
  137.  
  138.     birdY, score, pipes, pipeGap, gravity, jumpStrength, pipeSpawnRate, pipeWidth, birdCharacter, pipeCharacter, bgColor = setGameParameters(isPocket, monitorSize, difficulty)
  139.     maxX, maxY = term.getSize()  -- Define maxX and maxY dynamically after setting game parameters
  140.  
  141.     slowPrint("Flappy Bird! Ported By nonogamer9 For ComputerCraft", 0.05)
  142.  
  143.     local function newPipe()
  144.         local gapStart = math.random(2, maxY - pipeGap - 1)
  145.         local pipeX = maxX
  146.         if isPocket then
  147.             pipeX = math.min(pipeX, maxX - pipeWidth - 1) -- Adjust pipeX for pocket computers to ensure the pipe is visible
  148.         end
  149.         pipes[#pipes + 1] = { pipeX, gapStart, gapStart + pipeGap }
  150.     end
  151.  
  152.     local function resetPipes()
  153.         pipes = {}
  154.         for _ = 1, 3 do
  155.             newPipe()
  156.         end
  157.     end
  158.  
  159.     local function drawBird()
  160.         term.setCursorPos(5, math.floor(birdY))
  161.         if term.isColor and term.isColor() then
  162.             term.setBackgroundColor(colors.yellow)
  163.             term.setTextColor(colors.orange)
  164.         else
  165.             term.setTextColor(colors.white)
  166.         end
  167.         term.write(birdCharacter)
  168.     end
  169.  
  170.     local function drawGround()
  171.         if term.isColor and term.isColor() then
  172.             term.setBackgroundColor(colors.green)
  173.         else
  174.             term.setTextColor(colors.white)
  175.         end
  176.         for x = 1, maxX do
  177.             term.setCursorPos(x, maxY)
  178.             term.write("=")
  179.         end
  180.     end
  181.  
  182.     local function drawPipe(pipe)
  183.         if term.isColor and term.isColor() then
  184.             term.setBackgroundColor(colors.green)
  185.             term.setTextColor(colors.green)
  186.         else
  187.             term.setTextColor(colors.white)
  188.         end
  189.         for y = 1, pipe[2] - 1 do
  190.             term.setCursorPos(pipe[1], y)
  191.             term.write(pipeCharacter)
  192.         end
  193.         for y = pipe[3] + 1, maxY do
  194.             term.setCursorPos(pipe[1], y)
  195.             term.write(pipeCharacter)
  196.         end
  197.     end
  198.  
  199.     local function drawScore()
  200.         term.setBackgroundColor(colors.black)
  201.         term.setTextColor(colors.white)
  202.         term.setCursorPos(math.ceil((maxX - #("Score: " .. score)) / 2), 2)
  203.         term.write("Score: " .. score)
  204.     end
  205.  
  206.     local function gameOver()
  207.         local text = "GAME OVER"
  208.         term.setCursorPos(math.ceil((maxX - #text) / 2), maxY / 2)
  209.         term.write(text)
  210.         term.setCursorPos(math.ceil((maxX - 12) / 2), maxY / 2 + 1)
  211.         term.write("Score: " .. score)
  212.         os.pullEvent("key")
  213.         term.setBackgroundColor(colors.black)
  214.         term.clear()
  215.         term.setCursorPos(1, 1)
  216.         error()
  217.     end
  218.  
  219.     local function checkCollision()
  220.         local birdX = 5
  221.         if birdY >= maxY - 1 or birdY <= 0 then
  222.             return true
  223.         end
  224.         if #pipes == 0 then  -- Check if pipes table is empty
  225.             return false
  226.         end
  227.         for _, pipe in ipairs(pipes) do
  228.             if birdX >= pipe[1] and birdX <= pipe[1] + pipeWidth then
  229.                 if birdY < pipe[2] or birdY > pipe[3] then
  230.                     return true
  231.                 end
  232.             end
  233.         end
  234.         return false
  235.     end
  236.  
  237.     local function draw()
  238.         if isPocket then
  239.             term.clear()
  240.         end
  241.         term.setBackgroundColor(bgColor)
  242.         term.clear()
  243.         drawGround()
  244.         for _, pipe in ipairs(pipes) do
  245.             drawPipe(pipe)
  246.         end
  247.         drawBird()
  248.         drawScore()
  249.     end
  250.  
  251.     resetPipes()
  252.  
  253.     local id = os.startTimer(0.1)
  254.     local isJumping = false
  255.     local gameStarted = false  -- Flag to indicate if the game has started
  256.  
  257.     while true do
  258.         local event = { os.pullEvent() }
  259.         if event[1] == "timer" and event[2] == id then
  260.             id = os.startTimer(0.1)
  261.             if gameStarted then
  262.                 for i = #pipes, 1, -1 do
  263.                     pipes[i][1] = pipes[i][1] - 1
  264.                     if pipes[i][1] <= -pipeWidth then
  265.                         table.remove(pipes, i)
  266.                     end
  267.                 end
  268.                 if #pipes > 0 and pipes[#pipes][1] == maxX - pipeSpawnRate then
  269.                     newPipe()
  270.                 end
  271.                 for _, pipe in ipairs(pipes) do
  272.                     if pipe[1] == 5 then
  273.                         score = score + 1
  274.                     end
  275.                 end
  276.                 if checkCollision() then
  277.                     draw()
  278.                     gameOver()
  279.                 end
  280.                 if isJumping then
  281.                     birdY = math.max(2, birdY + jumpStrength)
  282.                     isJumping = false
  283.                 else
  284.                     birdY = math.min(maxY - 1, birdY + gravity)
  285.                 end
  286.             else
  287.                 gameStarted = true
  288.             end
  289.             draw()
  290.         elseif (event[1] == "key" and event[2] == keys.space) or (event[1] == "mouse_click" and event[2] == 1) then
  291.             isJumping = true
  292.         end
  293.     end
  294. end
  295.  
  296. init()  -- Call the initialization function
  297.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement