Napisz prostą grę, którą mogę włączyć w przeglądarce. Gra to prosty tenis 2D.
- User
Napisz prostą grę, którą mogę włączyć w przeglądarce. Gra to prosty tenis 2D.
Assistant
Oto prosta gra w tenisa 2D, którą możesz uruchomić w przeglądarce:
<!DOCTYPE html> <html lang="pl"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tenis 2D</title> <style> body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; background-color: #1a1a1a; font-family: Arial, sans-serif; } #gameContainer { text-align: center; } canvas { border: 2px solid #fff; background-color: #0a4d0a; } #score { color: white; font-size: 24px; margin-bottom: 10px; } #instructions { color: white; margin-top: 10px; font-size: 14px; } </style> </head> <body> <div id="gameContainer"> <div id="score">Gracz: 0 | Komputer: 0</div> <canvas id="gameCanvas" width="800" height="400"></canvas> <div id="instructions"> Sterowanie: W - góra, S - dół | Spacja - pauza/wznowienie </div> </div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // Zmienne gry let gameRunning = true; let playerScore = 0; let computerScore = 0; // Paletka gracza const player = { x: 20, y: canvas.height / 2 - 50, width: 15, height: 100, speed: 5, dy: 0 }; // Paletka komputera const computer = { x: canvas.width - 35, y: canvas.height / 2 - 50, width: 15, height: 100, speed: 3 }; // Piłka const ball = { x: canvas.width / 2, y: canvas.height / 2, radius: 8, speedX: 5, speedY: 3 }; // Obsługa klawiatury const keys = {}; document.addEventListener('keydown', (e) => { keys[e.key.toLowerCase()] = true; if (e.key === ' ') { gameRunning = !gameRunning; } }); document.addEventListener('keyup', (e) => { keys[e.key.toLowerCase()] = false; }); // Rysowanie prostokąta function drawRect(x, y, width, height, color) { ctx.fillStyle = color; ctx.fillRect(x, y, width, height); } // Rysowanie koła function drawCircle(x, y, radius, color) { ctx.fillStyle = color; ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI * 2); ctx.fill(); } // Rysowanie linii środkowej function drawNet() { ctx.setLineDash([5, 15]); ctx.beginPath(); ctx.moveTo(canvas.width / 2, 0); ctx.lineTo(canvas.width / 2, canvas.height); ctx.strokeStyle = 'white'; ctx.lineWidth = 2; ctx.stroke(); ctx.setLineDash([]); } // Aktualizacja pozycji gracza function updatePlayer() { if (keys['w'] && player.y > 0) { player.y -= player.speed; } if (keys['s'] && player.y < canvas.height - player.height) { player.y += player.speed; } } // AI komputera function updateComputer() { const computerCenter = computer.y + computer.height / 2; if (computerCenter < ball.y - 35) { computer.y += computer.speed; } else if (computerCenter > ball.y + 35) { computer.y -= computer.speed; } // Ograniczenia if (computer.y < 0) computer.y = 0; if (computer.y > canvas.height - computer.height) { computer.y = canvas.height - computer.height; } } // Aktualizacja piłki function updateBall() { ball.x += ball.speedX; ball.y += ball.speedY; // Odbicie od górnej i dolnej krawędzi if (ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height) { ball.speedY = -ball.speedY; } // Kolizja z paletką gracza if (ball.x - ball.radius < player.x + player.width && ball.x + ball.radius > player.x && ball.y > player.y && ball.y < player.y + player.height) { ball.speedX = -ball.speedX; // Zmiana kąta odbicia w zależności od miejsca uderzenia let collidePoint = ball.y - (player.y + player.height / 2); collidePoint = collidePoint / (player.height / 2); ball.speedY = collidePoint * 5; } // Kolizja z paletką komputera if (ball.x + ball.radius > computer.x && ball.x - ball.radius < computer.x + computer.width && ball.y > computer.y && ball.y < computer.y + computer.height) { ball.speedX = -ball.speedX; let collidePoint = ball.y - (computer.y + computer.height / 2); collidePoint = collidePoint / (computer.height / 2); ball.speedY = collidePoint * 5; } // Punkty if (ball.x < 0) { computerScore++; resetBall(); } else if (ball.x > canvas.width) { playerScore++; resetBall(); } } // Reset piłki function resetBall() { ball.x = canvas.width / 2; ball.y = canvas.height / 2; ball.speedX = -ball.speedX; ball.speedY = (Math.random() - 0.5) * 6; updateScore(); } // Aktualizacja wyniku function updateScore() { document.getElementById('score').textContent = `Gracz: ${playerScore} | Komputer: ${computerScore}`; } // Rysowanie wszystkiego function draw() { // Czyszczenie canvas ctx.clearRect(0, 0, canvas.width, canvas.height); // Rysowanie elementów drawNet(); drawRect(player.x, player.y, player.width, player.height, 'white'); drawRect(computer.x, computer.y, computer.width, computer.height, 'white'); drawCircle(ball.x, ball.y, ball.radius, 'yellow'); } // Główna pętla gry function gameLoop() { if (gameRunning) { updatePlayer(); updateComputer(); updateBall(); } draw(); requestAnimationFrame(gameLoop); } // Start gry gameLoop(); </script> </body> </html>Aby uruchomić grę:
- Skopiuj cały kod do nowego pliku tekstowego
- Zapisz plik z rozszerzeniem
.html(np.tenis.html) - Otwórz plik w dowolnej przeglądarce internetowej
Sterowanie:
- W - ruch paletki w górę
- S - ruch paletki w dół
- Spacja - pauza/wznowienie gry
Gra zawiera podstawowe funkcje tenisa:
- Gracz kontroluje lewą paletkę
- Komputer automatycznie steruje prawą paletką
- Piłka odbija się od paletek i ścian
- System punktacji
- Kąt odbicia piłki zależy od miejsca uderzenia w paletkę
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.