napisz szybką grę w ponga w html
- User
napisz szybką grę w ponga w html
Assistant
Oto prosty kod gry Pong w HTML z JavaScript:
<!DOCTYPE html> <html> <head> <title>Prosty Pong</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #222; } canvas { border: 3px solid white; background-color: black; } </style> </head> <body> <canvas id="pongCanvas" width="800" height="500"></canvas> <script> // Pobieramy canvas i context const canvas = document.getElementById("pongCanvas"); const ctx = canvas.getContext("2d"); // Ustawienia gry const paddleHeight = 100; const paddleWidth = 10; const ballRadius = 10; // Pozycje paletki let leftPaddleY = canvas.height / 2 - paddleHeight / 2; let rightPaddleY = canvas.height / 2 - paddleHeight / 2; // Pozycja piłki i prędkość let ballX = canvas.width / 2; let ballY = canvas.height / 2; let ballSpeedX = 5; let ballSpeedY = 3; // Punktacja let leftScore = 0; let rightScore = 0; // Kontrolery const keys = {}; // Obsługa klawiatury document.addEventListener("keydown", function(e) { keys[e.code] = true; }); document.addEventListener("keyup", function(e) { keys[e.code] = false; }); // Główna funkcja aktualizująca grę function update() { // Obsługa paletek if (keys["KeyW"] && leftPaddleY > 0) { leftPaddleY -= 8; } if (keys["KeyS"] && leftPaddleY < canvas.height - paddleHeight) { leftPaddleY += 8; } if (keys["ArrowUp"] && rightPaddleY > 0) { rightPaddleY -= 8; } if (keys["ArrowDown"] && rightPaddleY < canvas.height - paddleHeight) { rightPaddleY += 8; } // Ruch piłki ballX += ballSpeedX; ballY += ballSpeedY; // Odbicie od górnej i dolnej ściany if (ballY - ballRadius < 0 || ballY + ballRadius > canvas.height) { ballSpeedY = -ballSpeedY; } // Odbicie od paletek if ( ballX - ballRadius < paddleWidth && ballY > leftPaddleY && ballY < leftPaddleY + paddleHeight ) { ballSpeedX = -ballSpeedX; // Dodajemy losową zmianę kąta ballSpeedY += (Math.random() * 4 - 2); } if ( ballX + ballRadius > canvas.width - paddleWidth && ballY > rightPaddleY && ballY < rightPaddleY + paddleHeight ) { ballSpeedX = -ballSpeedX; // Dodajemy losową zmianę kąta ballSpeedY += (Math.random() * 4 - 2); } // Punktacja if (ballX < 0) { rightScore++; resetBall(); } else if (ballX > canvas.width) { leftScore++; resetBall(); } // Rysowanie draw(); // Pętla gry requestAnimationFrame(update); } // Resetowanie piłki function resetBall() { ballX = canvas.width / 2; ballY = canvas.height / 2; ballSpeedX = -ballSpeedX; ballSpeedY = Math.random() * 6 - 3; } // Funkcja rysująca function draw() { // Czyszczenie planszy ctx.fillStyle = "black"; ctx.fillRect(0, 0, canvas.width, canvas.height); // Rysowanie linii środkowej ctx.strokeStyle = "white"; ctx.setLineDash([10, 10]); ctx.beginPath(); ctx.moveTo(canvas.width / 2, 0); ctx.lineTo(canvas.width / 2, canvas.height); ctx.stroke(); ctx.setLineDash([]); // Rysowanie paletki lewej ctx.fillStyle = "white"; ctx.fillRect(0, leftPaddleY, paddleWidth, paddleHeight); // Rysowanie paletki prawej ctx.fillRect(canvas.width - paddleWidth, rightPaddleY, paddleWidth, paddleHeight); // Rysowanie piłki ctx.beginPath(); ctx.arc(ballX, ballY, ballRadius, 0, Math.PI * 2); ctx.fill(); // Rysowanie punktacji ctx.font = "40px Arial"; ctx.fillText(leftScore, canvas.width / 4, 50); ctx.fillText(rightScore, 3 * canvas.width / 4, 50); } // Startujemy grę update(); </script> </body> </html>Czy chciałbyś, żebym wyjaśnił lub omówił ten kod?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.