Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local maxX, maxY = term.getSize() -- Define maxX and maxY globally
- local birdY, score, pipes = maxY / 2, 0, {}
- local pipeGap = 12
- local gravity = 0.3 -- Adjusted gravity
- local jumpStrength = -7 -- Adjusted jumping strength
- local pipeSpawnRate = 35
- local pipeWidth = 5
- local birdCharacter = ">"
- local pipeCharacter = "#"
- local compatibilityMode = false
- local function newPipe()
- local gapStart = math.random(2, maxY - pipeGap - 1)
- local pipeX = maxX
- 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 compatibilityMode then
- term.setTextColor(colors.white)
- else
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.orange)
- end
- term.write(birdCharacter)
- end
- local function drawGround()
- if compatibilityMode then
- term.setTextColor(colors.white)
- else
- term.setBackgroundColor(colors.green)
- end
- for x = 1, maxX do
- term.setCursorPos(x, maxY)
- term.write("=")
- end
- end
- local function drawPipe(pipe)
- if compatibilityMode then
- term.setTextColor(colors.white)
- else
- term.setBackgroundColor(colors.green)
- term.setTextColor(colors.green)
- 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()
- if compatibilityMode then
- term.setTextColor(colors.white)
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- term.setCursorPos(5, 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 promptCompatibilityMode()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- print("Enable compatibility mode? (y/n)")
- local input
- repeat
- input = string.lower(io.read())
- until input == "y" or input == "n"
- compatibilityMode = input == "y"
- end
- local function draw()
- maxX, maxY = term.getSize() -- Calculate maxX and maxY dynamically
- if compatibilityMode then
- term.setBackgroundColor(colors.black)
- term.clear()
- else
- term.setBackgroundColor(colors.lightBlue)
- term.clear()
- end
- drawGround()
- for _, pipe in ipairs(pipes) do
- drawPipe(pipe)
- end
- drawBird()
- drawScore()
- end
- term.setBackgroundColor(colors.lightBlue)
- term.clear()
- promptCompatibilityMode()
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement