Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- Flappy Bird
- function setup()
- displayMode(FULLSCREEN) --sets display to full screen
- birdX=WIDTH/3 --the x position of the bird will be a third of the screen's width
- birdY=HEIGHT/2 --the bird will be half way up the screen
- birdSpeed=-(1/4)--the bird starts falling at a rate of 1/4 pixel every time the draw function runs
- score=0 --score starts at 0
- timer=0 --timer starts at 0
- gameState=1 --means the game starts with the first menu where it says "touch anywhere to begin"
- cloud1X=WIDTH --the first cloud starts all the way on the right side of the screen
- cloud1Y=math.random(100,550)--first cloud starts at a random y position
- cloud1Speed=-2 --first cloud will move to the left 2 pixels every time draw function runs
- cloud1img=readImage("Platformer Art:Cloud 2")--gives image for first cloud
- cloud2X=WIDTH --second cloud starts all the way on the right side of the screen
- cloud2Y=math.random(100,550) --second cloud starts at random y position
- cloud2Speed=-3 --second cloud will move to the left 3 pixels every time draw function runs
- cloud2img=readImage("Platformer Art:Cloud 3") --gives image for second cloud
- birdstandingimg=readImage("Platformer Art:Monster Standing") --a picture of the monster standing still, used in first menu
- birdmovingimg=readImage("Platformer Art:Monster Moving")--a picture of the monster moving, used in playing mode
- deadbirdimg=readImage("Platformer Art:Monster Squished")--a picture of the monster squished, shows when it hits a pipe
- pipe1bottomx=WIDTH --bottom and top of pipe will both start on right side of screen
- pipe1topx=WIDTH
- pipe1bottomy=0 --bottom of bottom part of pipe will be at the bottom of the screen
- pipe1width=100 --width of pipe in pixels
- pipe1bottomheight=math.random(100,550) --the bottom part of the pipe will have a random height
- pipe1topy=pipe1bottomheight+200 --the bottom of the top part of the pipe will be 200 pixels higher than the top of the bottom pipe
- pipe1topheight=HEIGHT-pipe1topy --makes it so the height of the top part of the pipe will go to the top of the screen
- pipe1speed=3 --the pipe will move across the screen 3 pixels every time draw function runs
- 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
- pipe2topx=WIDTH
- pipe2bottomy=0
- pipe2width=100
- pipe2bottomheight=math.random(100,550)
- pipe2topy=pipe2bottomheight+200
- pipe2topheight=HEIGHT-pipe2topy
- pipe2speed=3
- end
- function draw()
- background(71, 151, 217, 255)
- --1st menu
- if gameState==1 then
- sprite(birdstandingimg,birdX,birdY,80,60) --if it's the first menu, then show the picture of the stationary monster
- cloud1X=cloud1X+cloud1Speed --makes it so the cloud will move
- sprite(cloud1img,cloud1X,cloud1Y) --draws the first cloud image
- cloud2X=cloud2X+cloud2Speed --makes it so the other cloud will move
- sprite(cloud2img,cloud2X,cloud2Y) --draws the second cloud image
- fontSize(48) --sets the suze of the text
- font("Futura-CondensedExtraBold") --sets the font of the text
- fill(255,226,0,255) --sets the color of the text
- text("Bouncy Monster -",WIDTH/2,700) --writes this text in the middle of the screen at y=700
- text("Touch anywhere to begin", WIDTH/2,650)--writes this text just below the last line of text
- end
- --playing
- if gameState==2 then --if it is in gameplay mode, then the bird starts falling
- birdSpeed=birdSpeed-1 --makes the bird accelerate as it falls because the speed increases every frame
- birdY=birdY+birdSpeed --makes the y position of the bird decrease to make it seem like it's falling
- sprite(birdmovingimg,birdX,birdY,80,60) --draws the picture of the moving monster
- timer=timer+(1/60) --makes it so timer will be 1 after 1 second and so on
- if timer>=1 then --if 1 second has passed, then launch the first pipe
- pipe1bottomx=pipe1bottomx-pipe1speed --makes the bottom part move
- fill(30, 145, 39, 255) --sets the color of the pipe
- rect(pipe1bottomx,pipe1bottomy,pipe1width,pipe1bottomheight) --draws bottom part of first pipe
- pipe1topx=pipe1topx-pipe1speed --makes the top part of the pipe move
- rect(pipe1topx,pipe1topy,pipe1width,pipe1topheight) --draws top part of first pipe
- end
- 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
- end
- 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
- end
- if timer>=4.25 then --if 3.25 seconds have passed since the first pipe was launched, then launch the second one
- pipe2bottomx=pipe2bottomx-pipe2speed --makes bottom part move
- rect(pipe2bottomx,pipe2bottomy,pipe2width,pipe2bottomheight) --draws bottom part of 2nd pipe
- pipe2topx=pipe2topx-pipe2speed --makes top part move
- rect(pipe2topx,pipe2topy,pipe2width,pipe2topheight) --draws top part of 2nd pipe
- if pipe2bottomx+pipe2width<=0 then pipe2bottomx=WIDTH pipe2bottomheight=math.random(100,550)
- 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
- if pipe2topx+pipe2width<=0 then pipe2topx=WIDTH pipe2topy=pipe2bottomheight+200 pipe2topheight=HEIGHT-pipe2topy
- 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
- --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
- end
- if birdX>=pipe1bottomx and birdX<=pipe1bottomx+2 then score=score+1
- end --when the monster passes the first pipe, add a point to the score
- if birdX>=pipe2bottomx and birdX<=pipe2bottomx+2 then score=score+1
- end --when the monster passes the second pipe, add a point to the score
- fill(255,226,0,255) --sets color of text
- font("Optima-ExtraBlack") --sets font of text
- fontSize(48) --sets size of text
- text(score,WIDTH/2,700) --the score will be displayed at the top of the screen in the middle
- --collision detection
- if birdX+40>=pipe1bottomx and birdX+40<=pipe1bottomx+pipe1width and birdY-40<=pipe1bottomheight then
- gameState=3
- end --if the monster plus its radius is within the 1st bottom rectangle (pipe), then go into game over menu
- if birdX+40>=pipe1topx and birdX+40<=pipe1topx+pipe1width and birdY+25>=pipe1topy then
- gameState=3
- end --if the monster plus its radius is within the 1st top rectangle (pipe) then go into game over menu
- if birdX+40>=pipe2bottomx and birdX+40<=pipe2bottomx+pipe2width and birdY-40<=pipe2bottomheight then
- gameState=3
- end --if the monster plus its radius is within the 2nd bottom rectangle (pipe), then go into game over menu
- if birdX+40>=pipe2topx and birdX+40<=pipe2topx+pipe2width and birdY+25>=pipe2topy then
- gameState=3
- end --if the monster plus its radius is within the 2nd top rectangle (pipe), then go into game over menu
- 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
- end
- end --end pf gameplay mode
- --game over menu
- if gameState==3 then --if its in game over menu, then...
- birdSpeed=birdSpeed-1 --the monster still falls and accelerates while doing so
- birdY=birdY+birdSpeed --makes it appear that the monster is falling
- sprite(deadbirdimg,birdX,birdY,100,70) --shows picture of dead monster when it hits the pipe
- pipe1bottomx=WIDTH+50 --makes all of the pipes go off the screen
- pipe1topx=WIDTH+50
- pipe2bottomx=WIDTH+50
- pipe2topx=WIDTH+50
- if birdY<=0 then --once the dead monster has fallen off the screen, then show the score and the "Try Again" button
- fill(71,151,217,255) --covers up yellow score by drawing rectangle same color as background
- rect((WIDTH/2)-50,675,100,50)
- fill(255, 0, 27, 255) --sets color of button
- rect((WIDTH/2)-150,300,300,100) --draws button in the shape of a rectangle
- fill(255, 226, 0, 255) --sets color of text
- text("Game over! Your score is "..score,(WIDTH/2),600) --displays final score at top of screen
- text("Try again",WIDTH/2,350) --says try again inside button
- end --ends if statement
- end --ends game over menu
- end --ends the draw function
- function touched(touch)
- if gameState==1 then
- 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
- end
- end
- if gameState==2 then
- 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"
- end
- end
- if gameState==3 then
- if touch.x>=(WIDTH/2-150) and touch.x<=(WIDTH/2+150) and touch.y>=300 and touch.y<=400
- then gameState=1 --if the user's touch is inside the "Try Again" button then it goes back to the beginning menu
- birdX=WIDTH/3 --restarts bird settings
- birdY=HEIGHT/2
- pipe1bottomx=WIDTH --restarts all pipe settings
- pipe1topx=WIDTH
- pipe2bottomx=WIDTH
- pipe2topx=WIDTH
- timer=0 --restarts timer
- cloud1X=WIDTH --restarts clouds so they appear in the beginning menu again
- cloud2X=WIDTH
- score=0 --sets score back to zero
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement