Snake - Game
-Snake - Game
+Description
Eat as many apples and grow as much as possible
◀ or A or arrow left = move left
@@ -20,6 +20,7 @@▲ or W or arrow up = move up
▼ or S or arrow down = move down
diff --git a/secret/snake/game.js b/secret/snake/game.js index d300a86..66100a9 100644 --- a/secret/snake/game.js +++ b/secret/snake/game.js @@ -6,8 +6,7 @@ const downButton = document.getElementById("down"); const leftButton = document.getElementById("left"); const rightButton = document.getElementById("right"); const startButton = document.getElementById("start"); -const aButton = document.getElementById("a"); -const bButton = document.getElementById("b"); +const grid = document.getElementById("grid"); // Game variables let snake; @@ -17,10 +16,17 @@ let isGameRunning = false; // Initialize the game function initGame() { + document.getElementById('grid').style.display = 'grid'; + document.getElementById('title').style.display = 'none'; + document.getElementById('description').style.display = 'none'; snake = [{ x: 10, y: 10 }]; apple = spawnApple(); isGameRunning = true; gameInterval = setInterval(gameLoop, 100); + + // Render grid only during initialization + renderGrid(); + render(); } // Game loop @@ -33,15 +39,14 @@ function gameLoop() { // Spawn an apple at a random position function spawnApple() { return { - x: Math.floor(Math.random() * 20), - y: Math.floor(Math.random() * 20), + x: Math.floor(Math.random() * 10), + y: Math.floor(Math.random() * 10), }; } // Update the snake's position function updateSnake() { // Logic to move the snake based on direction - // This is where you would handle the snake's movement } // Check for collisions with walls, itself, or apple @@ -54,6 +59,23 @@ function render() { // Logic to render the snake and apple on the screen } +// Render the grid +function renderGrid() { + const gridSize = 10; + grid.innerHTML = ''; // Clear the grid first + + for (let y = 0; y < gridSize; y++) { + let row = document.createElement('div'); + row.classList.add('row'); + for (let x = 0; x < gridSize; x++) { + let cell = document.createElement('div'); + cell.classList.add('cell'); + row.appendChild(cell); + } + grid.appendChild(row); + } +} + // Button event listeners upButton.addEventListener("click", () => { // Logic to move the snake up @@ -76,12 +98,3 @@ startButton.addEventListener("click", () => { initGame(); } }); - -// Additional button functionalities for A and B -aButton.addEventListener("click", () => { - // Logic for A button (e.g., pause or special action) -}); - -bButton.addEventListener("click", () => { - // Logic for B button (e.g., reset or another action) -}); diff --git a/secret/snake/index.html b/secret/snake/index.html index b860b0f..56f96b5 100644 --- a/secret/snake/index.html +++ b/secret/snake/index.html @@ -11,8 +11,8 @@
Eat as many apples and grow as much as possible
◀ or A or arrow left = move left
@@ -20,6 +20,7 @@▲ or W or arrow up = move up
▼ or S or arrow down = move down