Color Match

Red
Time Left: 1.5s
Press "Start" to play!
**Color Match Challenge** is a fast-paced reaction game where players must select the correct color displayed on the screen before the timer runs out. Each correct answer increases your score and resets the timer for the next round. But be careful—the timer only lasts for 1.5 seconds, and one wrong move ends the game! With vibrant colors and increasing intensity, this game tests your reflexes and color recognition skills. Can you keep up and set a new high score?
Here is the code
CSS
h1 {
  text-align: center;
}

.game-container {
  max-width: 400px;
  margin: 0 auto;
  text-align: center;
}

.color-to-match {
  font-size: 2em;
  font-weight: bold;
  margin: 10px 0;
}

.timer-display {
  font-size: 1.2em;
  margin-top: 10px;
  color: #333;
}

.buttons-container {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 15px 0;
}

.color-btn {
  width: 45px;
  height: 45px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.result-box {
  margin-top: 10px;
  padding: 10px;
  width: 100%;
  font-size: 1em;
  border: 2px solid #333;
  border-radius: 5px;
  background-color: #f1f1f1;
  color: #555;
}

.start-btn {
  margin-top: 10px;
  padding: 10px 20px;
  font-size: 1em;
  border: none;
  border-radius: 5px;
  background-color: #333;
  color: white;
  cursor: pointer;
}

.start-btn:hover {
  background-color: #555;
}
HTML
<h1>Color Match</h1>
<div class="game-container">
  <div id="colorToMatch" class="color-to-match">Red</div>
  <div id="timerDisplay" class="timer-display">Time Left: 1.5s</div>
  <div class="buttons-container" id="buttonsContainer">
    <button class="color-btn" data-color="red" style="background-color: red;"></button>
    <button class="color-btn" data-color="green" style="background-color: green;"></button>
    <button class="color-btn" data-color="blue" style="background-color: blue;"></button>
    <button class="color-btn" data-color="yellow" style="background-color: yellow;"></button>
    <button class="color-btn" data-color="purple" style="background-color: purple;"></button>
  </div>
  <div class="result-box" id="resultBox">Press "Start" to play!</div>
  <button class="start-btn" id="startBtn">Start</button>
</div>
<input type="hidden" id="hiddenScore" value="0" />
JS
const hiddenScore = document.getElementById("hiddenScore");
const colorToMatch = document.getElementById("colorToMatch");
const buttonsContainer = document.getElementById("buttonsContainer");
const resultBox = document.getElementById("resultBox");
const startBtn = document.getElementById("startBtn");
const timerDisplay = document.getElementById("timerDisplay");

let score = 0;
let timer = null;
let timeLeft = 1.5;

const colors = ["red", "green", "blue", "yellow", "purple"];

function startGame() {
  document.querySelector('.share-buttons').style.display = 'none';
  score = 0;
  hiddenScore.value = score;
  resultBox.textContent = "Score: 0";
  timerDisplay.textContent = `Time Left: ${timeLeft}s`;
  startBtn.disabled = true;

  generateNewColor();
  startTimer();
}

function startTimer() {
  timeLeft = 1.5;
  timerDisplay.textContent = `Time Left: ${timeLeft}s`;

  timer = setInterval(() => {
    timeLeft = timeLeft - 0.5;
    timerDisplay.textContent = `Time Left: ${timeLeft}s`;

    if (timeLeft <= 0) {
      endGame(); // Player loses if time runs out
    }
  }, 500);
}

function resetTimer() {
  clearInterval(timer);
  startTimer();
}

function generateNewColor() {
  const randomColor = colors[Math.floor(Math.random() * colors.length)];
  colorToMatch.textContent = randomColor.charAt(0).toUpperCase() + randomColor.slice(1); // Capitalize the first letter
  colorToMatch.style.color = randomColor;
}

function checkMatch(event) {
  const chosenColor = event.target.getAttribute("data-color");
  const correctColor = colorToMatch.style.color;

  if (chosenColor === correctColor) {
    score++;
    hiddenScore.value = score;
    resultBox.textContent = `Score: ${score}`;
    resetTimer(); // Reset the timer for the next round
    generateNewColor();
  } else {
    endGame();
  }
}

function endGame() {
  clearInterval(timer);
  startBtn.disabled = false;
  resultBox.textContent = `Game Over! Final Score: ${score}`;
  document.querySelector('.share-buttons').style.display = 'flex';
  timerDisplay.textContent = ""; // Clear timer display
}

buttonsContainer.addEventListener("click", (event) => {
  if (event.target.classList.contains("color-btn")) {
    checkMatch(event);
  }
});

startBtn.addEventListener("click", startGame);