Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function slowPrint(text, delay)
- for i = 1, #text do
- term.setTextColor(colors.gray)
- term.write(text:sub(i, i))
- sleep(delay)
- end
- print() -- Move to the next line after printing the text
- end
- local function init()
- local maxX, maxY = term.getSize() -- Define maxX and maxY for pocket computers
- local function isPocketComputer()
- 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")
- end
- local function promptMonitorSize()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- print("Are you using a smaller, normal, or larger monitor? (s/n/l)")
- local input
- repeat
- input = string.lower(io.read())
- until input == "s" or input == "n" or input == "l"
- return input
- end
- local function promptPocketComputer()
- term.clear()
- term.setCursorPos(1, 1)
- print("Are you using a pocket computer? (y/n)")
- local input
- repeat
- input = string.lower(io.read())
- until input == "y" or input == "n"
- return input == "y"
- end
- local function promptDifficulty()
- print("Select difficulty: 1 (easy), 2 (normal), 3 (hard)")
- local difficulty
- repeat
- difficulty = tonumber(io.read())
- until difficulty == 1 or difficulty == 2 or difficulty == 3
- return difficulty
- end
- local function setGameParameters(isPocket, monitorSize, difficulty)
- local birdY, score, pipes, pipeGap, gravity, jumpStrength, pipeSpawnRate, pipeWidth, birdCharacter, pipeCharacter, bgColor
- if monitorSize == "l" then
- birdY, score, pipes = maxY / 2, 0, {}
- pipeGap = 15 -- Adjusted gap size for larger monitor
- gravity = 0.4
- jumpStrength = -5
- pipeSpawnRate = 40 -- Adjusted spawn rate for larger monitor
- pipeWidth = 5
- birdCharacter = ">"
- pipeCharacter = "#"
- bgColor = colors.lightBlue
- elseif monitorSize == "n" then
- birdY, score, pipes = maxY / 2, 0, {}
- pipeGap = 12
- gravity = 0.6
- jumpStrength = -5
- pipeSpawnRate = 35
- pipeWidth = 5
- birdCharacter = ">"
- pipeCharacter = "#"
- bgColor = colors.lightBlue
- elseif monitorSize == "s" then
- birdY, score, pipes = maxY / 2, 0, {}
- pipeGap = 6
- gravity = 0.6
- jumpStrength = -4
- pipeSpawnRate = 20
- pipeWidth = 3
- birdCharacter = ">"
- pipeCharacter = "#"
- bgColor = colors.lightBlue
- if isPocket then
- pipeGap = 15
- gravity = 0.6
- jumpStrength = -2
- pipeSpawnRate = 40
- pipeWidth = 5
- end
- elseif isPocket then
- birdY, score, pipes = maxY / 2, 0, {}
- pipeGap = 15
- gravity = 0.6
- jumpStrength = -2
- pipeSpawnRate = 40
- pipeWidth = 5
- birdCharacter = ">"
- pipeCharacter = "#"
- bgColor = colors.lightBlue
- else
- birdY, score, pipes = maxY / 2, 0, {}
- pipeWidth = 5
- birdCharacter = ">"
- pipeCharacter = "#"
- bgColor = colors.lightBlue
- if difficulty == 1 then
- pipeGap = 18
- gravity = 0.8
- jumpStrength = -4
- pipeSpawnRate = 30
- elseif difficulty == 2 then
- pipeGap = 14
- gravity = 0.6
- jumpStrength = -5
- pipeSpawnRate = 35
- elseif difficulty == 3 then
- pipeGap = 8
- gravity = 0.4
- jumpStrength = -6
- pipeSpawnRate = 50
- end
- end
- return birdY, score, pipes, pipeGap, gravity, jumpStrength, pipeSpawnRate, pipeWidth, birdCharacter, pipeCharacter, bgColor
- end
- local birdY, score, pipes, pipeGap, gravity, jumpStrength, pipeSpawnRate, pipeWidth, birdCharacter, pipeCharacter
- local monitorSize = promptMonitorSize()
- local isPocket = false
- local difficulty = 2 -- Default to normal difficulty
- if monitorSize == "l" or monitorSize == "n" then
- difficulty = promptDifficulty()
- elseif monitorSize == "s" then
- isPocket = promptPocketComputer()
- end
- birdY, score, pipes, pipeGap, gravity, jumpStrength, pipeSpawnRate, pipeWidth, birdCharacter, pipeCharacter, bgColor = setGameParameters(isPocket, monitorSize, difficulty)
- maxX, maxY = term.getSize() -- Define maxX and maxY dynamically after setting game parameters
- slowPrint("Flappy Bird! Ported By nonogamer9 For ComputerCraft", 0.05)
- local function newPipe()
- local gapStart = math.random(2, maxY - pipeGap - 1)
- local pipeX = maxX
- if isPocket then
- pipeX = math.min(pipeX, maxX - pipeWidth - 1) -- Adjust pipeX for pocket computers to ensure the pipe is visible
- end
- pipes[#pipes + 1] = { pipeX, gapStart, gapStart + pipeGap }
- end
- local function resetPipes()
- pipes = {}
- for _ = 1, 3 do
- newPipe()
- end
- end
- local function drawBird()
- term.setCursorPos(5, math.floor(birdY))
- if term.isColor and term.isColor() then
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.orange)
- else
- term.setTextColor(colors.white)
- end
- term.write(birdCharacter)
- end
- local function drawGround()
- if term.isColor and term.isColor() then
- term.setBackgroundColor(colors.green)
- else
- term.setTextColor(colors.white)
- end
- for x = 1, maxX do
- term.setCursorPos(x, maxY)
- term.write("=")
- end
- end
- local function drawPipe(pipe)
- if term.isColor and term.isColor() then
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.green)
- else
- term.setTextColor(colors.white)
- end
- for y = 1, pipe[2] - 1 do
- term.setCursorPos(pipe[1], y)
- term.write(pipeCharacter)
- end
- for y = pipe[3] + 1, maxY do
- term.setCursorPos(pipe[1], y)
- term.write(pipeCharacter)
- end
- end
- local function drawScore()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(math.ceil((maxX - #("Score: " .. score)) / 2), 2)
- term.write("Score: " .. score)
- end
- local function gameOver()
- local text = "GAME OVER"
- term.setCursorPos(math.ceil((maxX - #text) / 2), maxY / 2)
- term.write(text)
- term.setCursorPos(math.ceil((maxX - 12) / 2), maxY / 2 + 1)
- term.write("Score: " .. score)
- os.pullEvent("key")
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- error()
- end
- local function checkCollision()
- local birdX = 5
- if birdY >= maxY - 1 or birdY <= 0 then
- return true
- end
- if #pipes == 0 then -- Check if pipes table is empty
- return false
- end
- for _, pipe in ipairs(pipes) do
- if birdX >= pipe[1] and birdX <= pipe[1] + pipeWidth then
- if birdY < pipe[2] or birdY > pipe[3] then
- return true
- end
- end
- end
- return false
- end
- local function draw()
- if isPocket then
- term.clear()
- end
- term.setBackgroundColor(bgColor)
- term.clear()
- drawGround()
- for _, pipe in ipairs(pipes) do
- drawPipe(pipe)
- end
- drawBird()
- drawScore()
- end
- resetPipes()
- local id = os.startTimer(0.1)
- local isJumping = false
- local gameStarted = false -- Flag to indicate if the game has started
- while true do
- local event = { os.pullEvent() }
- if event[1] == "timer" and event[2] == id then
- id = os.startTimer(0.1)
- if gameStarted then
- for i = #pipes, 1, -1 do
- pipes[i][1] = pipes[i][1] - 1
- if pipes[i][1] <= -pipeWidth then
- table.remove(pipes, i)
- end
- end
- if #pipes > 0 and pipes[#pipes][1] == maxX - pipeSpawnRate then
- newPipe()
- end
- for _, pipe in ipairs(pipes) do
- if pipe[1] == 5 then
- score = score + 1
- end
- end
- if checkCollision() then
- draw()
- gameOver()
- end
- if isJumping then
- birdY = math.max(2, birdY + jumpStrength)
- isJumping = false
- else
- birdY = math.min(maxY - 1, birdY + gravity)
- end
- else
- gameStarted = true
- end
- draw()
- elseif (event[1] == "key" and event[2] == keys.space) or (event[1] == "mouse_click" and event[2] == 1) then
- isJumping = true
- end
- end
- end
- init() -- Call the initialization function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement