Advertisement
Guest User

Flappy Bird

a guest
Nov 14th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.07 KB | None | 0 0
  1.  
  2. --# Main
  3. -- Flappy Bird
  4.  
  5. function setup()
  6. displayMode(FULLSCREEN) --sets display to full screen
  7. birdX=WIDTH/3 --the x position of the bird will be a third of the screen's width
  8. birdY=HEIGHT/2 --the bird will be half way up the screen
  9. birdSpeed=-(1/4)--the bird starts falling at a rate of 1/4 pixel every time the draw function runs
  10. score=0 --score starts at 0
  11. timer=0 --timer starts at 0
  12. gameState=1 --means the game starts with the first menu where it says "touch anywhere to begin"
  13. cloud1X=WIDTH --the first cloud starts all the way on the right side of the screen
  14. cloud1Y=math.random(100,550)--first cloud starts at a random y position
  15. cloud1Speed=-2 --first cloud will move to the left 2 pixels every time draw function runs
  16. cloud1img=readImage("Platformer Art:Cloud 2")--gives image for first cloud
  17. cloud2X=WIDTH --second cloud starts all the way on the right side of the screen
  18. cloud2Y=math.random(100,550) --second cloud starts at random y position
  19. cloud2Speed=-3 --second cloud will move to the left 3 pixels every time draw function runs
  20. cloud2img=readImage("Platformer Art:Cloud 3") --gives image for second cloud
  21. birdstandingimg=readImage("Platformer Art:Monster Standing") --a picture of the monster standing still, used in first menu
  22. birdmovingimg=readImage("Platformer Art:Monster Moving")--a picture of the monster moving, used in playing mode
  23. deadbirdimg=readImage("Platformer Art:Monster Squished")--a picture of the monster squished, shows when it hits a pipe
  24.  
  25. pipe1bottomx=WIDTH --bottom and top of pipe will both start on right side of screen
  26. pipe1topx=WIDTH
  27. pipe1bottomy=0 --bottom of bottom part of pipe will be at the bottom of the screen
  28. pipe1width=100 --width of pipe in pixels
  29. pipe1bottomheight=math.random(100,550) --the bottom part of the pipe will have a random height
  30. pipe1topy=pipe1bottomheight+200 --the bottom of the top part of the pipe will be 200 pixels higher than the top of the bottom pipe
  31. pipe1topheight=HEIGHT-pipe1topy --makes it so the height of the top part of the pipe will go to the top of the screen
  32. pipe1speed=3 --the pipe will move across the screen 3 pixels every time draw function runs
  33.  
  34. pipe2bottomx=WIDTH --these are all the same settings as the first pipe - this makes it so later there will be two sets of pipes that will alternate going across the screen
  35. pipe2topx=WIDTH
  36. pipe2bottomy=0
  37. pipe2width=100
  38. pipe2bottomheight=math.random(100,550)
  39. pipe2topy=pipe2bottomheight+200
  40. pipe2topheight=HEIGHT-pipe2topy
  41. pipe2speed=3
  42. end
  43.  
  44. function draw()
  45. background(71, 151, 217, 255)
  46.  
  47. --1st menu
  48. if gameState==1 then
  49. sprite(birdstandingimg,birdX,birdY,80,60) --if it's the first menu, then show the picture of the stationary monster
  50. cloud1X=cloud1X+cloud1Speed --makes it so the cloud will move
  51. sprite(cloud1img,cloud1X,cloud1Y) --draws the first cloud image
  52. cloud2X=cloud2X+cloud2Speed --makes it so the other cloud will move
  53. sprite(cloud2img,cloud2X,cloud2Y) --draws the second cloud image
  54. fontSize(48) --sets the suze of the text
  55. font("Futura-CondensedExtraBold") --sets the font of the text
  56. fill(255,226,0,255) --sets the color of the text
  57. text("Bouncy Monster -",WIDTH/2,700) --writes this text in the middle of the screen at y=700
  58. text("Touch anywhere to begin", WIDTH/2,650)--writes this text just below the last line of text
  59. end
  60.  
  61. --playing
  62. if gameState==2 then --if it is in gameplay mode, then the bird starts falling
  63. birdSpeed=birdSpeed-1 --makes the bird accelerate as it falls because the speed increases every frame
  64. birdY=birdY+birdSpeed --makes the y position of the bird decrease to make it seem like it's falling
  65. sprite(birdmovingimg,birdX,birdY,80,60) --draws the picture of the moving monster
  66. timer=timer+(1/60) --makes it so timer will be 1 after 1 second and so on
  67. if timer>=1 then --if 1 second has passed, then launch the first pipe
  68. pipe1bottomx=pipe1bottomx-pipe1speed --makes the bottom part move
  69. fill(30, 145, 39, 255) --sets the color of the pipe
  70. rect(pipe1bottomx,pipe1bottomy,pipe1width,pipe1bottomheight) --draws bottom part of first pipe
  71. pipe1topx=pipe1topx-pipe1speed --makes the top part of the pipe move
  72. rect(pipe1topx,pipe1topy,pipe1width,pipe1topheight) --draws top part of first pipe
  73. end
  74. if pipe1bottomx+pipe1width<=0 then pipe1bottomx=WIDTH pipe1bottomheight=math.random(100,550) --if the bottom part of the pipe is off the left side of the screen, then it restarts back at the right side with a new random height
  75. end
  76. if pipe1topx+pipe1width<=0 then pipe1topx=WIDTH pipe1topy=pipe1bottomheight+200 pipe1topheight=HEIGHT-pipe1topy --if the top part of the pipe is off the left side of the screen, then it restarts back at the right side with a new random height
  77. end
  78.  
  79. if timer>=4.25 then --if 3.25 seconds have passed since the first pipe was launched, then launch the second one
  80. pipe2bottomx=pipe2bottomx-pipe2speed --makes bottom part move
  81. rect(pipe2bottomx,pipe2bottomy,pipe2width,pipe2bottomheight) --draws bottom part of 2nd pipe
  82. pipe2topx=pipe2topx-pipe2speed --makes top part move
  83. rect(pipe2topx,pipe2topy,pipe2width,pipe2topheight) --draws top part of 2nd pipe
  84. if pipe2bottomx+pipe2width<=0 then pipe2bottomx=WIDTH pipe2bottomheight=math.random(100,550)
  85. end --if the bottom part of the pipe is off the left side of the screen, then it restarts back at the right side with a new random height
  86. if pipe2topx+pipe2width<=0 then pipe2topx=WIDTH pipe2topy=pipe2bottomheight+200 pipe2topheight=HEIGHT-pipe2topy
  87. end --if the top part of the pipe is off the left side of the screen, then it restarts back at the right side with a new random height
  88. --when pipe 1 restarts, pipe 2 will still be on the screen so it will give the appearance that there are multiple pipes when really it is just two alternating
  89. end
  90.  
  91. if birdX>=pipe1bottomx and birdX<=pipe1bottomx+2 then score=score+1
  92. end --when the monster passes the first pipe, add a point to the score
  93. if birdX>=pipe2bottomx and birdX<=pipe2bottomx+2 then score=score+1
  94. end --when the monster passes the second pipe, add a point to the score
  95. fill(255,226,0,255) --sets color of text
  96. font("Optima-ExtraBlack") --sets font of text
  97. fontSize(48) --sets size of text
  98. text(score,WIDTH/2,700) --the score will be displayed at the top of the screen in the middle
  99.  
  100. --collision detection
  101. if birdX+40>=pipe1bottomx and birdX+40<=pipe1bottomx+pipe1width and birdY-40<=pipe1bottomheight then
  102. gameState=3
  103. end --if the monster plus its radius is within the 1st bottom rectangle (pipe), then go into game over menu
  104. if birdX+40>=pipe1topx and birdX+40<=pipe1topx+pipe1width and birdY+25>=pipe1topy then
  105. gameState=3
  106. end --if the monster plus its radius is within the 1st top rectangle (pipe) then go into game over menu
  107. if birdX+40>=pipe2bottomx and birdX+40<=pipe2bottomx+pipe2width and birdY-40<=pipe2bottomheight then
  108. gameState=3
  109. end --if the monster plus its radius is within the 2nd bottom rectangle (pipe), then go into game over menu
  110. if birdX+40>=pipe2topx and birdX+40<=pipe2topx+pipe2width and birdY+25>=pipe2topy then
  111. gameState=3
  112. end --if the monster plus its radius is within the 2nd top rectangle (pipe), then go into game over menu
  113.  
  114. if birdY+30<=0 then gameState=3 --if the bird has gotten to the bottom if the screen then it goes to game over menu
  115. end
  116. end --end pf gameplay mode
  117.  
  118. --game over menu
  119. if gameState==3 then --if its in game over menu, then...
  120. birdSpeed=birdSpeed-1 --the monster still falls and accelerates while doing so
  121. birdY=birdY+birdSpeed --makes it appear that the monster is falling
  122. sprite(deadbirdimg,birdX,birdY,100,70) --shows picture of dead monster when it hits the pipe
  123. pipe1bottomx=WIDTH+50 --makes all of the pipes go off the screen
  124. pipe1topx=WIDTH+50
  125. pipe2bottomx=WIDTH+50
  126. pipe2topx=WIDTH+50
  127. if birdY<=0 then --once the dead monster has fallen off the screen, then show the score and the "Try Again" button
  128. fill(71,151,217,255) --covers up yellow score by drawing rectangle same color as background
  129. rect((WIDTH/2)-50,675,100,50)
  130. fill(255, 0, 27, 255) --sets color of button
  131. rect((WIDTH/2)-150,300,300,100) --draws button in the shape of a rectangle
  132. fill(255, 226, 0, 255) --sets color of text
  133. text("Game over! Your score is "..score,(WIDTH/2),600) --displays final score at top of screen
  134. text("Try again",WIDTH/2,350) --says try again inside button
  135. end --ends if statement
  136. end --ends game over menu
  137. end --ends the draw function
  138.  
  139. function touched(touch)
  140. if gameState==1 then
  141. if touch.state==BEGAN then gameState=2 --if the user touches the screen while it's in the first menu, then it goes into gameplay mode
  142. end
  143. end
  144. if gameState==2 then
  145. if touch.state==BEGAN then birdSpeed=15 --if the user touches the screen while it's in gameplay mode, then the bird speed increases by 15 which makes it "jump"
  146. end
  147. end
  148. if gameState==3 then
  149. if touch.x>=(WIDTH/2-150) and touch.x<=(WIDTH/2+150) and touch.y>=300 and touch.y<=400
  150. then gameState=1 --if the user's touch is inside the "Try Again" button then it goes back to the beginning menu
  151. birdX=WIDTH/3 --restarts bird settings
  152. birdY=HEIGHT/2
  153. pipe1bottomx=WIDTH --restarts all pipe settings
  154. pipe1topx=WIDTH
  155. pipe2bottomx=WIDTH
  156. pipe2topx=WIDTH
  157. timer=0 --restarts timer
  158. cloud1X=WIDTH --restarts clouds so they appear in the beginning menu again
  159. cloud2X=WIDTH
  160. score=0 --sets score back to zero
  161. end
  162.  
  163.  
  164. end
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement