The Ping Pong Game JavaScript Code is a web-based game this is like a table tennis-themed video game for two players.
In addition, the Pong Game JavaScript was developed using JavaScript, CSS and HTML.
What is Ping Pong Game In JavaScript
In JavaScript, Ping Pong Game is a simple game which involves two paddles and a moving ball in this game.
The players must maneuver their paddles using arrow key in upwards or downwards in order to keep the ball from hitting the wall. If the ball hits the wall, it is a touchdown for the other team.
This Ping Pong Game JavaScript Code also includes a downloadable for free, just find the downloadable source code below and click to start downloading.
I have here a suggested list of Best JavaScript Projects with Source Code Free to download and I’m sure this can help you to improve your skills in JavaScript programming and web development as a whole.
To start executing a Ping Pong Game, makes sure that you have any platform in creating a JavaScript, CSS, bootstrap, and HTML installed in your computer, in my case I will use Sublime Text.
Ping Pong Game JavaScript Code : Steps on how to create the project
Time needed: 5 minutes
These are the steps on how to create Ping Pong Game JavaScript Code
- Step 1: Create a folder.
First, create a folder and named it.

- Step 2: Open the folder in “Sublime Text”.
Second ,after creating a folder, open it in “sublime text“.

- Step 3: Create a html file.
Third, create a “html” file and save it as “index.html“

The code given below is for the JavaScript module
< script >
let gameState = 'start';
let paddle_1 = document.querySelector('.paddle_1');
let paddle_2 = document.querySelector('.paddle_2');
let board = document.querySelector('.board');
let initial_ball = document.querySelector('.ball');
let ball = document.querySelector('.ball');
let score_1 = document.querySelector('.player_1_score');
let score_2 = document.querySelector('.player_2_score');
let message = document.querySelector('.message');
let paddle_1_coord = paddle_1.getBoundingClientRect();
let paddle_2_coord = paddle_2.getBoundingClientRect();
let initial_ball_coord = ball.getBoundingClientRect();
let ball_coord = initial_ball_coord;
let board_coord = board.getBoundingClientRect();
let paddle_common = document.querySelector('.paddle').getBoundingClientRect();
let dx = Math.floor(Math.random() * 4) + 3;
let dy = Math.floor(Math.random() * 4) + 3;
let dxd = Math.floor(Math.random() * 2);
let dyd = Math.floor(Math.random() * 2);
document.addEventListener('keydown', (e) => {
if (e.key == 'Enter') {
gameState = gameState == 'start' ? 'play' : 'start';
if (gameState == 'play') {
message.innerHTML = 'Game Started';
message.style.left = 42 + 'vw';
requestAnimationFrame(() => {
dx = Math.floor(Math.random() * 4) + 3;
dy = Math.floor(Math.random() * 4) + 3;
dxd = Math.floor(Math.random() * 2);
dyd = Math.floor(Math.random() * 2);
moveBall(dx, dy, dxd, dyd);
});
}
}
if (gameState == 'play') {
if (e.key == 'w') {
paddle_1.style.top = Math.max(board_coord.top, paddle_1_coord.top - window.innerHeight * 0.06) + 'px';
paddle_1_coord = paddle_1.getBoundingClientRect();
}
if (e.key == 's') {
paddle_1.style.top = Math.min(board_coord.bottom - paddle_common.height, paddle_1_coord.top + window.innerHeight * 0.06) + 'px';
paddle_1_coord = paddle_1.getBoundingClientRect();
}
if (e.key == 'ArrowUp') {
paddle_2.style.top = Math.max(board_coord.top, paddle_2_coord.top - window.innerHeight * 0.1) + 'px';
paddle_2_coord = paddle_2.getBoundingClientRect();
}
if (e.key == 'ArrowDown') {
paddle_2.style.top = Math.min(board_coord.bottom - paddle_common.height, paddle_2_coord.top + window.innerHeight * 0.1) + 'px';
paddle_2_coord = paddle_2.getBoundingClientRect();
}
}
});
function moveBall(dx, dy, dxd, dyd) {
if (ball_coord.top <= board_coord.top) {
dyd = 1;
}
if (ball_coord.bottom >= board_coord.bottom) {
dyd = 0;
}
if (ball_coord.left <= paddle_1_coord.right && ball_coord.top >= paddle_1_coord.top && ball_coord.bottom <= paddle_1_coord.bottom) {
dxd = 1;
dx = Math.floor(Math.random() * 4) + 3;
dy = Math.floor(Math.random() * 4) + 3;
}
if (ball_coord.right >= paddle_2_coord.left && ball_coord.top >= paddle_2_coord.top && ball_coord.bottom <= paddle_2_coord.bottom) {
dxd = 0;
dx = Math.floor(Math.random() * 4) + 3;
dy = Math.floor(Math.random() * 4) + 3;
}
if (ball_coord.left <= board_coord.left || ball_coord.right >= board_coord.right) {
if (ball_coord.left <= board_coord.left) {
score_2.innerHTML = +score_2.innerHTML + 1;
} else {
score_1.innerHTML = +score_1.innerHTML + 1;
}
gameState = 'start';
ball_coord = initial_ball_coord;
ball.style = initial_ball.style;
message.innerHTML = 'Press Enter to Play Pong';
message.style.left = 38 + 'vw';
return;
}
ball.style.top = ball_coord.top + dy * (dyd == 0 ? -1 : 1) + 'px';
ball.style.left = ball_coord.left + dx * (dxd == 0 ? -1 : 1) + 'px';
ball_coord = ball.getBoundingClientRect();
requestAnimationFrame(() => {
moveBall(dx, dy, dxd, dyd);
});
} < /script>Project Output:

| ABOUT PROJECT | PROJECT DETAILS |
|---|---|
| Project Name : | Ping Pong Game JavaScript Code |
| Project Platform : | JavaScript |
| Programming Language Used: | JavaScript, HTML, and CSS |
| Developer Name : | itsourcecode.com |
| IDE Tool (Recommended): | Sublime |
| Project Type : | Web Application |
| Database: | None |
| Upload Date: | 9 / 24 /2022 |
Downloadable Source Code
Summary
This JavaScript Project is simply in HTML, CSS, and JavaScript. To start creating this Simple JavaScript Project With Source Code, make sure that you have basic knowledge in HTML, CSS and JavaScript.
This JavaScript Game Project also includes a downloadable JavaScript Game Project With Source Code for free.
Related Articles
- Todo List In JavaScript With Source Code
- Currency Converter Project in JavaScript with Source Code
- Calculator In JavaScript Source Code
- [SOLVED] How To Make A Live Chat In JavaScript With Source Code
Inquiries
If you have any questions or suggestions about Ping Pong Game JavaScript Code, please feel free to leave a comment below.
Frequently Asked Questions
How does this JavaScript game work?
Built with vanilla JS + HTML5 Canvas API. Game loop via requestAnimationFrame. Input via document.addEventListener(‘keydown’). Common: Snake, Tic-Tac-Toe, Tetris, Pong, Hangman, Flappy Bird. Drop-in browser play, no install needed. Foundation BSIT 1st-2nd year mini-project.
What JavaScript runtime or browser does this project need?
Pure frontend projects (vanilla JS, Vue, jQuery) run in any modern browser (Chrome 90+, Firefox 88+, Safari 14+, Edge 90+) – no install needed. Node.js projects need Node.js 18 LTS or 20 LTS (download from nodejs.org). Run: npm install in project folder, then npm start or node app.js.
How do I run this JavaScript project locally?
For pure HTML/CSS/JS: open index.html in your browser, or use VS Code Live Server extension for auto-reload. For Vue projects: npm install then npm run dev (Vite) or npm run serve (Vue CLI). For Node.js: npm install then npm start. For projects with backend (PHP+MySQL): place in XAMPP htdocs, start Apache + MySQL, browse to localhost/project.
Can I use this JavaScript project for a BSIT capstone or thesis?
Yes. Extend it: add user authentication (JWT for SPA, sessions for traditional), role-based access, real backend (Node.js+Express+MongoDB or PHP+MySQL), responsive design (Bootstrap/Tailwind), PDF exports (jsPDF), real-time features (Socket.IO). Pair with Chapter 1-5 documentation matching your panel’s rubric.
Why am I getting ‘Uncaught ReferenceError’ or ‘Cannot read property of undefined’?
Three common JavaScript issues: (1) Script tag placement wrong, put
Looking for similar projects or tutorials?
Search for more source code, capstone projects, or programming tutorials



