Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Flappy Bird clone
- -- XXX scoring doesn't work at irregular frametimes (i.e. only 10/20 work)
- local ftTarget = 1/10
- local score = 0
- local pillars = {}
- if not term.isColor() then
- error("this program requires an advanced computer", 0)
- end
- local old
- if not term.current().isVisible then
- old = term.redirect(window.create(term.current(), 1, 1, term.getSize()))
- end
- -- CC maxes out at 20fps
- if ftTarget < 1/20 then
- ftTarget = 1/20
- end
- -- half the gap size in characters
- local gapsize = 4
- local width, height = term.getSize()
- local pillarCount = math.floor(width / 10)
- local halfHeight = math.floor(height * 0.5)
- -- x is x-coordinate on screen
- -- height is the y-coordinate at which the center of the gap is
- local function drawPillar(x, y)
- x = math.floor(x)
- -- draw the top bit
- local tby, bty = y - gapsize, y + gapsize
- for i=1, height, 1 do
- if i < tby or i > bty then
- term.setCursorPos(x, i)
- term.blit(" ", "5d", "5d")
- end
- end
- end
- local birdX = math.floor(width / 2)
- local function drawBird(y)
- term.setCursorPos(birdX, y)
- -- flip/flip/noflip
- term.blit(" \x84\x90", "3f1", "443")
- term.setCursorPos(birdX, y+1)
- -- flip/flip/noflip
- term.blit(" \x81", "331", "443")
- end
- local birdY, birdV = math.floor(height / 2), 0
- local gapH, gapL = math.ceil(height * 0.25 + 0.5), math.floor(height * 0.75)
- local pillarGap = math.ceil(width / pillarCount)
- local function drawObjects()
- term.current().setVisible(false)
- term.setBackgroundColor(colors.lightBlue)
- term.clear()
- for i=1, #pillars, 1 do
- drawPillar(pillars[i].x, pillars[i].gap)
- end
- drawBird(math.floor(birdY))
- term.setTextColor(colors.black)
- term.setCursorPos(1, 1)
- term.write("Score: " .. tostring(score))
- term.current().setVisible(true)
- end
- local function tickObjects()
- birdV = birdV - 5*ftTarget
- birdY = birdY - (birdV * ftTarget * 10)
- if #pillars < pillarCount then
- if #pillars == 0 or pillars[#pillars].x < width - pillarGap then
- pillars[#pillars+1] = {x = width, gap = math.random(gapH, gapL)}
- end
- end
- for i=#pillars, 1, -1 do
- pillars[i].x = pillars[i].x - 10*ftTarget
- if pillars[i].x < -1 then
- table.remove(pillars, i)
- elseif pillars[i].x >= birdX and pillars[i].x <= birdX + 1 then
- local tby, bty = pillars[i].gap - gapsize, pillars[i].gap + gapsize
- if birdY < tby or birdY > bty then
- birdV = 2
- repeat
- birdV = birdV - 5*ftTarget
- birdY = birdY - (birdV * ftTarget * 10)
- drawObjects()
- os.sleep(ftTarget)
- until birdY > height + 10
- if old then term.redirect(old) end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- error("died; score " .. score, 0)
- elseif pillars[i].x == birdX then
- score = score + 1
- end
- end
- end
- end
- -- target 10fps
- local tid
- local function tickInput()
- local sig, id, x, y = os.pullEvent()
- if sig == "timer" and id == tid then
- tid = os.startTimer(ftTarget)
- tickObjects()
- elseif sig == "mouse_click" or sig == "monitor_touch" or
- (sig == "key" and id == keys.space) then
- birdV = 2
- end
- end
- tickObjects()
- drawObjects()
- term.setTextColor(colors.black)
- term.setCursorPos(3, 3)
- term.write("press space, click, or tap")
- repeat
- local sig, id = os.pullEventRaw()
- until (sig == "key" and id == keys.space) or sig == "mouse_click"
- or sig == "monitor_touch"
- tid = os.startTimer(0.1)
- birdV = 2
- while true do
- tickInput()
- drawBird(math.floor(birdY))
- drawObjects()
- end
Add Comment
Please, Sign In to add comment