VOOZH about

URL: https://www.geeksforgeeks.org/reactjs/ping-pong-game-using-react/

⇱ Ping Pong Game using React - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ping Pong Game using React

Last Updated : 23 Jul, 2025

Ping Pong is one of the earliest video games. It's a two-player game in which each player controls the paddle by dragging it from one side of the screen to the other to strike the ball back and forth. In this article, you will see how you can create a simple but exciting game of ping pong using React.

Preview of the project: Let us have a look at how the final application will look like.👁 ping

Prerequisites

Approach to create Ping Pong Game:

  • To create a game interface and to handle the state, we will use React components.
  • The arrow keys and the "w" and "d" keys are used by the players to control the paddles.
  • inside the playing field, paddles can move upwards and downwards.
  • When the ball hits a paddle it goes off the top and bottom of the wall.
  • When the ball comes to either side of the screen, the game is over.
  • We can start, restar and stop the game with the helps of button provided.
  • We are able to detect the ball collision with paddle or wall with the help of DOM element boundingClientRect properties.

Steps to Create the Project:

Step 1: Create a ReactJS project:

npm create vite@latest ping-pong-game --template react

Step 2: Navigate to the project:

cd ping-pong-game

Step 3: Start the project:

npm run dev 

Project Structure:

👁 Screenshot-(1399)
The project structure of the code

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"
}

Example : Write the following code in App.js file

Output:

Comment