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;
}
<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" />
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);