gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
napisz gre w html do gry w ping pong
- User
napisz gre w html do gry w ping pong
- Assistant
Oczywiście! Oto prosty przykład gry w Ping Pong (Pong) napisanej w HTML, CSS i JavaScript. Możesz skopiować poniższy kod do jednego pliku
.htmli uruchomić go w przeglądarce.
<!DOCTYPE html> <html lang="pl"> <head> <meta charset="UTF-8"> <title>Ping Pong Gra</title> <style> body { background: #222; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; } canvas { background: #000; display: block; border: 2px solid #fff; } </style> </head> <body> <canvas id="pong" width="600" height="400"></canvas> <script> const canvas = document.getElementById('pong'); const ctx = canvas.getContext('2d'); // Parametry gry const paddleWidth = 10, paddleHeight = 80; const ballSize = 10; let leftPaddleY = canvas.height / 2 - paddleHeight / 2; let rightPaddleY = canvas.height / 2 - paddleHeight / 2; let leftPaddleSpeed = 0, rightPaddleSpeed = 0; let ballX = canvas.width / 2 - ballSize / 2; let ballY = canvas.height / 2 - ballSize / 2; let ballSpeedX = 4, ballSpeedY = 4; let leftScore = 0, rightScore = 0; // Rysowanie paletek, piłki i wyniku function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); // Siatka ctx.fillStyle = "#fff"; for (let i = 0; i < canvas.height; i += 20) { ctx.fillRect(canvas.width / 2 - 1, i, 2, 10); } // Lewa paletka ctx.fillRect(10, leftPaddleY, paddleWidth, paddleHeight); // Prawa paletka ctx.fillRect(canvas.width - 20, rightPaddleY, paddleWidth, paddleHeight); // Piłka ctx.fillRect(ballX, ballY, ballSize, ballSize); // Wynik ctx.font = "32px Arial"; ctx.fillText(leftScore, canvas.width / 4, 40); ctx.fillText(rightScore, 3 * canvas.width / 4, 40); } // Ruch paletek function movePaddles() { leftPaddleY += leftPaddleSpeed; rightPaddleY += rightPaddleSpeed; // Ograniczenia if (leftPaddleY < 0) leftPaddleY = 0; if (leftPaddleY + paddleHeight > canvas.height) leftPaddleY = canvas.height - paddleHeight; if (rightPaddleY < 0) rightPaddleY = 0; if (rightPaddleY + paddleHeight > canvas.height) rightPaddleY = canvas.height - paddleHeight; } // Ruch piłki function moveBall() { ballX += ballSpeedX; ballY += ballSpeedY; // Odbicie od góry i dołu if (ballY <= 0 || ballY + ballSize >= canvas.height) { ballSpeedY = -ballSpeedY; } // Odbicie od lewej paletki if ( ballX <= 20 && ballY + ballSize >= leftPaddleY && ballY <= leftPaddleY + paddleHeight ) { ballSpeedX = -ballSpeedX; } // Odbicie od prawej paletki if ( ballX + ballSize >= canvas.width - 20 && ballY + ballSize >= rightPaddleY && ballY <= rightPaddleY + paddleHeight ) { ballSpeedX = -ballSpeedX; } // Punkt dla prawego gracza if (ballX < 0) { rightScore++; resetBall(); } // Punkt dla lewego gracza if (ballX + ballSize > canvas.width) { leftScore++; resetBall(); } } // Reset piłki po zdobyciu punktu function resetBall() { ballX = canvas.width / 2 - ballSize / 2; ballY = canvas.height / 2 - ballSize / 2; ballSpeedX = -ballSpeedX; ballSpeedY = 4 * (Math.random() > 0.5 ? 1 : -1); } // Sterowanie document.addEventListener('keydown', function(e) { // W/S dla lewego gracza if (e.key === 'w' || e.key === 'W') leftPaddleSpeed = -6; if (e.key === 's' || e.key === 'S') leftPaddleSpeed = 6; // Strzałki dla prawego gracza if (e.key === 'ArrowUp') rightPaddleSpeed = -6; if (e.key === 'ArrowDown') rightPaddleSpeed = 6; }); document.addEventListener('keyup', function(e) { if (e.key === 'w' || e.key === 'W') leftPaddleSpeed = 0; if (e.key === 's' || e.key === 'S') leftPaddleSpeed = 0; if (e.key === 'ArrowUp') rightPaddleSpeed = 0; if (e.key === 'ArrowDown') rightPaddleSpeed = 0; }); // Główna pętla gry function gameLoop() { movePaddles(); moveBall(); draw(); requestAnimationFrame(gameLoop); } gameLoop(); </script> </body> </html>
Sterowanie:
- Lewy gracz:
W(góra),S(dół) - Prawy gracz:
Strzałka w górę,Strzałka w dół
To bardzo prosty Pong, ale możesz go dowolnie rozbudować! Jeśli chcesz wersję z komputerowym przeciwnikiem lub innymi funkcjami, daj znać!
- Lewy gracz:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.