Pewnie! Oto bardzo prosty przykład gry Ping Pong w HTML i JavaScript. Grę możesz uruchomić w przeglądarce – wystarczy skopiować poniższy kod do pliku .html i otworzyć go w dowolnej przeglądarce internetowej.
🎮 Gra Ping Pong (HTML + CSS + JavaScript)
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8" />
<title>Gra Ping Pong</title>
<style>
body {
margin: 0;
background: #000;
}
canvas {
display: block;
margin: 0 auto;
background: #111;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="500"></canvas>
<script>
/** USTAWIENIA **/
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const paddleWidth = 10, paddleHeight = 100;
let upPressed = false, downPressed = false;
// Paletki
const leftPaddle = { x: 20, y: canvas.height / 2 - paddleHeight / 2, speed: 6 };
const rightPaddle = { x: canvas.width - 30, y: canvas.height / 2 - paddleHeight / 2, speed: 6 };
// Piłka
const ball = {
x: canvas.width / 2,
y: canvas.height / 2,
radius: 10,
speedX: 5 * (Math.random() > 0.5 ? 1 : -1),
speedY: 5 * (Math.random() > 0.5 ? 1 : -1)
};
// Punkty
let leftScore = 0, rightScore = 0;
// Obsługa klawiatury
document.addEventListener("keydown", e => {
if (e.key === "ArrowUp") upPressed = true;
if (e.key === "ArrowDown") downPressed = true;
});
document.addEventListener("keyup", e => {
if (e.key === "ArrowUp") upPressed = false;
if (e.key === "ArrowDown") downPressed = false;
});
function drawRect(x, y, width, height, color) {
ctx.fillStyle = color;
ctx.fillRect(x, y, width, height);
}
function drawCircle(x, y, radius, color) {
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
}
function drawText(text, x, y, size = "30px", color = "#FFF") {
ctx.fillStyle = color;
ctx.font = `${size} Arial`;
ctx.fillText(text, x, y);
}
function resetBall() {
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
ball.speedX *= -1;
ball.speedY = 5 * (Math.random() > 0.5 ? 1 : -1);
}
function update() {
// Ruch gracza
if (upPressed && rightPaddle.y > 0) rightPaddle.y -= rightPaddle.speed;
if (downPressed && rightPaddle.y < canvas.height - paddleHeight) rightPaddle.y += rightPaddle.speed;
// Prosta SI: ruch lewej paletki
if (ball.y < leftPaddle.y + paddleHeight / 2) {
leftPaddle.y -= leftPaddle.speed;
} else {
leftPaddle.y += leftPaddle.speed;
}
// Zaktualizuj pozycję piłki
ball.x += ball.speedX;
ball.y += ball.speedY;
// Odbicie od góry/dolu
if (ball.y - ball.radius < 0 || ball.y + ball.radius > canvas.height) {
ball.speedY *= -1;
}
// Kolizja z paletkami
function checkCollision(paddle) {
return (
ball.x - ball.radius < paddle.x + paddleWidth &&
ball.x + ball.radius > paddle.x &&
ball.y + ball.radius > paddle.y &&
ball.y - ball.radius < paddle.y + paddleHeight
);
}
if (checkCollision(leftPaddle) || checkCollision(rightPaddle)) {
ball.speedX *= -1;
}
// Punktacja
if (ball.x - ball.radius < 0) {
rightScore++;
resetBall();
} else if (ball.x + ball.radius > canvas.width) {
leftScore++;
resetBall();
}
}
function draw() {
drawRect(0, 0, canvas.width, canvas.height, "#111");
drawRect(leftPaddle.x, leftPaddle.y, paddleWidth, paddleHeight, "#0F0");
drawRect(rightPaddle.x, rightPaddle.y, paddleWidth, paddleHeight, "#F00");
drawCircle(ball.x, ball.y, ball.radius, "#FFF");
drawText(leftScore, canvas.width / 4, 50);
drawText(rightScore, (3 * canvas.width) / 4, 50);
}
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>
✅ Jak zagrać?
- Skopiuj powyższy kod i zapisz go jako plik
pingpong.html.
- Otwórz plik w przeglądarce (np. Chrome).
- Steruj grą klawiszami:
- Użyj strzałek (↑ i ↓) do poruszania się prawą paletką.
- Lewa paletka jest sterowana automatycznie (SI).
Jeśli chcesz rozbudować grę, mogę dodać:
- Tryb dla dwóch graczy,
- Menu startowe,
- Dźwięki,
- GUI punktacji,
- Stopień trudności SI.
Chcesz dodać coś do gry? 😊