Open In App

Flappy Bird Game Using React JS

Last Updated : 24 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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.

ann1

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:

Screenshot-(1380)
The project structure of the flappy bird game

The 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 JavaScript JavaScript

Output:


Similar Reads

three90RightbarBannerImg