write me a very simple version of Pong in javascript
- User
write me a very simple version of Pong in javascript
Assistant
<!DOCTYPE html> <html> <head> <title>Simple Pong Game</title> <style> body { margin: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #222; } canvas { border: 2px solid white; background-color: black; } </style> </head> <body> <canvas id="pongCanvas" width="600" height="400"></canvas> <script> // Get the canvas and context const canvas = document.getElementById('pongCanvas'); const ctx = canvas.getContext('2d'); // Game objects const ball = { x: canvas.width / 2, y: canvas.height / 2, radius: 10, speedX: 5, speedY: 3 }; const paddleHeight = 100; const paddleWidth = 10; const player = { x: 10, y: canvas.height / 2 - paddleHeight / 2, width: paddleWidth, height: paddleHeight, score: 0 }; const computer = { x: canvas.width - paddleWidth - 10, y: canvas.height / 2 - paddleHeight / 2, width: paddleWidth, height: paddleHeight, score: 0 }; // Track mouse movement canvas.addEventListener('mousemove', (event) => { const rect = canvas.getBoundingClientRect(); player.y = event.clientY - rect.top - player.height / 2; // Keep paddle inside the canvas if (player.y < 0) { player.y = 0; } if (player.y + player.height > canvas.height) { player.y = canvas.height - player.height; } }); // Game loop function gameLoop() { // Clear the canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Move the ball ball.x += ball.speedX; ball.y += ball.speedY; // Simple AI for computer paddle const computerCenter = computer.y + computer.height / 2; const ballCenter = ball.y; if (computerCenter < ballCenter - 35) { computer.y += 4; } else if (computerCenter > ballCenter + 35) { computer.y -= 4; } // Ball collision with top and bottom walls if (ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height) { ball.speedY = -ball.speedY; } // Ball collision with paddles if ( ball.x - ball.radius < player.x + player.width && ball.y > player.y && ball.y < player.y + player.height && ball.speedX < 0 ) { ball.speedX = -ball.speedX; } if ( ball.x + ball.radius > computer.x && ball.y > computer.y && ball.y < computer.y + computer.height && ball.speedX > 0 ) { ball.speedX = -ball.speedX; } // Score points if ball goes past paddle if (ball.x < 0) { computer.score++; resetBall(); } else if (ball.x > canvas.width) { player.score++; resetBall(); } // Draw everything drawPaddle(player.x, player.y, player.width, player.height); drawPaddle(computer.x, computer.y, computer.width, computer.height); drawBall(ball.x, ball.y, ball.radius); drawScore(); drawNet(); // Continue the game loop requestAnimationFrame(gameLoop); } function resetBall() { ball.x = canvas.width / 2; ball.y = canvas.height / 2; ball.speedX = -ball.speedX; ball.speedY = Math.random() * 6 - 3; // Random Y direction } function drawPaddle(x, y, width, height) { ctx.fillStyle = 'white'; ctx.fillRect(x, y, width, height); } function drawBall(x, y, radius) { ctx.fillStyle = 'white'; ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI * 2); ctx.closePath(); ctx.fill(); } function drawScore() { ctx.fillStyle = 'white'; ctx.font = '32px Arial'; ctx.textAlign = 'center'; ctx.fillText(player.score, canvas.width / 4, 50); ctx.fillText(computer.score, 3 * canvas.width / 4, 50); } function drawNet() { ctx.setLineDash([5, 5]); ctx.beginPath(); ctx.moveTo(canvas.width / 2, 0); ctx.lineTo(canvas.width / 2, canvas.height); ctx.strokeStyle = 'white'; ctx.stroke(); ctx.setLineDash([]); } // Start the game gameLoop(); </script> </body> </html>Would you like me to explain how this code works?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.