VOOZH about

URL: https://www.geeksforgeeks.org/reactjs/create-a-snake-game-in-react/

⇱ Create a Snake Game in React - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Create a Snake Game in React

Last Updated : 17 Jul, 2025

Snake Game using ReactJS project implements functional components and manages the state accordingly. The developed Game allows users to control a snake using the arrow keys or touch the buttons displayed on the screen to collect food and grow in length. The goal of the game is to eat as much food as possible without colliding with the walls or the snake's own body.

Let's have a quick look at what the final application will look like:

👁 Image

Steps to create Snake Game:

Step 1: Set up the React project using the below command in VSCode IDE.

npm create vite@latest Snake_Game

Step 2: Navigate to the newly created project folder by executing the below command.

cd Snake_Game

Step 3: Create a folder named Components. We will create various components and their styling files in this components folder such as Button.js, Food.js, Menu.js, Snake.js, Menu.css, and Button.css.

Folder Structure:

👁 Image

The updated dependencies in package.json will look like:

"dependencies": {
"react": "^19.1.0",
"react-dom": "^19.1.0",
},
"devDependencies": {
"@vitejs/plugin-react": "^4.6.0",
"vite": "^7.0.4"
}

Example: Insert the below code in the App.js ad index.css files mentioned in the above directory structure.

Write the following mentioned code in different files(The name of the files is mentioned in the first line of each code block).

  • Button.js: Button.js represents a React functional component that renders buttons for controlling the snake's movement in the Snake Game.
  • Menu.js: Menu.js file code renders a menu for the Snake Game. It displays a "Start Game" button and triggers the onRouteChange function when clicked. The menu is styled using CSS from the "Menu.css" file
  • Food.js: Food.js is a React component that renders the food item in a game based on the provided coordinates.
  • Snake.js: Snake.js file code is a React component that renders the snake in a game based on an array of coordinates representing its dots.

Steps to run the application:

  • Execute the following command in the terminal.
npm run dev
  • Open the web browser and type the following URL in the address bar.
http://localhost:5173/

Output:

Comment