Écris un jeu de pong html
- User
Écris un jeu de pong html
Assistant
<!DOCTYPE html> <html> <head> <title>Jeu de Pong</title> <style> body { margin: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #222; font-family: Arial, sans-serif; } canvas { border: 2px solid white; background-color: #000; } #score { position: absolute; top: 10px; color: white; font-size: 24px; font-weight: bold; } </style> </head> <body> <div id="score">0 : 0</div> <canvas id="pongCanvas" width="800" height="500"></canvas> <script> // Récupération du canvas et initialisation du contexte const canvas = document.getElementById('pongCanvas'); const ctx = canvas.getContext('2d'); const scoreElement = document.getElementById('score'); // Variables du jeu let playerScore = 0; let computerScore = 0; // Création de la balle const ball = { x: canvas.width / 2, y: canvas.height / 2, radius: 10, velocityX: 5, velocityY: 5, speed: 7, color: 'white' }; // Création des raquettes const player = { x: 0, y: (canvas.height - 100) / 2, width: 10, height: 100, score: 0, color: 'white', dy: 8 }; const computer = { x: canvas.width - 10, y: (canvas.height - 100) / 2, width: 10, height: 100, score: 0, color: 'white', dy: 5 }; // Création du filet const net = { x: (canvas.width - 2) / 2, y: 0, width: 2, height: 10, color: 'white' }; // Dessiner le rectangle function drawRect(x, y, w, h, color) { ctx.fillStyle = color; ctx.fillRect(x, y, w, h); } // Dessiner le cercle function drawCircle(x, y, r, color) { ctx.fillStyle = color; ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2, false); ctx.closePath(); ctx.fill(); } // Dessiner le filet function drawNet() { for (let i = 0; i <= canvas.height; i += 15) { drawRect(net.x, net.y + i, net.width, net.height, net.color); } } // Afficher le score function updateScore() { scoreElement.textContent = `${playerScore} : ${computerScore}`; } // Détection de collision function collision(b, p) { b.top = b.y - b.radius; b.bottom = b.y + b.radius; b.left = b.x - b.radius; b.right = b.x + b.radius; p.top = p.y; p.bottom = p.y + p.height; p.left = p.x; p.right = p.x + p.width; return b.right > p.left && b.bottom > p.top && b.left < p.right && b.top < p.bottom; } // Contrôles du joueur let playerDirection = 0; // 0: immobile, 1: vers le bas, -1: vers le haut window.addEventListener('keydown', function(e) { if (e.key === 'ArrowUp') { playerDirection = -1; } else if (e.key === 'ArrowDown') { playerDirection = 1; } }); window.addEventListener('keyup', function(e) { if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { playerDirection = 0; } }); // Mettre à jour l'état du jeu function update() { // Déplacer le joueur player.y += player.dy * playerDirection; // Limiter le mouvement du joueur if (player.y < 0) { player.y = 0; } else if (player.y > canvas.height - player.height) { player.y = canvas.height - player.height; } // Intelligence artificielle simple pour l'ordinateur computer.y += (ball.y - (computer.y + computer.height/2)) * 0.1; // Déplacer la balle ball.x += ball.velocityX; ball.y += ball.velocityY; // Collision avec les murs supérieur et inférieur if (ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height) { ball.velocityY = -ball.velocityY; } // Déterminer quel côté a été touché let player1 = (ball.x < canvas.width/2) ? player : computer; // Si la balle touche une raquette if (collision(ball, player1)) { // Point de collision let collidePoint = ball.y - (player1.y + player1.height/2); // Normalisation collidePoint = collidePoint / (player1.height/2); // Calculer l'angle let angleRad = collidePoint * Math.PI/4; // Déterminer la direction let direction = (ball.x < canvas.width/2) ? 1 : -1; // Changer la vélocité ball.velocityX = direction * ball.speed * Math.cos(angleRad); ball.velocityY = ball.speed * Math.sin(angleRad); // Augmenter la vitesse ball.speed += 0.2; } // Mettre à jour le score if (ball.x - ball.radius < 0) { // L'ordinateur marque computerScore++; updateScore(); resetBall(); } else if (ball.x + ball.radius > canvas.width) { // Le joueur marque playerScore++; updateScore(); resetBall(); } } // Réinitialiser la balle function resetBall() { ball.x = canvas.width/2; ball.y = canvas.height/2; ball.velocityX = -ball.velocityX; ball.speed = 7; } // Dessiner tout function render() { // Effacer le canvas drawRect(0, 0, canvas.width, canvas.height, 'black'); // Dessiner le filet drawNet(); // Dessiner les raquettes drawRect(player.x, player.y, player.width, player.height, player.color); drawRect(computer.x, computer.y, computer.width, computer.height, computer.color); // Dessiner la balle drawCircle(ball.x, ball.y, ball.radius, ball.color); } // Boucle de jeu function gameLoop() { update(); render(); requestAnimationFrame(gameLoop); } // Démarrer le jeu updateScore(); gameLoop(); </script> </body> </html>Voulez-vous que j'explique comment fonctionne ce code ou certaines parties spécifiques du jeu ?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.