Flappy Bird Game Using React JS
Last Updated :
24 Jul, 2024
The game Flappy Bird has gained popularity with millions of people, from, all over the world playing it. In this game, players take control of a bird that needs to navigate between pipes while flying. The bird cannot get crashed into the pipes or the ground. If the bird does, then the game is over. In this article we will learn how we can create it using ReactJS.
Preview of the project: Let us have a look at how the final output will look like.

Prerequisites
Approach:
- At the start of the game we set the position of the bird, create an array for pipes and establish the initial game state.
- During gameplay if the user inputs a command to make the bird jump and if the game is still ongoing we update its position accordingly.
- We also have collision detection in place which updates the score and checks for conditions that could lead to a game over.
- Gravity plays a role in determining how the birds position changes over time.
- To keep things interesting pipes are. Move from right to left on the screen.
- When the game is over we make sure to clear any intervals that were set up during gameplay to prevent any memory leaks.
- Finally, based on aspects of our game state such as whether its ongoing or over we render different components including displaying visuals, for birds and pipes as well as showing appropriate messages when its game over.
Steps to Create the Project
Step 1: Create a ReactJS project:
npx create-react-app flappy-bird-game
Step 2: Navigate to the project
cd flappy-bird-game
Project Structure:
The project structure of the flappy bird gameThe updated dependencies in package.json file will look like:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Images used:
Bird: https://media.geeksforgeeks.org/wp-content/uploads/20231211115925/flappy_bird_by_jubaaj_d93bpnj.gif
Pipes: https://media.geeksforgeeks.org/wp-content/uploads/20231211115753/6d2a698f31595a1.png
Example: Write the following code in respective files:
- App.js: This file imports all the components and contains the collision and scoring logic.
- Bird.js: This file is used to render bird image and set its position.
- Pipes.js: This file renders pipes on the screen.
- App.css: This file contains the styling of the application
CSS
/* src/App.css */
.App {
position: relative;
width: 600px;
height: 600px;
border: 1px solid #000;
overflow: hidden;
background-color: #87ceeb;
/* Sky Blue */
transition: background-color 0.5s ease;
}
.game-over {
background-color: #ff6347;
/* Tomato */
}
.game-over-message {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 24px;
font-weight: bold;
color: white;
}
.bird {
position: absolute;
width: 50px;
height: 50px;
user-select: none;
}
.pipe {
position: absolute;
width: 100px;
height: 600px;
}
JavaScript
// App.js
import React, { useState, useEffect } from 'react';
import Bird from './components/Bird';
import Pipes from './components/Pipes';
import './App.css';
const App = () => {
const [birdPosition, setBirdPosition] = useState({ x: 50, y: 200 });
const [pipes, setPipes] = useState([]);
const [gameOver, setGameOver] = useState(false);
const [score, setScore] = useState(0);
const [gameStarted, setGameStarted] = useState(false);
const jump = () => {
if (!gameOver && gameStarted) {
setBirdPosition((prev) => ({ ...prev, y: prev.y - 60 }));
} else if (!gameOver && !gameStarted) {
// Start the game on the first jump
setGameStarted(true);
} else {
// Restart the game
setBirdPosition({ x: 50, y: 200 });
setPipes([]);
setGameOver(false);
setGameStarted(true);
}
};
const checkCollision = () => {
const birdTop = birdPosition.y;
const birdBottom = birdPosition.y + 50;
const birdLeft = birdPosition.x;
const birdRight = birdPosition.x + 50;
pipes.forEach((pipe) => {
const pipeTop = pipe.y;
const pipeBottom = pipe.y + 600;
const pipeLeft = pipe.x;
const pipeRight = pipe.x + 100;
const isColliding =
birdRight > pipeLeft &&
birdLeft < pipeRight &&
birdBottom > pipeTop &&
birdTop < pipeBottom;
if (isColliding) {
if (birdLeft > pipeLeft && birdRight < pipeRight && birdBottom < pipeBottom) {
// Bird has crashed through the pipe, increase score
setScore((prevScore) => prevScore + 1);
} else {
// Bird has hit the pipe, end the game
setGameOver(true);
setGameStarted(false);
}
}
});
// Check if bird is out of the screen vertically
if (birdBottom > 800 || birdTop < -170) {
// Bird is out of bounds, end the game
setGameOver(true);
setGameStarted(false);
}
};
useEffect(() => {
checkCollision();
}, [birdPosition, pipes, gameOver]);
useEffect(() => {
const gravity = setInterval(() => {
setBirdPosition((prev) => ({ ...prev, y: prev.y + 5 }));
checkCollision();
}, 30);
const pipeGenerator = setInterval(() => {
if (!gameOver && gameStarted) {
setPipes((prev) => [
...prev,
{ x: 400, y: Math.floor(Math.random() * 300) },
]);
}
}, 2000);
const pipeMove = setInterval(() => {
if (!gameOver && gameStarted) {
setPipes((prev) =>
prev.map((pipe) => ({ ...pipe, x: pipe.x - 5 }))
);
}
}, 30);
return () => {
clearInterval(gravity);
clearInterval(pipeGenerator);
clearInterval(pipeMove);
};
}, [gameOver, gameStarted]);
return (
<div className={`App ${gameOver ? 'game-over' : ''}`} onClick={jump}>
<Bird birdPosition={birdPosition} />
{pipes.map((pipe, index) => (
<Pipes key={index} pipePosition={pipe} />
))}
{gameOver && (
<center>
<div className="game-over-message">
Game Over!
<br />
<p style={{ backgroundColor: 'blue', padding: "2px 6px", borderRadius: '5px' }}>Click anywhere to Restart</p>
</div>
</center>
)}
</div>
);
};
export default App;
JavaScript
// Bird.js
import React from "react";
const Bird = ({ birdPosition }) => {
return (
<img
src={"https://media.geeksforgeeks.org/wp-content/uploads/20231211115925/flappy_bird_by_jubaaj_d93bpnj.gif"}
alt="bird"
className="bird"
style={{
left: birdPosition.x,
top: birdPosition.y,
}}
draggable={true}
/>
);
};
export default Bird;
JavaScript
// Pipes.js
import React from "react";
const Pipes = ({ pipePosition }) => {
return (
<img
src={'https://media.geeksforgeeks.org/wp-content/uploads/20231211115753/6d2a698f31595a1.png'}
alt="pipe"
className="pipe"
style={{
left: pipePosition.x,
top: pipePosition.y,
}}
draggable={true}
/>
);
};
export default Pipes;
Output:
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read