Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Flappy Bird
- function setup() --runs once
- 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
- highscore=0 --highscore starts at 0
- timer=0 --timer starts at 0
- cloudFrequency=1/1.5 --sets how often clouds come on the screen
- C={} --table to hold clouds
- gameState=1 --means the game starts with the first menu where it says "touch anywhere to begin"
- 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
- birdJump=12 --initial height of the bird's jump
- pipeFrequency=(1/2.75) --sets how often pipes come on the screen
- P={} --table to hold pipes
- end
- function LaunchCloud() --inserts a cloud into the table of clouds whenever this runs
- c={} -- table to hold details of new cloud
- c.x=WIDTH+75 --cloud starts off the right side of the screen
- c.y=math.random(100,700) --cloud will be at a random y position
- c.s=-2 --the cloud moves 2 pixels to the left every frame
- c.i=readImage("Platformer Art:Cloud 2") --sets image for clouds
- table.insert(C,c) --inserts the cloud with details "c" into the table of all clouds called "C"
- end --ends launch cloud function
- function LaunchPipe() --inserts a pipe into the table of pipes whenever this runs
- p={} --table to hold details of new pipe
- p.bx=WIDTH --bottom part starts at the right side of the screen
- p.tx=WIDTH --top part starts at the right side of the screen
- p.by=0 --bottom of the bottom part of the pipe is at the bottom of the screen
- p.w= 100 --width of all pipes is 100 pixels
- p.bh=math.random(100,550) --the bottom part has a random height
- p.ty=p.bh+200 --the gap between the two parts of the pipes will always be 200 pixels wide
- p.th=HEIGHT-p.ty --the top part will go up to the top of the screen
- p.s=3 --the pipes will move 3 pixels per frame
- p.c=color(30, 145, 39, 255) --sets color of all pipes
- table.insert(P,p) --inserts a pipe with details "p" into the table of all pipes called "P"
- end --enda launch pipe function
- function draw() --runs 60 times per second
- background(71, 151, 217, 255) --the background is always this blue color
- --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
- timer=timer+DeltaTime --each frame the timer increases by the amount of time since the last frame
- 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
- end
- for i,c in pairs(C) do --creates a loop in the table of clouds
- c.x=c.x+c.s --makes it so the cloud will move because the x position is changing by the speed every frame
- 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
- sprite(c.i,c.x,c.y) --draws the cloud image
- sprite(birdstandingimg,birdX,birdY,80,60) --draw the monster again so it is in front of the clouds
- end --ends loop
- fontSize(48) --sets the size 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 --ends first menu
- --playing
- if gameState==2 then --if it is in gameplay mode, then the bird starts falling
- table.remove(C,i) --takes all the clouds away
- 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
- font("Optima-ExtraBlack") --sets new font for score
- text(score,WIDTH/2,700) --shows score at the top of the screen
- timer=timer+DeltaTime --each frame the timer increases by the amount of time since the last frame
- 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
- end
- for i,p in pairs(P) do --creates a loop in the table of pipes
- 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
- 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
- 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
- end
- fill(p.c) --sets the color of the pipe (determined in LaunchPipe function)
- rect(p.bx,p.by,p.w,p.bh) --draws the bottom part of the pipe
- rect(p.tx,p.ty,p.w,p.th) --draws the top part of the pipe
- 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
- fill(255,226,0,255) --sets color of score
- text(score,WIDTH/2,700) --draws the score at the top of the screen again so it is in front of the pipes
- --collision detection
- if birdX+40>=p.bx and birdX+40<=p.bx+p.w and birdY-40<=p.bh then
- gameState=3
- end --if the monster plus its radius is within the bottom rectangle (pipe), then go into game over menu
- if birdX+40>=p.tx and birdX+40<=p.tx+p.w and birdY+25>=p.ty then
- gameState=3
- end --if the monster plus its radius is within the top rectangle (pipe) then go into game over menu
- end
- 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
- end
- end --end pf gameplay mode
- --game over menu
- if gameState==3 then --if it's 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 and then while it is falling
- table.remove(P,i) --removes all pipes from the table and from the screen
- 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) --draws the blue rectangle over the score
- 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),700) --displays final score at top of screen
- if score>=highscore then highscore=score --if the score is higher than the previous high score, then the high score becomes that score
- text("New high score!",WIDTH/2,600) --if you get a new high score then it will say that in the game over menu
- end
- text("High score: "..highscore,WIDTH/2,500) --says what the high score is at the end of the game
- 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 of touch if statement
- end --end of beginning menu if statement
- if gameState==2 then
- 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"
- birdJump=birdJump+(1/40) --makes the game harder because the jump height increases with every jump
- end --end of jump if statement
- end --end of gameplay if statement
- 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 --resets bird position settings
- birdY=HEIGHT/2
- timer=0 --restarts timer
- birdJump=12 --resets height of jump back to its starting height
- score=0 --sets score back to zero
- end --end of touch if statement
- end --end of game over if statement
- end --end of touched function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement