# Flappy Bird

I had nothing to do one day after school in 11th grade and decided to use p5.js (processing) to recreate Flappy Bird. Below is a copy of the code as well as a working version of the game. Press Start and use the Return Key to play.

# Code (p5):

// Define Variables

let pipes

let bird

let back

let pipeX1

let pipeX2

let pipeX3

let pipeY1

let pipeY2

let pipeY3

let gravity

let birdY

let up

let score

let startButton

let resetButton

// Load in images

}

function setup() {

// Create start button

startButton = createButton('Start');

startButton.position(19, 19);

// Run start function if start button is pressed

startButton.mousePressed(start);

// Set up screen

createCanvas(400, 600);

// Define x locations of 3 pipes

pipeX1 = 210

pipeX2 = 440

pipeX3 = 680

// Initial speed is 0

speed = 0

// Create random y locations for pipes

pipeY1 = random(-450,-199)

pipeY2 = random(-450,-199)

pipeY3 = random(-450,-199)

// Create starting y position of bird and set gravity to 0

birdY = -400

gravity = 0

up = 0

score = 0

}

// Set speed and gravity to begin game

function start() {

speed = -3

gravity = 4

}

// Reset back to initial state

function reset() {

birdY = -400

speed = -3

gravity = 4

pipeX1 = 210

pipeX2 = 440

pipeX3 = 680

birdY = birdY + gravity

score = 0

}

function die() {

// If you go past x loc of pipe

if (pipeX1 >= -53.5 && pipeX1 <= 30) {

// Check to see if out of the y bounds of pipe

if (birdY <= pipeY1-42 || birdY >= pipeY1+68) {

// end game

speed = 0

gravity = 20

if (keyIsDown(RETURN)) {

birdY = birdY + 16

}

}

}

// Same for second pipe

if (pipeX2 >= -53.5 && pipeX2 <= 30) {

if (birdY <= pipeY2-42 || birdY >= pipeY2+68) {

speed = 0

gravity = 20

if (keyIsDown(RETURN)) {

birdY = birdY + 16

}

}

}

// Same for third pipe

if (pipeX3 >= -53.5 && pipeX3 <= 30) {

if (birdY <= pipeY3-42 || birdY >= pipeY3+68) {

speed = 0

gravity = 20

if (keyIsDown(RETURN)) {

birdY = birdY + 16

}

}

}

}

// Animate screen

function draw() {

die()

if (keyIsDown(UP_ARROW)) {

print('up')

}

image(back, 0,0)

// Make pipes move

pipeX1 += speed

pipeX2 += speed

pipeX3 += speed

// Define the images and locations

image(pipes, pipeX1, pipeY1);

image(pipes, pipeX2, pipeY2)

image(pipes, pipeX3, pipeY3)

// Handle pipes moving off screen

if (pipeX1 < -270) {

pipeX1 = 440

pipeX1 += speed

pipeY1 = random(-450,-199)

}

if (pipeX2 < -270) {

pipeX2 = 440

pipeX2 += speed

pipeY2 = random(-450,-199)

}

if (pipeX3 < -270) {

pipeX3 = 440

pipeX3 += speed

pipeY3 = random(-450,-199)

}

if (keyIsDown(RETURN)) {

birdY = birdY - 8

} else {

birdY = birdY + gravity

}

// Define bird location

image(bird, 25, birdY)

// Handle scoring

if (pipeX1 == -85 || pipeX1 == -86 || pipeX1 == -87)

score = score + 1

if (pipeX2 == -85 || pipeX2 == -86 || pipeX2 == -87)

score = score + 1

if (pipeX3 == -85 || pipeX3 == -86 || pipeX3 == -87)

score = score + 1

// Handle game over --> Print score and show reset button

if (birdY > -34) {

birdY = -34

speed = 0

textSize(32)

image(back,0,0)

image(bird,25,-34)

text('Game Over', 110, 300)

text('Score: ' + score, 110, 340)

resetButton = createButton('Reset');

resetButton.position(19, 19);

resetButton.mousePressed(reset)

} else {

textSize(32)

fill(255,255,255)

text(score, 180, 30)

}

}