pages/secret/snake/game.js

133 lines
3.6 KiB
JavaScript
Raw Normal View History

2025-01-05 01:59:00 +01:00
'use strict';
2025-01-06 11:56:05 +01:00
// DOM Elements
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');
const grid = document.createElement('div'); // Create grid dynamically
grid.id = 'grid';
document.querySelector('.game').appendChild(grid);
// Game Variables
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 11:56:05 +01:00
let gridSize = 10;
2025-01-05 01:59:00 +01:00
2025-01-06 11:56:05 +01:00
// Initialize Game
2025-01-05 01:59:00 +01:00
function initGame() {
2025-01-06 11:56:05 +01:00
console.log('Game initialized!');
grid.style.display = 'grid';
grid.style.gridTemplateColumns = 'repeat(10, 1fr)';
grid.style.gridTemplateRows = 'repeat(10, 1fr)';
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-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 };
clearInterval(gameInterval);
gameInterval = setInterval(gameLoop, 200);
2025-01-05 01:59:00 +01:00
render();
}
2025-01-06 11:56:05 +01:00
// Spawn an Apple
2025-01-05 01:59:00 +01:00
function spawnApple() {
return {
2025-01-05 03:20:42 +01:00
x: Math.floor(Math.random() * 10),
y: Math.floor(Math.random() * 10),
2025-01-05 01:59:00 +01:00
};
}
2025-01-06 11:56:05 +01:00
// Update Snake's Position
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,
y: snake[0].y + direction.y,
};
// Wrap snake around edges
if (newHead.x < 0) newHead.x = 9;
if (newHead.y < 0) newHead.y = 9;
if (newHead.x > 9) newHead.x = 0;
if (newHead.y > 9) newHead.y = 0;
// Check collision with itself
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
// Check collision with apple
if (newHead.x === apple.x && newHead.y === apple.y) {
apple = spawnApple();
} else {
snake.pop(); // Remove tail
}
2025-01-05 01:59:00 +01:00
}
2025-01-06 11:56:05 +01:00
// Render Game State
2025-01-05 01:59:00 +01:00
function render() {
2025-01-06 11:56:05 +01:00
const grid = document.getElementById("grid");
grid.innerHTML = ""; // Clear the grid
2025-01-05 03:20:42 +01:00
2025-01-06 11:56:05 +01:00
// Generate the grid
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");
// Alternate colors for a chessboard pattern
if ((x + y) % 2 === 0) {
cell.classList.add("light-green");
} else {
cell.classList.add("dark-green");
}
// Check if the cell is part of the snake
if (snake.some(segment => segment.x === x && segment.y === y)) {
cell.classList.add("snake");
}
// Check if the cell is the apple
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
// Game Loop
function gameLoop() {
if (!isGameRunning) return;
updateSnake();
render();
}
2025-01-05 01:59:00 +01:00
2025-01-06 11:56:05 +01:00
// Handle Game Over
function gameOver() {
alert('Game Over!');
clearInterval(gameInterval);
isGameRunning = false;
}
2025-01-05 01:59:00 +01:00
2025-01-06 11:56:05 +01:00
// Event Listeners for Buttons
upButton.addEventListener('click', () => direction = { x: 0, y: -1 });
downButton.addEventListener('click', () => direction = { x: 0, y: 1 });
leftButton.addEventListener('click', () => direction = { x: -1, y: 0 });
rightButton.addEventListener('click', () => direction = { x: 1, y: 0 });
startButton.addEventListener('click', () => {
if (!isGameRunning) initGame();
2025-01-05 01:59:00 +01:00
});