Écris un jeu de pong html
- User
Écris un jeu de pong html
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Écris un jeu de pong html
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Voici un jeu de Pong complet en HTML/CSS/JavaScript, prêt à être copié-collé dans un fichier .html et ouvert dans un navigateur :
pong.html<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<title>Pong</title>
<style>
body {
margin: 0;
background: #111;
color: white;
font-family: sans-serif;
text-align: center;
}
canvas {
background: black;
display: block;
margin: 20px auto;
border: 2px solid white;
}
</style>
</head>
<body>
<h1>Pong</h1>
<canvas id="game" width="800" height="400"></canvas>
<p>Joueur 1 : W / S | Joueur 2 : ↑ / ↓</p>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const paddleWidth = 10, paddleHeight = 80, ballSize = 10;
let ballX = canvas.width / 2, ballY = canvas.height / 2;
let ballSpeedX = 5, ballSpeedY = 3;
let paddle1Y = canvas.height / 2 - paddleHeight / 2;
let paddle2Y = canvas.height / 2 - paddleHeight / 2;
let score1 = 0, score2 = 0;
const winningScore = 5;
let wDown = false, sDown = false;
let upDown = false, downDown = false;
document.addEventListener("keydown", e => {
if (e.key.toLowerCase() === "w") wDown = true;
if (e.key.toLowerCase() === "s") sDown = true;
if (e.key === "ArrowUp") upDown = true;
if (e.key === "ArrowDown") downDown = true;
});
document.addEventListener("keyup", e => {
if (e.key.toLowerCase() === "w") wDown = false;
if (e.key.toLowerCase() === "s") sDown = false;
if (e.key === "ArrowUp") upDown = false;
if (e.key === "ArrowDown") downDown = false;
});
function resetBall() {
ballX = canvas.width / 2;
ballY = canvas.height / 2;
ballSpeedX = -ballSpeedX;
ballSpeedY = Math.random() * 6 - 3;
}
function drawRect(x, y, w, h, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, w, h);
}
function drawCircle(x, y, r, color) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2);
ctx.fill();
}
function drawNet() {
for (let i = 0; i < canvas.height; i += 20) {
drawRect(canvas.width / 2 - 1, i, 2, 10, "white");
}
}
function draw() {
drawRect(0, 0, canvas.width, canvas.height, "black");
drawNet();
drawRect(0, paddle1Y, paddleWidth, paddleHeight, "white");
drawRect(canvas.width - paddleWidth, paddle2Y, paddleWidth, paddleHeight, "white");
drawCircle(ballX, ballY, ballSize, "white");
ctx.font = "48px sans-serif";
ctx.fillText(score1, canvas.width / 4, 50);
ctx.fillText(score2, 3 * canvas.width / 4, 50);
}
function move() {
if (wDown && paddle1Y > 0) paddle1Y -= 6;
if (sDown && paddle1Y < canvas.height - paddleHeight) paddle1Y += 6;
if (upDown && paddle2Y > 0) paddle2Y -= 6;
if (downDown && paddle2Y < canvas.height - paddleHeight) paddle2Y += 6;
ballX += ballSpeedX;
ballY += ballSpeedY;
if (ballY <= 0 || ballY >= canvas.height - ballSize) {
ballSpeedY = -ballSpeedY;
}
if (
ballX <= paddleWidth &&
ballY >= paddle1Y &&
ballY <= paddle1Y + paddleHeight
) {
ballSpeedX = -ballSpeedX;
const deltaY = ballY - (paddle1Y + paddleHeight / 2);
ballSpeedY = deltaY * 0.35;
}
if (
ballX >= canvas.width - paddleWidth - ballSize &&
ballY >= paddle2Y &&
ballY <= paddle2Y + paddleHeight
) {
ballSpeedX = -ballSpeedX;
const deltaY = ballY - (paddle2Y + paddleHeight / 2);
ballSpeedY = deltaY * 0.35;
}
if (ballX < 0) {
score2++;
resetBall();
} else if (ballX > canvas.width) {
score1++;
resetBall();
}
if (score1 === winningScore || score2 === winningScore) {
alert(`Victoire du Joueur ${score1 === winningScore ? 1 : 2} !`);
score1 = 0;
score2 = 0;
resetBall();
}
}
function gameLoop() {
move();
draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>
pong.html.Si tu veux une version IA ou multijoueur en ligne, je peux te l’ajouter aussi.