Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="styles.css">
  <title>Pong Game</title>
</head>
<body>
  <canvas id="pongCanvas" width="800" height="400"></canvas>
  <script src="script.js"></script>
</body>
</html>
              
            
!

CSS

              
                body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #000; /* Set background color to black */
}

canvas {
  border: 1px solid #fff; /* Set border color to white */
  display: block;
  margin: auto;
  touch-action: manipulation;
}

              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function () {
  const canvas = document.getElementById("pongCanvas");
  const ctx = canvas.getContext("2d");

  const paddleHeight = 60;
  const paddleWidth = 10;
  const ballRadius = 8;
  const initialBallSpeed = 5;

  const halfCanvasWidth = canvas.width / 2;

  let leftPaddleY = (canvas.height - paddleHeight) / 2;
  let rightPaddleY = (canvas.height - paddleHeight) / 2;
  let ballX = canvas.width / 2;
  let ballY = canvas.height / 2;
  let ballSpeedX = initialBallSpeed;
  let ballSpeedY = initialBallSpeed;
  let leftPlayerScore = 0;
  let rightPlayerScore = 0;

  function drawPaddle(x, y) {
    ctx.fillStyle = "#fff";
    ctx.fillRect(x, y, paddleWidth, paddleHeight);
  }

  function drawBall() {
    ctx.beginPath();
    ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2);
    ctx.fillStyle = "#fff";
    ctx.fill();
    ctx.closePath();
  }

  function drawScore() {
    ctx.font = "30px Arial";
    ctx.fillStyle = "#fff";
    ctx.fillText(leftPlayerScore + " - " + rightPlayerScore, canvas.width / 2 - 30, 30);
  }

  function movePaddle(player, direction) {
    const paddleSpeed = 10;
    let paddleY;

    if (player === "left") {
      paddleY = leftPaddleY;
      if (direction === "up") {
        paddleY -= paddleSpeed;
      } else if (direction === "down") {
        paddleY += paddleSpeed;
      }
      if (paddleY < 0) {
        paddleY = 0;
      } else if (paddleY > canvas.height - paddleHeight) {
        paddleY = canvas.height - paddleHeight;
      }
      leftPaddleY = paddleY;
    } else if (player === "right") {
      paddleY = rightPaddleY;
      if (direction === "up") {
        paddleY -= paddleSpeed;
      } else if (direction === "down") {
        paddleY += paddleSpeed;
      }
      if (paddleY < 0) {
        paddleY = 0;
      } else if (paddleY > canvas.height - paddleHeight) {
        paddleY = canvas.height - paddleHeight;
      }
      rightPaddleY = paddleY;
    }
  }

  function update() {
    ballX += ballSpeedX;
    ballY += ballSpeedY;

    if (ballY - ballRadius < 0 || ballY + ballRadius > canvas.height) {
      ballSpeedY = -ballSpeedY;
    }

    if (
      ballX + ballRadius > canvas.width - paddleWidth &&
      ballY > rightPaddleY &&
      ballY < rightPaddleY + paddleHeight
    ) {
      ballSpeedX = -ballSpeedX;
    }

    if (
      ballX - ballRadius < paddleWidth &&
      ballY > leftPaddleY &&
      ballY < leftPaddleY + paddleHeight
    ) {
      ballSpeedX = -ballSpeedX;
    }

    if (ballX + ballRadius > canvas.width) {
      leftPlayerScore++;
      resetBall();
    } else if (ballX - ballRadius < 0) {
      rightPlayerScore++;
      resetBall();
    }
  }

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

  function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // Draw the dividing line
    ctx.fillStyle = "#fff";
    ctx.fillRect(halfCanvasWidth - 2, 0, 4, canvas.height);

    // Draw the paddles and ball
    drawPaddle(0, leftPaddleY);
    drawPaddle(canvas.width - paddleWidth, rightPaddleY);
    drawBall();

    // Draw the score
    drawScore();
  }

  function gameLoop() {
    update();
    draw();
    requestAnimationFrame(gameLoop);
  }

  window.addEventListener("keydown", function (e) {
    e.preventDefault();

    if (e.key === "ArrowUp") {
      movePaddle("left", "up");
    } else if (e.key === "ArrowDown") {
      movePaddle("left", "down");
    }

    if (e.key === "w") {
      movePaddle("right", "up");
    } else if (e.key === "s") {
      movePaddle("right", "down");
    }
  });

  canvas.addEventListener("touchstart", function (e) {
    e.preventDefault();
    const touchY = e.touches[0].clientY - canvas.getBoundingClientRect().top;

    if (e.touches[0].clientX < halfCanvasWidth) {
      // Left side of the canvas
      if (touchY < canvas.height / 2) {
        movePaddle("left", "up");
      } else {
        movePaddle("left", "down");
      }
    } else {
      // Right side of the canvas
      if (touchY < canvas.height / 2) {
        movePaddle("right", "up");
      } else {
        movePaddle("right", "down");
      }
    }
  });

  canvas.addEventListener("touchmove", function (e) {
    e.preventDefault();
    const touchY = e.touches[0].clientY - canvas.getBoundingClientRect().top;

    if (e.touches[0].clientX < halfCanvasWidth) {
      // Left side of the canvas
      if (touchY < leftPaddleY + paddleHeight / 2) {
        movePaddle("left", "up");
      } else {
        movePaddle("left", "down");
      }
    } else {
      // Right side of the canvas
      if (touchY < rightPaddleY + paddleHeight / 2) {
        movePaddle("right", "up");
      } else {
        movePaddle("right", "down");
      }
    }
  });

  gameLoop();
});

              
            
!
999px

Console