|
| 1 | +const playBoard = document.querySelector(".play-board"); |
| 2 | +let foodX, foodY; |
| 3 | +let snakeX = 5, snakeY = 10; |
| 4 | +let snakeBody = []; |
| 5 | +let velocityX = 0, velocityY = 0; |
| 6 | +let score = 0; |
| 7 | +let highScore = localStorage.getItem("highScore") || 0; |
| 8 | +let gameInterval; |
| 9 | +let GameOver = false; |
| 10 | +let gameSpeed = 125; // Default game speed |
| 11 | + |
| 12 | +// Function to change the food position to a random location |
| 13 | +const changeFoodPosition = () => { |
| 14 | + foodX = Math.floor(Math.random() * 30) + 1; |
| 15 | + foodY = Math.floor(Math.random() * 30) + 1; |
| 16 | +} |
| 17 | + |
| 18 | +// Function to change the direction of the snake based on arrow keys |
| 19 | +const changeDirection = (e) => { |
| 20 | + // Prevent the snake from reversing direction |
| 21 | + if (e.key === "ArrowUp" && velocityY !== 1) { |
| 22 | + velocityX = 0; |
| 23 | + velocityY = -1; |
| 24 | + } else if (e.key === "ArrowDown" && velocityY !== -1) { |
| 25 | + velocityX = 0; |
| 26 | + velocityY = 1; |
| 27 | + } else if (e.key === "ArrowRight" && velocityX !== -1) { |
| 28 | + velocityX = 1; |
| 29 | + velocityY = 0; |
| 30 | + } else if (e.key === "ArrowLeft" && velocityX !== 1) { |
| 31 | + velocityX = -1; |
| 32 | + velocityY = 0; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Function to display game over message and stop the game |
| 37 | +const gameOver = () => { |
| 38 | + clearInterval(gameInterval); |
| 39 | + GameOver = true; |
| 40 | + document.getElementById("finalScore").textContent = score; |
| 41 | + document.getElementById("gameOverModal").style.display = "flex"; |
| 42 | +} |
| 43 | + |
| 44 | +// Function to restart the game |
| 45 | +const restartGame = () => { |
| 46 | + // Reset game state |
| 47 | + snakeX = 5; |
| 48 | + snakeY = 10; |
| 49 | + snakeBody = []; |
| 50 | + velocityX = 0; |
| 51 | + velocityY = 0; |
| 52 | + score = 0; |
| 53 | + GameOver = false; |
| 54 | + |
| 55 | + // Update the score display |
| 56 | + document.querySelector(".score").textContent = `Score: ${score}`; |
| 57 | + document.querySelector(".highscore").textContent = `High Score: ${highScore}`; |
| 58 | + |
| 59 | + // Hide the game over modal |
| 60 | + document.getElementById("gameOverModal").style.display = "none"; |
| 61 | + |
| 62 | + // Show difficulty selection modal |
| 63 | + document.getElementById("difficultyModal").style.display = "flex"; |
| 64 | +} |
| 65 | + |
| 66 | +// Function to start the game based on selected difficulty |
| 67 | +const startGame = (difficulty) => { |
| 68 | + switch (difficulty) { |
| 69 | + case 'easy': |
| 70 | + gameSpeed = 200; // Slower speed for easy |
| 71 | + break; |
| 72 | + case 'medium': |
| 73 | + gameSpeed = 125; // Medium speed |
| 74 | + break; |
| 75 | + case 'hard': |
| 76 | + gameSpeed = 75; // Faster speed for hard |
| 77 | + break; |
| 78 | + } |
| 79 | + |
| 80 | + // Hide the difficulty modal |
| 81 | + document.getElementById("difficultyModal").style.display = "none"; |
| 82 | + |
| 83 | + // Initialize the food position |
| 84 | + changeFoodPosition(); |
| 85 | + |
| 86 | + // Start the game with the selected speed |
| 87 | + gameInterval = setInterval(initGame, gameSpeed); |
| 88 | +} |
| 89 | + |
| 90 | +// Main function to update the game state |
| 91 | +const initGame = () => { |
| 92 | + // Save the current tail position |
| 93 | + let tailX = snakeBody.length ? snakeBody[snakeBody.length - 1][0] : snakeX; |
| 94 | + let tailY = snakeBody.length ? snakeBody[snakeBody.length - 1][1] : snakeY; |
| 95 | + |
| 96 | + // Update snake's head position based on current velocity |
| 97 | + snakeX += velocityX; |
| 98 | + snakeY += velocityY; |
| 99 | + |
| 100 | + // Check for border collision (game over condition) |
| 101 | + if (snakeX < 1 || snakeX > 30 || snakeY < 1 || snakeY > 30) { |
| 102 | + gameOver(); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Check for self-collision (game over condition) |
| 107 | + for (let i = 0; i < snakeBody.length; i++) { |
| 108 | + if (snakeX === snakeBody[i][0] && snakeY === snakeBody[i][1]) { |
| 109 | + gameOver(); |
| 110 | + return; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Check if the snake has reached the food |
| 115 | + if (snakeX === foodX && snakeY === foodY) { |
| 116 | + changeFoodPosition(); // Move the food to a new position |
| 117 | + // Add a new segment to the snake's body at the tail's previous position |
| 118 | + snakeBody.push([tailX, tailY]); |
| 119 | + score++; |
| 120 | + if (score > highScore) { |
| 121 | + highScore = score; |
| 122 | + localStorage.setItem("highScore", highScore); |
| 123 | + } |
| 124 | + document.querySelector(".score").textContent = `Score: ${score}`; |
| 125 | + document.querySelector(".highscore").textContent = `High Score: ${highScore}`; |
| 126 | + } |
| 127 | + |
| 128 | + // Move each part of the snake's body to follow the segment in front of it |
| 129 | + for (let i = snakeBody.length - 1; i > 0; i--) { |
| 130 | + snakeBody[i] = snakeBody[i - 1]; |
| 131 | + } |
| 132 | + // Update the first segment of the snake's body to the previous head position |
| 133 | + if (snakeBody.length > 0) { |
| 134 | + snakeBody[0] = [snakeX - velocityX, snakeY - velocityY]; |
| 135 | + } |
| 136 | + |
| 137 | + // Render the food and the snake's head on the grid |
| 138 | + let htmlMarkup = `<div class="food" style='grid-area: ${foodY} / ${foodX}'></div>`; |
| 139 | + htmlMarkup += `<div class="head" style='grid-area: ${snakeY} / ${snakeX}'></div>`; |
| 140 | + |
| 141 | + // Render the snake's body on the grid |
| 142 | + for (let i = 0; i < snakeBody.length; i++) { |
| 143 | + htmlMarkup += `<div class="body" style='grid-area: ${snakeBody[i][1]} / ${snakeBody[i][0]}'></div>`; |
| 144 | + } |
| 145 | + |
| 146 | + // Update the play board with the new positions |
| 147 | + playBoard.innerHTML = htmlMarkup; |
| 148 | +} |
| 149 | + |
| 150 | +// Show the difficulty selection modal on page load |
| 151 | +document.getElementById("difficultyModal").style.display = "flex"; |
| 152 | + |
| 153 | +// Add event listener for keypress to change the snake's direction |
| 154 | +document.addEventListener('keydown', changeDirection); |
0 commit comments