Poniżej znajdziesz kompletny przykład prostej gry „Wężyka” napisanego w czystym HTML/CSS/JavaScript. Wystarczy skopiować całość do pliku index.html i otworzyć go w przeglądarce.
html
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Gra w Wężyka</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
background: #f0f0f0;
font-family: sans-serif;
margin: 0;
height: 100vh;
justify-content: center;
}
canvas {
background: #222;
border: 4px solid #555;
}
#score {
margin: 10px;
font-size: 1.2em;
color: #333;
}
</style>
</head>
<body>
<div id="score">Wynik: 0</div>
<canvas id="game" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const scoreEl = document.getElementById('score');
const tileSize = 20;
const tileCount = canvas.width / tileSize; // 400/20 = 20
let snake = [{ x: 10, y: 10 }];
let apple = { x: 15, y: 15 };
let vel = { x: 0, y: 0 };
let tailLength = 1;
let score = 0;
let gameOver = false;
document.addEventListener('keydown', keyDown);
function keyDown(e) {
// strzałki lub WASD
switch(e.key) {
case 'ArrowUp': case 'w': if (vel.y === 0) vel = { x: 0, y: -1 }; break;
case 'ArrowDown': case 's': if (vel.y === 0) vel = { x: 0, y: 1 }; break;
case 'ArrowLeft': case 'a': if (vel.x === 0) vel = { x: -1, y: 0 }; break;
case 'ArrowRight': case 'd': if (vel.x === 0) vel = { x: 1, y: 0 }; break;
}
}
function gameLoop() {
if (gameOver) return showGameOver();
update();
draw();
}
function update() {
// Przesuń węża
const head = {
x: snake[0].x + vel.x,
y: snake[0].y + vel.y
};
// Warunki końca gry (ściana)
if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) {
gameOver = true;
}
// Zderzenie z własnym ogonem
for (let segment of snake) {
if (head.x === segment.x && head.y === segment.y) {
gameOver = true;
}
}
snake.unshift(head);
// Gdy zjadamy jabłko
if (head.x === apple.x && head.y === apple.y) {
score++;
tailLength++;
placeApple();
}
// Przycinamy ogon
while (snake.length > tailLength) {
snake.pop();
}
scoreEl.textContent = 'Wynik: ' + score;
}
function placeApple() {
apple.x = Math.floor(Math.random() * tileCount);
apple.y = Math.floor(Math.random() * tileCount);
// upewnij się, że jabłko nie pojawi się na wężu
for (let segment of snake) {
if (segment.x === apple.x && segment.y === apple.y) {
return placeApple();
}
}
}
function draw() {
// Tło
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Wąż
ctx.fillStyle = '#0f0';
snake.forEach((segment, idx) => {
ctx.fillRect(segment.x * tileSize, segment.y * tileSize, tileSize-2, tileSize-2);
});
// Jabłko
ctx.fillStyle = '#f00';
ctx.fillRect(apple.x * tileSize, apple.y * tileSize, tileSize-2, tileSize-2);
}
function showGameOver() {
ctx.fillStyle = 'rgba(0,0,0,0.7)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.font = '36px sans-serif';
ctx.textAlign = 'center';
ctx.fillText('Koniec gry!', canvas.width/2, canvas.height/2 - 10);
ctx.font = '20px sans-serif';
ctx.fillText('Twój wynik: ' + score, canvas.width/2, canvas.height/2 + 30);
}
// Start pętli gry co 100ms
setInterval(gameLoop, 100);
</script>
</body>
</html>
Jak to działa?
Definiujemy kwadratową planszę (canvas) o wymiarach 400×400 pikseli i siatce 20×20 pól.
Wąż to tablica współrzędnych; na wejściu ma tylko jedną „głowę”.
Sterowanie realizujemy klawiszami strzałek lub WASD.
Po zjedzeniu jabłka (czerwony kwadrat) wąż rośnie, a na planszy losuje się nowe jabłko.
Gra kończy się, gdy wąż zderzy się ze ścianą lub ze swoim ogonem.