Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---------CONFIG----------
- -- The side the monitor (of any size) is on
- monitorside = 'top'
- -- The side the jump/retry button is on
- -- Set it to 'touch' to use the screen
- -- as a button
- buttonside = 'touch'
- -- If this side receives rs signal when
- -- the game over screen is visible, the
- -- program quits
- quitside = 'back'
- -- The size of the space between the top
- -- and bottom pipes. The smaller the
- -- harder the game gets
- holesize = 3
- -- The height the bird jumps with each press.
- jumpsize = 3
- -- The time spent sleeping between each
- -- frame. The smaller the harder
- waittime = 0.4
- -------------------------
- function setup()
- print('FlappyCraft by Zegrento7')
- mon = peripheral.wrap(monitorside)
- scrw, scrh = mon.getSize()
- holesize = holesize - 1 --Dirty workaround
- end
- function reset()
- bird = 1
- pipes = {{scrw, math.random(1, scrh - holesize)}}
- jump = 0
- score = 0
- time = 0
- tch = false
- end
- function checkButton()
- if buttonside == 'touch' then
- if tch then
- tch = false
- return true
- else
- return false
- end
- else
- return rs.getInput(buttonside)
- end
- end
- function clearscr()
- mon.setBackgroundColor(colors.black)
- mon.clear()
- end
- function gameover()
- clearscr()
- mon.setCursorPos(scrw / 2 - 5, scrh / 2)
- mon.write('Game over!')
- mon.setCursorPos(scrw / 2 - 5, scrh / 2 + 1)
- mon.write('Score: ' .. score)
- return nil
- end
- function drawbird()
- mon.setBackgroundColor(colors.yellow)
- mon.setCursorPos(2, bird)
- mon.write(' ')
- end
- function drawpipes()
- mon.setBackgroundColor(colors.green)
- for p = 1, #pipes do
- local x, h = pipes[p][1], pipes[p][2]
- for y = 1, scrh do
- if y < h or y > h + holesize then
- mon.setCursorPos(x, y)
- mon.write(' ')
- end
- end
- end
- end
- function loop()
- while true do
- if jump > 0 then
- if bird == 1 then
- jump = 0
- else
- bird = bird - 1
- jump = jump - 1
- end
- else
- bird = bird + 1
- end
- if bird > scrh then return gameover() end
- x, h = pipes[1][1], pipes[1][2]
- if x == 3 then
- if bird < h or bird > h + holesize then return gameover() end
- end
- if x == 1 then
- for p = 1, #pipes - 1 do
- pipes[p] = pipes[p + 1]
- end
- pipes[#pipes] = nil
- elseif x == 2 then
- score = score + 1
- end
- for p = 1, #pipes do
- pipes[p][1] = pipes[p][1] - 1
- end
- if time > 0 and time % scrh == 0 then
- pipes[#pipes + 1] = {scrw, math.random(1, scrh - holesize)}
- end
- if checkButton() then jump = 3 end
- clearscr()
- drawpipes()
- drawbird()
- time = time + 1
- sleep(waittime)
- end
- end
- function checkTouch()
- while true do
- os.pullEvent('monitor_touch')
- tch = true
- end
- end
- function main()
- while true do
- reset()
- loop()
- tch = false --Another dirty workaround
- os.sleep(1) --And another. Yuck, I hate myself
- while not checkButton() do
- if rs.getInput(quitside) then return end
- sleep(0.2)
- end
- end
- end
- setup()
- if buttonside == 'touch' then
- parallel.waitForAny(checkTouch, main)
- else
- main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement