Advertisement
Guest User

Untitled

a guest
Jul 5th, 2016
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 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. Physics.GenerateObstacles(100)
  105.  
  106. while true do
  107. if dead == true then
  108. local message = "You lost! Score: " .. currObstacle
  109. local x, y = term.getSize()
  110. local xtoprint = (x-(x/2)) - (string.len(message)/2)
  111. local ytoprint = y-(y/2)
  112. term.clear()
  113. term.setCursorPos(xtoprint, ytoprint)
  114. print(message)
  115. sleep(3)
  116. term.setBackgroundColor(colors.black)
  117. term.clear()
  118. break
  119. end
  120. Events.main()
  121. GUI.DrawBackground()
  122. GUI.DrawBird()
  123. GUI.DrawObstacles()
  124. Physics.BirdMove()
  125. Physics.KeepInBoundaries()
  126. howFar = howFar - 1
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement