|
| 1 | +const gridElement = document.getElementById("grid"); |
| 2 | +const startBtn = document.getElementById("startbtn"); |
| 3 | +const pauseBtn = document.getElementById("pausebtn"); |
| 4 | +const restartBtn = document.getElementById("restartbtn"); |
| 5 | +const scoreElement = document.getElementById("score"); |
| 6 | +const finalscoreElement = document.getElementById("finalscore"); |
| 7 | + |
| 8 | +const welcomeScreen = document.getElementById("welcomescreen"); |
| 9 | +const gameScreen = document.getElementById("gamescreen"); |
| 10 | +const gameoverScreen = document.getElementById("gameoverscreen"); |
| 11 | + |
| 12 | +let squares = []; |
| 13 | +let currentSnake = [2, 1, 0]; |
| 14 | +let direction = 1; |
| 15 | +const width = 10; |
| 16 | +let appleIndex = 0; |
| 17 | +let score = 0; |
| 18 | +let intervalTime = 300; |
| 19 | +let speed = 0.9; |
| 20 | +let timerId; |
| 21 | +let paused = false; |
| 22 | +let started = false; |
| 23 | + |
| 24 | +function createGrid() { |
| 25 | + gridElement.innerHTML = ""; |
| 26 | + squares = []; |
| 27 | + for (let i = 0; i < width * width; i++) { |
| 28 | + const square = document.createElement("div"); |
| 29 | + square.classList.add("square"); |
| 30 | + gridElement.appendChild(square); |
| 31 | + squares.push(square); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function startGame() { |
| 36 | + welcomeScreen.classList.add("hidden"); |
| 37 | + gameoverScreen.classList.add("hidden"); |
| 38 | + gameScreen.classList.remove("hidden"); |
| 39 | + |
| 40 | + createGrid(); |
| 41 | + currentSnake = [2, 1, 0]; |
| 42 | + direction = 1; |
| 43 | + score = 0; |
| 44 | + intervalTime = 400; |
| 45 | + paused = false; |
| 46 | + started = true; |
| 47 | + scoreElement.textContent = `Score: ${score}`; |
| 48 | + pauseBtn.textContent = "Pause"; |
| 49 | + |
| 50 | + squares.forEach((i) => i.classList.remove("snake", "apple")); |
| 51 | + currentSnake.forEach((i) => squares[i].classList.add("snake")); |
| 52 | + |
| 53 | + generateApple(); |
| 54 | + |
| 55 | + clearInterval(timerId); |
| 56 | + timerId = setInterval(move, intervalTime); |
| 57 | +} |
| 58 | + |
| 59 | +function move() { |
| 60 | + if (paused) return; |
| 61 | + |
| 62 | + if ( |
| 63 | + (currentSnake[0] + width >= width * width && direction === width) || |
| 64 | + (currentSnake[0] % width === width - 1 && direction === 1) || |
| 65 | + (currentSnake[0] % width === 0 && direction === -1) || |
| 66 | + (currentSnake[0] - width < 0 && direction === -width) || |
| 67 | + squares[currentSnake[0] + direction].classList.contains("snake") |
| 68 | + ) { |
| 69 | + clearInterval(timerId); |
| 70 | + gameOver(); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const tail = currentSnake.pop(); |
| 75 | + squares[tail].classList.remove("snake"); |
| 76 | + currentSnake.unshift(currentSnake[0] + direction); |
| 77 | + |
| 78 | + if (squares[currentSnake[0]].classList.contains("apple")) { |
| 79 | + squares[currentSnake[0]].classList.remove("apple"); |
| 80 | + squares[tail].classList.add("snake"); |
| 81 | + currentSnake.push(tail); |
| 82 | + generateApple(); |
| 83 | + score++; |
| 84 | + scoreElement.textContent = `Score: ${score}`; |
| 85 | + clearInterval(timerId); |
| 86 | + if (intervalTime > 200) { |
| 87 | + intervalTime = intervalTime * speed; |
| 88 | + } |
| 89 | + timerId = setInterval(move, intervalTime); |
| 90 | + } |
| 91 | + |
| 92 | + squares[currentSnake[0]].classList.add("snake"); |
| 93 | +} |
| 94 | + |
| 95 | +function generateApple() { |
| 96 | + do { |
| 97 | + appleIndex = Math.floor(Math.random() * squares.length); |
| 98 | + } while (squares[appleIndex].classList.contains("snake")); |
| 99 | + squares[appleIndex].classList.add("apple"); |
| 100 | +} |
| 101 | + |
| 102 | +function control(k) { |
| 103 | + if (!started || paused) return; |
| 104 | + if (k.key === "ArrowRight" && direction !== -1) direction = 1; |
| 105 | + else if (k.key === "ArrowUp" && direction !== width) direction = -width; |
| 106 | + else if (k.key === "ArrowLeft" && direction !== 1) direction = -1; |
| 107 | + else if (k.key === "ArrowDown" && direction !== -width) direction = +width; |
| 108 | +} |
| 109 | + |
| 110 | +document.addEventListener("keydown", control); |
| 111 | + |
| 112 | +function stopGame() { |
| 113 | + if (!started) return; |
| 114 | + paused = !paused; |
| 115 | + pauseBtn.textContent = paused ? "Resume" : "Pause"; |
| 116 | +} |
| 117 | + |
| 118 | +function gameOver() { |
| 119 | + started = false; |
| 120 | + gameScreen.classList.add("hidden"); |
| 121 | + gameoverScreen.classList.remove("hidden"); |
| 122 | + finalscoreElement.textContent = `Your score: ${score}`; |
| 123 | +} |
| 124 | + |
| 125 | +startBtn.addEventListener("click", startGame); |
| 126 | +restartBtn.addEventListener("click", startGame); |
| 127 | +pauseBtn.addEventListener("click", stopGame); |
0 commit comments