Oto rozbudowana wersja gry w węża napisana w HTML, CSS i JavaScript:
html
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Gra Snake</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #2c3e50;
font-family: Arial, sans-serif;
}
#gameContainer {
text-align: center;
}
#gameCanvas {
border: 4px solid #34495e;
background-color: #ecf0f1;
}
#scoreDisplay {
color: white;
font-size: 24px;
margin-bottom: 10px;
}
#gameOverScreen {
display: none;
color: white;
font-size: 36px;
}
</style>
</head>
<body>
<div id="gameContainer">
<div id="scoreDisplay">Wynik: 0</div>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div id="gameOverScreen">Koniec gry!</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreDisplay = document.getElementById('scoreDisplay');
const gameOverScreen = document.getElementById('gameOverScreen');
const gridSize = 20;
const tileCount = canvas.width / gridSize;
let snake = [{x: 10, y: 10}];
let food = generateFood();
let dx = 1;
let dy = 0;
let score = 0;
let gameSpeed = 150;
let gameLoop;
function drawGame() {
clearCanvas();
moveSnake();
drawSnake();
drawFood();
checkCollision();
}
function clearCanvas() {
ctx.fillStyle = '#ecf0f1';
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
function moveSnake() {
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score++;
scoreDisplay.textContent = `Wynik: ${score}`;
food = generateFood();
gameSpeed = Math.max(50, gameSpeed - 5);
clearInterval(gameLoop);
gameLoop = setInterval(drawGame, gameSpeed);
} else {
snake.pop();
}
}
function drawSnake() {
ctx.fillStyle = '#2ecc71';
snake.forEach(segment => {
ctx.fillRect(
segment.x * gridSize,
segment.y * gridSize,
gridSize - 2,
gridSize - 2
);
});
}
function drawFood() {
ctx.fillStyle = '#e74c3c';
ctx.fillRect(
food.x * gridSize,
food.y * gridSize,
gridSize - 2,
gridSize - 2
);
}
function generateFood() {
return {
x: Math.floor(Math.random() * tileCount),
y: Math.floor(Math.random() * tileCount)
};
}
function checkCollision() {
const head = snake[0];
// Kolizja ze ścianami
if (head.x < 0 || head.x >= tileCount ||
head.y < 0 || head.y >= tileCount) {
gameOver();
}
// Kolizja z własnym ciałem
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
gameOver();
}
}
}
function gameOver() {
clearInterval(gameLoop);
gameOverScreen.style.display = 'block';
}
document.addEventListener('keydown', changeDirection);
function changeDirection(event) {
const LEFT_KEY = 37;
const RIGHT_KEY = 39;
const UP_KEY = 38;
const DOWN_KEY = 40;
const keyPressed = event.keyCode;
const goingUp = dy === -1;
const goingDown = dy === 1;
const goingRight = dx === 1;
const goingLeft = dx === -1;
if (keyPressed === LEFT_KEY && !goingRight) {
dx = -1;
dy = 0;
}
if (keyPressed === UP_KEY && !goingDown) {
dx = 0;
dy = -1;
}
if (keyPressed === RIGHT_KEY && !goingLeft) {
dx = 1;
dy = 0;
}
if (keyPressed === DOWN_KEY && !goingUp) {
dx = 0;
dy = 1;
}
}
gameLoop = setInterval(drawGame, gameSpeed);
</script>
</body>
</html>
Ta wersja gry w węża oferuje:
✅ Dynamiczną planszę
✅ Zliczanie punktów
✅ Zwiększanie trudności (przyspieszanie gry)
✅ Ekran końca gry
✅ Responsywny design
✅ Ładną kolorystykę
✅ Sprawdzanie kolizji
Możesz ją od razu uruchomić w przeglądarce. Sterowanie strzałkami, zasady klasyczne - im więcej zjesz, tym dłuższy wąż i więcej punktów.
Chcesz, żebym jeszcze coś dopracował lub dodał?