Tutorial: Create a JavaScript Pong Game in Replit

javascript tutorial

Tutorial Overview

We’ll be building a Pong game from scratch, covering all stages from setting up your project on Replit to expanding the game’s features. The tutorial is structured as follows:

  1. Getting Started with Replit
  2. Introduction to JavaScript
  3. Building the Pong Game
    • Drawing the game board
    • Adding paddles and ball
    • Moving the paddles
    • Ball movement and collision detection
  4. Adding Score and Restarting the Game
  5. Expanding Your Game
  6. Conclusion with Complete Code

Getting Started with Replit

Creating an Account on Replit

Setting Up Your Project

  • Once logged in, create a new “HTML, CSS, JS” project, naming it “Pong Game”.

Introduction to JavaScript

We’ll briefly go over JavaScript basics that we’ll use in our game:

  • Variables: let score = 0;
  • Functions: function moveBall() { /* code */ }
  • Event Listeners: document.addEventListener('keydown', function(event) { /* code */ });

Building the Pong Game

Drawing the Game Board

  1. HTML (index.html):
<!DOCTYPE html>
<html>
<head>
    <title>Pong Game</title>
    <style>
        canvas { background: #000; display: block; margin: auto; }
    </style>
</head>
<body>
    <canvas id="pongCanvas" width="600" height="400"></canvas>
    <script src="script.js"></script>
</body>
</html>
  1. JavaScript (script.js):
  • Initializing canvas:
const canvas = document.getElementById('pongCanvas');
const ctx = canvas.getContext('2d');

Adding Paddles and Ball

  • Define paddles and ball:
const paddleWidth = 10, paddleHeight = 60;
let leftPaddleY = (canvas.height - paddleHeight) / 2, rightPaddleY = (canvas.height - paddleHeight) / 2;
let ballX = canvas.width / 2, ballY = canvas.height / 2;
let ballSpeedX = 2, ballSpeedY = 2;
  • Drawing function:
function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    // Draw paddles
    ctx.fillStyle = 'white';
    ctx.fillRect(0, leftPaddleY, paddleWidth, paddleHeight);
    ctx.fillRect(canvas.width - paddleWidth, rightPaddleY, paddleWidth, paddleHeight);
    // Draw ball
    ctx.beginPath();
    ctx.arc(ballX, ballY, 10, 0, Math.PI*2, false);
    ctx.fill();
    ballX += ballSpeedX;
    ballY += ballSpeedY;
}
setInterval(draw, 10);

Moving the Paddles

  • Add event listeners for keyboard inputs:
document.addEventListener('keydown', function(event) {
    if(event.keyCode == 38) { // Up arrow
        rightPaddleY -= 20;
    } else if(event.keyCode == 40) { // Down arrow
        rightPaddleY += 20;
    }
});

Ball Movement and Collision Detection

  • Add basic collision detection to draw() function:
// Invert ball direction when hitting top/bottom walls
if(ballY + 10 >= canvas.height || ballY - 10 <= 0) ballSpeedY = -ballSpeedY;

// Reset ball to center if it goes past paddles
if(ballX + 10 >= canvas.width || ballX - 10 <= 0) {
    ballX = canvas.width / 2;
    ballSpeedX = -ballSpeedX;
}

Adding Score and Restarting the Game

  • Adding score variables and display:
let leftScore = 0, rightScore = 0;

function draw() {
    // Previous draw function content

    // Display scores
    ctx.font = "32px Arial";
    ctx.fillText(leftScore, 100, 50);
    ctx.fillText(rightScore, canvas.width - 100, 50);
}

Expanding Your Game

  • Ideas to expand your game:
    • Increase ballSpeedX and ballSpeedY gradually to make the game harder.
    • Add sounds using the Audio object for more immersive gameplay.

Conclusion with Complete Code

In conclusion, you
have learned how to build a simple Pong game using HTML, CSS, and JavaScript on Replit. Here’s the complete code for your game:

<!DOCTYPE html>
<html>
<head>
    <title>Pong Game</title>
    <style>
        canvas { background: #000; display: block; margin: auto; }
    </style>
</head>
<body>
    <canvas id="pongCanvas" width="600" height="400"></canvas>
    <script src="script.js"></script>
</body>
</html>

JavaScript (script.js):

const canvas = document.getElementById('pongCanvas');
const ctx = canvas.getContext('2d');

const paddleWidth = 10, paddleHeight = 60;
let leftPaddleY = (canvas.height - paddleHeight) / 2, rightPaddleY = (canvas.height - paddleHeight) / 2;
let ballX = canvas.width / 2, ballY = canvas.height / 2;
let ballSpeedX = 2, ballSpeedY = 2;
let leftScore = 0, rightScore = 0;

document.addEventListener('keydown', function(event) {
    if(event.keyCode == 38) { // Up arrow
        rightPaddleY -= 20;
    } else if(event.keyCode == 40) { // Down arrow
        rightPaddleY += 20;
    }
});

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    // Draw paddles
    ctx.fillStyle = 'white';
    ctx.fillRect(0, leftPaddleY, paddleWidth, paddleHeight);
    ctx.fillRect(canvas.width - paddleWidth, rightPaddleY, paddleWidth, paddleHeight);
    // Draw ball
    ctx.beginPath();
    ctx.arc(ballX, ballY, 10, 0, Math.PI*2, false);
    ctx.fill();
    
    // Ball movement
    ballX += ballSpeedX;
    ballY += ballSpeedY;

    // Collision detection
    if(ballY + 10 >= canvas.height || ballY - 10 <= 0) ballSpeedY = -ballSpeedY;
    if(ballX + 10 >= canvas.width) {
        leftScore += 1;
        resetBall();
    } else if(ballX - 10 <= 0) {
        rightScore += 1;
        resetBall();
    }

    // Display scores
    ctx.font = "32px Arial";
    ctx.fillText(leftScore, 100, 50);
    ctx.fillText(rightScore, canvas.width - 100, 50);
}

function resetBall() {
    ballX = canvas.width / 2;
    ballY = canvas.height / 2;
    ballSpeedX = -ballSpeedX;
    ballSpeedY = 3;
}

setInterval(draw, 10);

SHARE WITH FRIENDS >

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

After-school Scratch classes in Phoenix: schedules, age groups, and what your child will learn

After-school Live Virtual Scratch & Block Coding Classes For Kids, Phoenix

20 Apr 2026

Phoenix coding classes for kids: Compare live virtual after-school Scratch & block coding options

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Accelerated Math & Enrichment Programs for Gifted Expat Students in Dubai (Pre‑IB / AP / Early College Math)

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

FAQ: Everything Dubai parents ask about virtual North American math tutors (credentials, safety, results, booking)

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Pricing, trials and guarantees: What Dubai parents should expect for premium North American math tutors

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Parent guide to scheduling virtual math tutoring in Dubai: best times, timezone tips and tech checklist

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Success stories: Dubai expat students who improved grades and SAT/AP scores with virtual North American tutors

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

Choosing Between One‑on‑One vs Small‑Group Virtual Math Tutoring for Expat Kids in Dubai

Dubai, Dubai Coding and math, Dubai, UAE coding and math, Online Math Tutoring (US & Canadian Curriculum) For Expat Families

20 Apr 2026

How Our Lessons Align with US Common Core and Ontario/BC Curricula — Sample Math Scope & Sequence by Grade