|
| 1 | +const colors = ["green", "red", "yellow", "blue"]; |
| 2 | +let gamePattern = []; |
| 3 | +let userPattern = []; |
| 4 | +let level = 0; |
| 5 | +let clickCount = 0; |
| 6 | +let gameStarted = false; |
| 7 | + |
| 8 | +document.getElementById("start-btn").addEventListener("click", startGame); |
| 9 | + |
| 10 | +function startGame() { |
| 11 | + if (!gameStarted) { |
| 12 | + gameStarted = true; |
| 13 | + level = 0; |
| 14 | + gamePattern = []; |
| 15 | + userPattern = []; |
| 16 | + clickCount = 0; |
| 17 | + document.getElementById("status").textContent = `Level ${level}`; |
| 18 | + document.getElementById("click-count").textContent = clickCount; |
| 19 | + |
| 20 | + showMyTexts(); |
| 21 | + nextSequence(); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function nextSequence() { |
| 26 | + userPattern = []; |
| 27 | + clickCount = 0; |
| 28 | + document.getElementById("click-count").textContent = clickCount; |
| 29 | + level++; |
| 30 | + document.getElementById("status").textContent = `Level ${level}`; |
| 31 | + |
| 32 | + // Clear the sequence display at the start of each level |
| 33 | + document.getElementById("sequence-display").textContent = "-"; |
| 34 | + |
| 35 | + // Generate a random color and push it to the gamePattern |
| 36 | + const randomColor = colors[Math.floor(Math.random() * colors.length)]; |
| 37 | + gamePattern.push(randomColor); |
| 38 | + |
| 39 | + animateSequence(); |
| 40 | +} |
| 41 | + |
| 42 | +function animateSequence() { |
| 43 | + let i = 0; |
| 44 | + const interval = setInterval(() => { |
| 45 | + flashButton(gamePattern[i]); |
| 46 | + i++; |
| 47 | + if (i === gamePattern.length) { |
| 48 | + clearInterval(interval); |
| 49 | + enableUserInput(); |
| 50 | + } |
| 51 | + }, 600); |
| 52 | +} |
| 53 | + |
| 54 | +function flashButton(color) { |
| 55 | + const button = document.getElementById(color); |
| 56 | + button.classList.add("active"); |
| 57 | + setTimeout(() => { |
| 58 | + button.classList.remove("active"); |
| 59 | + }, 300); |
| 60 | +} |
| 61 | + |
| 62 | +function enableUserInput() { |
| 63 | + colors.forEach((color) => { |
| 64 | + document.getElementById(color).addEventListener("click", handleUserClick); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +function disableUserInput() { |
| 69 | + colors.forEach((color) => { |
| 70 | + document |
| 71 | + .getElementById(color) |
| 72 | + .removeEventListener("click", handleUserClick); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +function handleUserClick(event) { |
| 77 | + const clickedColor = event.target.id; |
| 78 | + userPattern.push(clickedColor); |
| 79 | + flashButton(clickedColor); |
| 80 | + clickCount++; |
| 81 | + document.getElementById("click-count").textContent = clickCount; |
| 82 | + |
| 83 | + // Update the current sequence display and set the text color to the box clicked |
| 84 | + const sequenceDisplay = document.getElementById("sequence-display"); |
| 85 | + sequenceDisplay.innerHTML = ""; // Clear the previous content |
| 86 | + |
| 87 | + // Iterate through the userPattern and display each color with its own color |
| 88 | + userPattern.forEach((color) => { |
| 89 | + const span = document.createElement("span"); |
| 90 | + span.style.color = color; // Set the color of the span to match the button color |
| 91 | + span.textContent = color.toUpperCase() + " "; // Convert the color name to uppercase |
| 92 | + sequenceDisplay.appendChild(span); // Append the span to the display |
| 93 | + }); |
| 94 | + |
| 95 | + checkAnswer(userPattern.length - 1); |
| 96 | +} |
| 97 | + |
| 98 | +function checkAnswer(currentLevel) { |
| 99 | + if (userPattern[currentLevel] === gamePattern[currentLevel]) { |
| 100 | + if (userPattern.length === gamePattern.length) { |
| 101 | + disableUserInput(); |
| 102 | + setTimeout(() => { |
| 103 | + // Show the congrats message when the level is passed |
| 104 | + showCongratsMessage(); |
| 105 | + setTimeout(() => { |
| 106 | + hideCongratsMessage(); |
| 107 | + setTimeout(() => {}, 1000); // Delay before starting the next level |
| 108 | + nextSequence(); |
| 109 | + }, 2000); // Hide congrats message after 2 seconds |
| 110 | + }, 1000); |
| 111 | + } |
| 112 | + } else { |
| 113 | + document.getElementById("status").textContent = `Game Over!`; |
| 114 | + |
| 115 | + setTimeout(() => { |
| 116 | + flashButton(missedColor); |
| 117 | + }, 1000); |
| 118 | + setTimeout(() => { |
| 119 | + hideMyTexts(); |
| 120 | + }, 1000); |
| 121 | + |
| 122 | + showLoseMessage(); |
| 123 | + setTimeout(() => { |
| 124 | + hideLoseMessage(); |
| 125 | + }, 2000); |
| 126 | + |
| 127 | + // Disable further input |
| 128 | + gameStarted = false; |
| 129 | + |
| 130 | + // Reset the game state after a brief delay |
| 131 | + setTimeout(() => { |
| 132 | + level = 0; |
| 133 | + gamePattern = []; |
| 134 | + document.getElementById("sequence-display").textContent = "-"; |
| 135 | + document.getElementById("click-count").textContent = 0; |
| 136 | + }, 1500); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +function showCongratsMessage() { |
| 141 | + const message = `Congrats! You passed level ${level}!`; |
| 142 | + const congratsMessageElement = document.getElementById("level-message"); |
| 143 | + congratsMessageElement.textContent = message; |
| 144 | + congratsMessageElement.style.display = "block"; // Ensure it's displayed |
| 145 | + setTimeout(() => { |
| 146 | + congratsMessageElement.classList.add("show"); // Show the message with animation |
| 147 | + }, 50); // Small delay to trigger the animation |
| 148 | +} |
| 149 | + |
| 150 | +function hideCongratsMessage() { |
| 151 | + const congratsMessageElement = document.getElementById("level-message"); |
| 152 | + congratsMessageElement.classList.remove("show"); // Remove animation class |
| 153 | + setTimeout(() => { |
| 154 | + congratsMessageElement.style.display = "none"; // Hide the message after animation |
| 155 | + }, 1000); // Delay hiding it after the fade-out effect |
| 156 | +} |
| 157 | + |
| 158 | +function showLoseMessage() { |
| 159 | + const message = `Game Over! Correct color was ${ |
| 160 | + gamePattern[userPattern.length - 1] |
| 161 | + }.`; |
| 162 | + const loseMessageElement = document.getElementById("level-message"); |
| 163 | + loseMessageElement.textContent = message; |
| 164 | + // set color to red |
| 165 | + loseMessageElement.style.color = "red"; |
| 166 | + loseMessageElement.style.display = "block"; // Ensure it's displayed |
| 167 | + setTimeout(() => { |
| 168 | + loseMessageElement.classList.add("show"); // Show the message with animation |
| 169 | + }, 50); // Small delay to trigger the animation |
| 170 | +} |
| 171 | + |
| 172 | +function hideLoseMessage() { |
| 173 | + const loseMessageElement = document.getElementById("level-message"); |
| 174 | + loseMessageElement.classList.remove("show"); // Remove animation class |
| 175 | + setTimeout(() => { |
| 176 | + loseMessageElement.style.display = "none"; // Hide the message after animation |
| 177 | + }, 1000); // Delay hiding it after the fade-out effect |
| 178 | +} |
| 179 | + |
| 180 | +function showMyTexts() { |
| 181 | + const texts = document.getElementsByClassName("my-text"); |
| 182 | + |
| 183 | + for (let i = 0; i < texts.length; i++) { |
| 184 | + texts[i].style.display = "block"; |
| 185 | + texts[i].classList.add("show"); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +function hideMyTexts() { |
| 190 | + const texts = document.getElementsByClassName("my-text"); |
| 191 | + |
| 192 | + for (let i = 0; i < texts.length; i++) { |
| 193 | + texts[i].classList.remove("show"); |
| 194 | + texts[i].style.display = "none"; |
| 195 | + } |
| 196 | +} |
0 commit comments