PingPong Game html code
index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" href="style.css"><title>PingPong Game</title></head><body><canvas id="game"></canvas><!-- Audio ch. --><audio id="backgroundMusic" autoplay loop><source src="background_music.mp3" type="audio/mpeg">Your browser does not support the audio element.</audio><script src="main.js"></script></body></html>
styles.css
body{margin: 0;padding: 0;text-align: center;overflow: hidden;}
main.js
const canvas = document.getElementById("game");const context = canvas.getContext("2d");canvas.width = window.innerWidth;canvas.height = window.innerHeight;const paddleWidth = 18,paddleHeight = 120,paddleSpeed = 8,ballRadius = 12,initialBallSpeed = 16,maxBallSpeed = 50,netWidth = 5,netColor = "Gray";// Draw net on canvasfunction drawNet() {for (let i = 0; i <= canvas.height; i += 15) {drawRect(canvas.width / 2 - netWidth / 2, i, netWidth, 10, netColor);}}// Draw rectangle on canvasfunction drawRect(x, y, width, height, color) {context.fillStyle = color;context.fillRect(x, y, width, height);}// Draw a circle on canvasfunction drawCircle(x, y, radius, color) {context.fillStyle = color;context.beginPath();context.arc(x, y, radius, 0, Math.PI * 2, false);context.closePath();context.fill();}// Draw text on canvasfunction drawText(text, x, y, color, fontSize = 60, fontWeight = 'bold', font = "Courier New") {context.fillStyle = color;context.font = `${fontWeight} ${fontSize}px ${font}`;context.textAlign = "center";context.fillText(text, x, y);}// Create a paddle objectfunction createPaddle(x, y, width, height, color) {return { x, y, width, height, color, score: 0 };}// Create a ball objectfunction createBall(x, y, radius, velocityX, velocityY, color) {return { x, y, radius, velocityX, velocityY, color, speed: initialBallSpeed };}// Define user and computer paddle objectsconst user = createPaddle(0, canvas.height / 2 - paddleHeight / 2, paddleWidth, paddleHeight, "WHITE");const com = createPaddle(canvas.width - paddleWidth, canvas.height / 2 - paddleHeight / 2, paddleWidth, paddleHeight, "WHITE");// Define ball objectconst ball = createBall(canvas.width / 2, canvas.height / 2, ballRadius, initialBallSpeed, initialBallSpeed, "WHITE");// Update user paddle position based on mouse movementcanvas.addEventListener('mousemove', movePaddle);function movePaddle(event) {const rect = canvas.getBoundingClientRect();user.y = event.clientY - rect.top - user.height / 2;}// Check for collision between ball and paddlefunction collision(b, p) {return (b.x + b.radius > p.x && b.x - b.radius < p.x + p.width && b.y + b.radius > p.y && b.y - b.radius < p.y + p.height);}// Reset ball position and velocityfunction resetBall() {ball.x = canvas.width / 2;ball.y = Math.random() * (canvas.height - ball.radius * 2) + ball.radius;ball.velocityX = -ball.velocityX;ball.speed = initialBallSpeed;}// Update game logicfunction update() {// Check for score and reset ball if necessaryif (ball.x - ball.radius < 0) {com.score++;resetBall();} else if (ball.x + ball.radius > canvas.width) {user.score++;resetBall();}// Update ball positionball.x += ball.velocityX;ball.y += ball.velocityY;// Update computer paddle position based on ball positioncom.y += (ball.y - (com.y + com.height / 2)) * 0.1;// Top and bottom wallsif (ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height) {ball.velocityY = -ball.velocityY;}// Determine which paddle is begin hit by the ball and handle collisionlet player = ball.x + ball.radius < canvas.width / 2 ? user : com;if (collision(ball, player)) {const collidePoint = ball.y - (player.y + player.height / 2);const collisionAngle = (Math.PI / 4) * (collidePoint / (player.height / 2));const direction = ball.x + ball.radius < canvas.width / 2 ? 1 : -1;ball.velocityX = direction * ball.speed * Math.cos(collisionAngle);ball.velocityY = ball.speed * Math.sin(collisionAngle);// Increase ball speed and limit to max speedball.speed += 0.2;if (ball.speed > maxBallSpeed) {ball.speed = maxBallSpeed;}}}// Render game on canvasfunction render() {// Clear canvas with black screendrawRect(0, 0, canvas.width, canvas.height, "BLACK");drawNet();// Draw scoresdrawText(user.score, canvas.width / 4, canvas.height / 2, "GRAY", 120, 'bold');drawText(com.score, (3 * canvas.width) / 4, canvas.height / 2, "GRAY", 120, 'bold');// Draw paddlesdrawRect(user.x, user.y, user.width, user.height, user.color);drawRect(com.x, com.y, com.width, com.height, com.color);// Draw balldrawCircle(ball.x, ball.y, ball.radius, ball.color);}// Run game loopfunction gameLoop() {update();render();}// Set gameLoop to run at 60 frame per secondconst framePerSec = 60;setInterval(gameLoop, 1000 / framePerSec);