Вот полный 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 {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
margin: 0;
padding: 20px;
}
.game-container {
border: 2px solid #000;
padding: 10px;
background-color: #fff;
display: inline-block;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
canvas {
border: 1px solid #000;
background-color: #fff;
}
.controls {
margin-top: 10px;
}
button {
padding: 10px 15px;
margin: 5px;
border: none;
border-radius: 5px;
background-color: #007bff;
color: #fff;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<h1>Игра Змейка</h1>
<div class="game-container">
<canvas id="gameCanvas" width="300" height="600"></canvas>
<div class="controls">
<button onclick="changeDirection('up')">↑</button>
<button onclick="changeDirection('down')">↓</button>
<button onclick="changeDirection('left')">←</button>
<button onclick="changeDirection('right')">→</button>
</div>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
let snake = [{ x: 150, y: 300 }];
let direction = 'right';
let food = { x: Math.floor(Math.random() * (canvas.width / 10)) * 10, y: Math.floor(Math.random() * (canvas.height / 10)) * 10 };
let score = 0;
document.addEventListener('keydown', (event) => {
switch (event.key) {
case 'ArrowUp':
changeDirection('up');
break;
case 'ArrowDown':
changeDirection('down');
break;
case 'ArrowLeft':
changeDirection('left');
break;
case 'ArrowRight':
changeDirection('right');
break;
}
});
function changeDirection(newDirection) {
if (newDirection === 'up' && direction !== 'down') direction = 'up';
else if (newDirection === 'down' && direction !== 'up') direction = 'down';
else if (newDirection === 'left' && direction !== 'right') direction = 'left';
else if (newDirection === 'right' && direction !== 'left') direction = 'right';
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawSnake();
drawFood();
moveSnake();
checkCollision();
setTimeout(gameLoop, 100);
}
function drawSnake() {
ctx.fillStyle = 'green';
for (let segment of snake) {
ctx.fillRect(segment.x, segment.y, 10, 10);
}
}
function drawFood() {
ctx.fillStyle = 'red';
ctx.fillRect(food.x, food.y, 10, 10);
}
function moveSnake() {
let head = { x: snake[0].x, y: snake[0].y };
if (direction === 'up') head.y -= 10;
else if (direction === 'down') head.y += 10;
else if (direction === 'left') head.x -= 10;
else if (direction === 'right') head.x += 10;
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score++;
food.x = Math.floor(Math.random() * (canvas.width / 10)) * 10;
food.y = Math.floor(Math.random() * (canvas.height / 10)) * 10;
} else {
snake.pop();
}
}
function checkCollision() {
const head = snake[0];
// Проверка на столкновение с границами
if (head.x < 0 || head.x >= canvas.width || head.y < 0 || head.y >= canvas.height) {
resetGame();
}
// Проверка на столкновение с самим собой
for (let i = 1; i < snake.length; i++) {
if (head.x === snake[i].x && head.y === snake[i].y) {
resetGame();
}
}
}
function resetGame() {
snake = [{ x: 150, y: 300 }];
direction = 'right';
score = 0;
food.x = Math.floor(Math.random() * (canvas.width / 10)) * 10;
food.y = Math.floor(Math.random() * (canvas.height / 10)) * 10;
alert('Игра окончена! Ваш счет: ' + score);
}
gameLoop();
</script>
</body>
</html>
Этот код реализует игру "Змейка" с элементами управления. Игра завершится, если змейка столкнется с границами игрового поля или с самой собой, после чего выведется алерта с вашим счетом. Включены виртуальные кнопки для управления змейкой, а также возможность управления с клавиатуры.