Создание полной игры "Змейка" на HTML, CSS и JavaScript может выглядеть следующим образом. Обратите внимание, что это базовая версия, которая может быть улучшена добавлением стилей и функциональности:
html
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Игра Змейка</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
canvas {
border: 1px solid #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const box = 20;
const canvasSize = 20;
let snake = [];
snake[0] = { x: 9 * box, y: 10 * box };
let food = {
x: Math.floor(Math.random() * canvasSize) * box,
y: Math.floor(Math.random() * canvasSize) * box
};
let score = 0;
let d;
document.addEventListener('keydown', direction);
function direction(event) {
if (event.keyCode === 37 && d !== 'RIGHT') {
d = 'LEFT';
} else if (event.keyCode === 38 && d !== 'DOWN') {
d = 'UP';
} else if (event.keyCode === 39 && d !== 'LEFT') {
d = 'RIGHT';
} else if (event.keyCode === 40 && d !== 'UP') {
d = 'DOWN';
}
}
function collision(head, array) {
for (let i = 0; i < array.length; i++) {
if (head.x === array[i].x && head.y === array[i].y) {
return true;
}
}
return false;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < snake.length; i++) {
ctx.fillStyle = i === 0 ? 'green' : 'white';
ctx.fillRect(snake[i].x, snake[i].y, box, box);
ctx.strokeStyle = 'black';
ctx.strokeRect(snake[i].x, snake[i].y, box, box);
}
ctx.fillStyle = 'red';
ctx.fillRect(food.x, food.y, box, box);
let snakeX = snake[0].x;
let snakeY = snake[0].y;
if (d === 'LEFT') snakeX -= box;
if (d === 'UP') snakeY -= box;
if (d === 'RIGHT') snakeX += box;
if (d === 'DOWN') snakeY += box;
if (snakeX === food.x && snakeY === food.y) {
score++;
food = {
x: Math.floor(Math.random() * canvasSize) * box,
y: Math.floor(Math.random() * canvasSize) * box
}
} else {
snake.pop();
}
let newHead = {
x: snakeX,
y: snakeY
};
if (snakeX < 0 || snakeY < 0 || snakeX >= canvas.width || snakeY >= canvas.height || collision(newHead, snake)) {
clearInterval(game);
}
snake.unshift(newHead);
ctx.fillStyle = 'white';
ctx.font = '45px Changa one';
ctx.fillText(score, 2 * box, 1.6 * box);
}
let game = setInterval(draw, 100);
</script>
</body>
</html>
Этот код строит простую версию игры "Змейка", в которой используются элементы canvas для рисования змейки и еды. Игрок управляет змейкой с помощью клавиш стрелок.