Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.54 KB | None | 0 0
  1. -- Flappy Bird
  2.  
  3. function setup() --runs once
  4. displayMode(FULLSCREEN) --sets display to full screen
  5. birdX=WIDTH/3 --the x position of the bird will be a third of the screen's width
  6. birdY=HEIGHT/2 --the bird will be half way up the screen
  7. birdSpeed=-(1/4)--the bird starts falling at a rate of 1/4 pixel every time the draw function runs
  8. score=0 --score starts at 0
  9. highscore=0 --highscore starts at 0
  10. timer=0 --timer starts at 0
  11. cloudFrequency=1/1.5 --sets how often clouds come on the screen
  12. C={} --table to hold clouds
  13. gameState=1 --means the game starts with the first menu where it says "touch anywhere to begin"
  14. birdstandingimg=readImage("Platformer Art:Monster Standing") --a picture of the monster standing still, used in first menu
  15. birdmovingimg=readImage("Platformer Art:Monster Moving")--a picture of the monster moving, used in playing mode
  16. deadbirdimg=readImage("Platformer Art:Monster Squished")--a picture of the monster squished, shows when it hits a pipe
  17. birdJump=12 --initial height of the bird's jump
  18. pipeFrequency=(1/2.75) --sets how often pipes come on the screen
  19. P={} --table to hold pipes
  20.  
  21. end
  22.  
  23. function LaunchCloud() --inserts a cloud into the table of clouds whenever this runs
  24. c={} -- table to hold details of new cloud
  25. c.x=WIDTH+75 --cloud starts off the right side of the screen
  26. c.y=math.random(100,700) --cloud will be at a random y position
  27. c.s=-2 --the cloud moves 2 pixels to the left every frame
  28. c.i=readImage("Platformer Art:Cloud 2") --sets image for clouds
  29. table.insert(C,c) --inserts the cloud with details "c" into the table of all clouds called "C"
  30. end --ends launch cloud function
  31.  
  32. function LaunchPipe() --inserts a pipe into the table of pipes whenever this runs
  33. p={} --table to hold details of new pipe
  34. p.bx=WIDTH --bottom part starts at the right side of the screen
  35. p.tx=WIDTH --top part starts at the right side of the screen
  36. p.by=0 --bottom of the bottom part of the pipe is at the bottom of the screen
  37. p.w= 100 --width of all pipes is 100 pixels
  38. p.bh=math.random(100,550) --the bottom part has a random height
  39. p.ty=p.bh+200 --the gap between the two parts of the pipes will always be 200 pixels wide
  40. p.th=HEIGHT-p.ty --the top part will go up to the top of the screen
  41. p.s=3 --the pipes will move 3 pixels per frame
  42. p.c=color(30, 145, 39, 255) --sets color of all pipes
  43. table.insert(P,p) --inserts a pipe with details "p" into the table of all pipes called "P"
  44. end --enda launch pipe function
  45.  
  46. function draw() --runs 60 times per second
  47. background(71, 151, 217, 255) --the background is always this blue color
  48.  
  49. --1st menu
  50. if gameState==1 then
  51. sprite(birdstandingimg,birdX,birdY,80,60) --if it's the first menu, then show the picture of the stationary monster
  52. timer=timer+DeltaTime --each frame the timer increases by the amount of time since the last frame
  53. if timer>=1/cloudFrequency then LaunchCloud() timer=0 --if the elapsed time is more than the frequency of the clouds (set in setup function) then launch another cloud by going to the LaunchCloud function and reset the timer back to 0
  54. end
  55. for i,c in pairs(C) do --creates a loop in the table of clouds
  56. c.x=c.x+c.s --makes it so the cloud will move because the x position is changing by the speed every frame
  57. if c.x+75<=0 then table.remove(C,i) end --if the full cloud is off the screen then remove it from the table of clouds
  58. sprite(c.i,c.x,c.y) --draws the cloud image
  59. sprite(birdstandingimg,birdX,birdY,80,60) --draw the monster again so it is in front of the clouds
  60. end --ends loop
  61. fontSize(48) --sets the size of the text
  62. font("Futura-CondensedExtraBold") --sets the font of the text
  63. fill(255,226,0,255) --sets the color of the text
  64. text("Bouncy Monster -",WIDTH/2,700) --writes this text in the middle of the screen at y=700
  65. text("Touch anywhere to begin", WIDTH/2,650)--writes this text just below the last line of text
  66. end --ends first menu
  67.  
  68. --playing
  69. if gameState==2 then --if it is in gameplay mode, then the bird starts falling
  70. table.remove(C,i) --takes all the clouds away
  71. birdSpeed=birdSpeed-1 --makes the bird accelerate as it falls because the speed increases every frame
  72. birdY=birdY+birdSpeed --makes the y position of the bird decrease to make it seem like it's falling
  73. sprite(birdmovingimg,birdX,birdY,80,60) --draws the picture of the moving monster
  74.  
  75. font("Optima-ExtraBlack") --sets new font for score
  76. text(score,WIDTH/2,700) --shows score at the top of the screen
  77.  
  78. timer=timer+DeltaTime --each frame the timer increases by the amount of time since the last frame
  79. if timer>1/pipeFrequency then LaunchPipe() timer=0 --if the elapsed time is more than the frequency of the pipes (set in setup function) then launch another pipe by going to the LaunchPipe function and reset the timer back to 0
  80. end
  81.  
  82. for i,p in pairs(P) do --creates a loop in the table of pipes
  83. p.bx=p.bx-p.s --the x position of the bottom part of the pipe moves to the left by the amount of the pipe speed each frame
  84. p.tx=p.tx-p.s --the x position of the top part of the pipe also moves to the left by the amount of the pipe speed each frame
  85. if p.bx+p.w<=0 then table.remove(P,i) --if the entire pipe is off the screen then remove it from the table
  86. end
  87. fill(p.c) --sets the color of the pipe (determined in LaunchPipe function)
  88. rect(p.bx,p.by,p.w,p.bh) --draws the bottom part of the pipe
  89. rect(p.tx,p.ty,p.w,p.th) --draws the top part of the pipe
  90.  
  91. if birdX>=p.bx+97 and birdX<=p.bx+100 then score=score+1 end --if the bird passes through the entire pipe then the score increases by 1
  92. fill(255,226,0,255) --sets color of score
  93. text(score,WIDTH/2,700) --draws the score at the top of the screen again so it is in front of the pipes
  94.  
  95. --collision detection
  96. if birdX+40>=p.bx and birdX+40<=p.bx+p.w and birdY-40<=p.bh then
  97. gameState=3
  98. end --if the monster plus its radius is within the bottom rectangle (pipe), then go into game over menu
  99. if birdX+40>=p.tx and birdX+40<=p.tx+p.w and birdY+25>=p.ty then
  100. gameState=3
  101. end --if the monster plus its radius is within the top rectangle (pipe) then go into game over menu
  102. end
  103.  
  104. if birdY+30<=0 then gameState=3 --if the entire bird has gotten to the bottom if the screen then it goes to game over menu
  105. end
  106. end --end pf gameplay mode
  107.  
  108. --game over menu
  109. if gameState==3 then --if it's in game over menu, then...
  110. birdSpeed=birdSpeed-1 --the monster still falls and accelerates while doing so
  111. birdY=birdY+birdSpeed --makes it appear that the monster is falling
  112. sprite(deadbirdimg,birdX,birdY,100,70) --shows picture of dead monster when it hits the pipe and then while it is falling
  113. table.remove(P,i) --removes all pipes from the table and from the screen
  114. if birdY<=0 then --once the dead monster has fallen off the screen, then show the score and the "Try Again" button
  115. fill(71,151,217,255) --covers up yellow score by drawing rectangle same color as background
  116. rect((WIDTH/2)-50,675,100,50) --draws the blue rectangle over the score
  117. fill(255, 0, 27, 255) --sets color of button
  118. rect((WIDTH/2)-150,300,300,100) --draws button in the shape of a rectangle
  119. fill(255, 226, 0, 255) --sets color of text
  120. text("Game over! Your score is "..score,(WIDTH/2),700) --displays final score at top of screen
  121.  
  122. if score>=highscore then highscore=score --if the score is higher than the previous high score, then the high score becomes that score
  123. text("New high score!",WIDTH/2,600) --if you get a new high score then it will say that in the game over menu
  124. end
  125.  
  126. text("High score: "..highscore,WIDTH/2,500) --says what the high score is at the end of the game
  127. text("Try again",WIDTH/2,350) --says try again inside button
  128. end --ends if statement
  129. end --ends game over menu
  130. end --ends the draw function
  131.  
  132. function touched(touch)
  133. if gameState==1 then
  134. 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
  135. end --end of touch if statement
  136. end --end of beginning menu if statement
  137. if gameState==2 then
  138. if touch.state==BEGAN then birdSpeed=birdJump --if the user touches the screen while it's in gameplay mode, then the bird speed becomes the height of the bird jump, which makes the bird "jump"
  139. birdJump=birdJump+(1/40) --makes the game harder because the jump height increases with every jump
  140. end --end of jump if statement
  141. end --end of gameplay if statement
  142. if gameState==3 then
  143. if touch.x>=(WIDTH/2-150) and touch.x<=(WIDTH/2+150) and touch.y>=300 and touch.y<=400
  144. then gameState=1 --if the user's touch is inside the "Try Again" button then it goes back to the beginning menu
  145. birdX=WIDTH/3 --resets bird position settings
  146. birdY=HEIGHT/2
  147. timer=0 --restarts timer
  148. birdJump=12 --resets height of jump back to its starting height
  149. score=0 --sets score back to zero
  150. end --end of touch if statement
  151. end --end of game over if statement
  152. end --end of touched function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement