pages/secret/snake/game.js

145 lines
3.9 KiB
JavaScript
Raw Normal View History

2025-01-05 01:59:00 +01:00
'use strict';
2025-01-06 11:56:05 +01:00
const upButton = document.getElementById('up');
const downButton = document.getElementById('down');
const leftButton = document.getElementById('left');
const rightButton = document.getElementById('right');
const startButton = document.getElementById('start');
2025-01-06 12:04:53 +01:00
const grid = document.createElement('div');
2025-01-06 11:56:05 +01:00
grid.id = 'grid';
document.querySelector('.game').appendChild(grid);
let snake = [{ x: 5, y: 5 }];
let apple = { x: 8, y: 5 };
let direction = { x: 0, y: 0 };
let gameInterval = null;
2025-01-05 01:59:00 +01:00
let isGameRunning = false;
2025-01-06 12:04:53 +01:00
const gridSize = 10;
2025-01-05 01:59:00 +01:00
function initGame() {
2025-01-06 11:56:05 +01:00
grid.style.display = 'grid';
2025-01-06 12:04:53 +01:00
grid.style.gridTemplateColumns = `repeat(${gridSize}, 1fr)`;
grid.style.gridTemplateRows = `repeat(${gridSize}, 1fr)`;
2025-01-06 11:56:05 +01:00
grid.style.width = '350px';
grid.style.height = '350px';
2025-01-05 03:20:42 +01:00
document.getElementById('title').style.display = 'none';
document.getElementById('description').style.display = 'none';
2025-01-06 12:04:53 +01:00
2025-01-05 01:59:00 +01:00
isGameRunning = true;
2025-01-06 11:56:05 +01:00
snake = [{ x: 5, y: 5 }];
apple = spawnApple();
direction = { x: 0, y: 0 };
2025-01-06 12:04:53 +01:00
2025-01-06 11:56:05 +01:00
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, 200);
2025-01-05 01:59:00 +01:00
render();
}
function spawnApple() {
2025-01-06 12:04:53 +01:00
let newApple;
do {
newApple = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize)
};
} while (snake.some(segment => segment.x === newApple.x && segment.y === newApple.y));
return newApple;
2025-01-05 01:59:00 +01:00
}
function updateSnake() {
2025-01-06 11:56:05 +01:00
const newHead = {
x: snake[0].x + direction.x,
2025-01-06 12:04:53 +01:00
y: snake[0].y + direction.y
2025-01-06 11:56:05 +01:00
};
2025-01-06 12:04:53 +01:00
if (newHead.x < 0) newHead.x = gridSize - 1;
if (newHead.y < 0) newHead.y = gridSize - 1;
if (newHead.x >= gridSize) newHead.x = 0;
if (newHead.y >= gridSize) newHead.y = 0;
2025-01-06 11:56:05 +01:00
if (snake.some(segment => segment.x === newHead.x && segment.y === newHead.y)) {
gameOver();
return;
}
snake.unshift(newHead);
2025-01-05 01:59:00 +01:00
2025-01-06 11:56:05 +01:00
if (newHead.x === apple.x && newHead.y === apple.y) {
apple = spawnApple();
} else {
2025-01-06 12:04:53 +01:00
snake.pop();
2025-01-06 11:56:05 +01:00
}
2025-01-05 01:59:00 +01:00
}
function render() {
2025-01-06 12:04:53 +01:00
grid.innerHTML = "";
2025-01-05 01:59:00 +01:00
2025-01-05 03:20:42 +01:00
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
2025-01-06 11:56:05 +01:00
const cell = document.createElement("div");
cell.classList.add("cell");
if ((x + y) % 2 === 0) {
cell.classList.add("light-green");
} else {
cell.classList.add("dark-green");
}
if (snake.some(segment => segment.x === x && segment.y === y)) {
cell.classList.add("snake");
}
if (apple.x === x && apple.y === y) {
cell.classList.add("apple");
}
grid.appendChild(cell);
2025-01-05 03:20:42 +01:00
}
}
}
2025-01-06 11:56:05 +01:00
function gameLoop() {
if (!isGameRunning) return;
updateSnake();
render();
}
2025-01-05 01:59:00 +01:00
2025-01-06 11:56:05 +01:00
function gameOver() {
alert('Game Over!');
clearInterval(gameInterval);
isGameRunning = false;
}
2025-01-05 01:59:00 +01:00
2025-01-06 12:04:53 +01:00
function handleDirectionInput(key) {
switch (key) {
case 'ArrowUp':
case 'w':
if (direction.y === 0) direction = { x: 0, y: -1 };
break;
case 'ArrowDown':
case 's':
if (direction.y === 0) direction = { x: 0, y: 1 };
break;
case 'ArrowLeft':
case 'a':
if (direction.x === 0) direction = { x: -1, y: 0 };
break;
case 'ArrowRight':
case 'd':
if (direction.x === 0) direction = { x: 1, y: 0 };
break;
}
}
upButton.addEventListener('click', () => handleDirectionInput('ArrowUp'));
downButton.addEventListener('click', () => handleDirectionInput('ArrowDown'));
leftButton.addEventListener('click', () => handleDirectionInput('ArrowLeft'));
rightButton.addEventListener('click', () => handleDirectionInput('ArrowRight'));
2025-01-06 11:56:05 +01:00
startButton.addEventListener('click', () => {
if (!isGameRunning) initGame();
2025-01-05 01:59:00 +01:00
});
2025-01-06 12:04:53 +01:00
document.addEventListener('keydown', (event) => {
handleDirectionInput(event.key);
});