Guest User

FlappyPixel

a guest
Jan 22nd, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.87 KB | None | 0 0
  1. ------------FlappyBird v1.0----------
  2. ----------------Game-----------------
  3. --------------by Creator-------------
  4.  
  5. --Variables--
  6. term.redirect(term.native())
  7. local w,h = term.getSize()
  8. local dead = false
  9. local defaultConfing = {
  10.     birdColor = colors.yellow,
  11.     bgColor = colors.lightBlue,
  12.     grassColor = colors.green,
  13.     scoreColor = colors.red,
  14.     obstacleColor = colors.blue
  15. }
  16. local obstacles = {}
  17. local currObstacle = 0
  18. local totalLenght = 0
  19. local howFar = 0
  20.  
  21. --PhysicsVars--
  22. local position = math.floor(h/2)
  23. local G = 30
  24. local maxSpeed = 20
  25. local speed = maxSpeed
  26. local position = 1
  27. local totalTime = 0
  28. local diff = 0.05
  29. local xPos = math.floor(w/6)
  30.  
  31. --Functions--
  32. local GUI = {}
  33. local Physics = {}
  34. local Events = {}
  35.  
  36. --GUI related functions--
  37.  
  38. function GUI.DrawBird()
  39.     term.setCursorPos(xPos,h-position)
  40.     term.setBackgroundColor(defaultConfing.birdColor)
  41.     term.write("@")
  42. end
  43.  
  44. function GUI.DrawBackground()
  45.     term.setBackgroundColor(defaultConfing.bgColor)
  46.     term.clear()
  47.     term.setCursorPos(1,1)
  48.     term.setTextColor(defaultConfing.scoreColor)
  49.     term.write("You have passed "..tostring(currObstacle).." obstacles!")
  50.     paintutils.drawLine(1,h,w,h,defaultConfing.grassColor)
  51. end
  52.  
  53. function GUI.DrawObstacles()
  54.     for i,v in pairs(obstacles) do
  55.         local totalFar = v[1] + howFar
  56.         if 0 < totalFar and totalFar <= w then
  57.             paintutils.drawLine(totalFar,1,totalFar,v[3],defaultConfing.obstacleColor)
  58.             paintutils.drawLine(totalFar,v[2]+v[3]-1,totalFar,h-1,defaultConfing.obstacleColor)
  59.             if totalFar == xPos then
  60.                 currObstacle = currObstacle + 1
  61.                 if (1 <= h - position and h - position <= v[3]) or (v[2]+v[3]-1 <= h - position and h - position <= h-1) then
  62.                     dead = true
  63.                 end
  64.             end
  65.         end
  66.        
  67.     end
  68. end
  69.  
  70. --Physics related functions--
  71.  
  72. function Physics.BirdMove()
  73.     position = position + speed*diff - 0.5*G*math.pow( diff, 2 )
  74.     speed = speed - G*diff
  75. end
  76.  
  77. function Physics.KeepInBoundaries()
  78.     if position <= 0 then
  79.         dead = true
  80.     elseif position > h-2 then
  81.         speed = 0
  82.         position = h-2
  83.     end
  84. end
  85.  
  86. function Physics.GenerateObstacles(howMany)
  87.     for i=1,howMany do
  88.         totalLenght = totalLenght + math.random(35,70)
  89.         local opening = math.random(5,15)
  90.         local whereOpen = math.random(1,h-opening)
  91.         obstacles[#obstacles + 1] = {totalLenght,opening,whereOpen}
  92.     end
  93. end
  94.  
  95. function Events.main()
  96.     local timer = os.startTimer(.2)
  97.     local event, p2 = os.pullEvent()
  98.     if event == "key" and p2 == keys.space then
  99.         os.cancelTimer(timer)
  100.         speed = maxSpeed
  101.     end
  102. end
  103.  
  104. --Code--
  105.  
  106. --[[f = fs.open("FlappyBirdConfig/configs","r")
  107. configurationBuffer = f.readAll()
  108. f.close()]]--
  109.  
  110. Physics.GenerateObstacles(100)
  111.  
  112. while true do
  113.     if dead == true then
  114.         error("you died")
  115.     end
  116.     Events.main()
  117.     GUI.DrawBackground()
  118.     GUI.DrawBird()
  119.     GUI.DrawObstacles()
  120.     Physics.BirdMove()
  121.     Physics.KeepInBoundaries()
  122.     --Physics.DetectColision()
  123.     howFar = howFar - 1
  124. end
Add Comment
Please, Sign In to add comment