-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimon_says.js
More file actions
93 lines (80 loc) · 2.61 KB
/
simon_says.js
File metadata and controls
93 lines (80 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Define the available colors for the game
const colors = ["blue", "purple", "magenta", "pink"];
let sequence = [];
let playerSequence = [];
let score = 0;
let highScore = localStorage.getItem("simonHighScore") || 0;
let waitingForInput = false; //check if the game is ready for player input
const startBtn = document.getElementById("startBtn");
const scoreDisplay = document.getElementById("score");
const highScoreDisplay = document.getElementById("highScore");
const buttons = document.querySelectorAll(".btn");
// Set initial high score display
if (highScoreDisplay) {
highScoreDisplay.textContent = highScore;
}
// Flash effect for a color
function flashColor(color) {
const btn = document.querySelector(`.btn.${color}`);
btn.classList.add("flash");
setTimeout(() => btn.classList.remove("flash"), 500);
}
// Play the full sequence to the player
function playSequence() {
let i = 0;
const interval = setInterval(() => {
flashColor(sequence[i]);
i++;
if (i >= sequence.length) {
clearInterval(interval);
waitingForInput = true;
}
}, 800);
}
// Generate next color and play sequence
function nextRound() {
const nextColor = colors[Math.floor(Math.random() * 4)];
sequence.push(nextColor);
playerSequence = [];
scoreDisplay.textContent = score;
playSequence();
}
// Handle player input
function handlePlayerClick(e) {
if (!waitingForInput) return;
const clickedColor = e.target.dataset.color;
playerSequence.push(clickedColor);
flashColor(clickedColor);
const currentIndex = playerSequence.length - 1;
if (playerSequence[currentIndex] !== sequence[currentIndex]) { // Check if the clicked color matches the sequence
alert(" Game Over. Your score was: " + score);
resetGame();
return;
}
if (playerSequence.length === sequence.length) { // check if player finished the current sequence correctly
score++;
waitingForInput = false;
// Check and update high score
if (score > highScore) {
highScore = score;
localStorage.setItem("simonHighScore", highScore);
if (highScoreDisplay) {
highScoreDisplay.textContent = highScore;
}
}
setTimeout(nextRound, 1000); // Start the next round after a short delay
}
}
// Reset the game state
function resetGame() {
sequence = [];
playerSequence = [];
score = 0;
scoreDisplay.textContent = score;
}
// Add event listeners to each color button to handle player input
startBtn.addEventListener("click", () => { // Start the game when the player clicks the start button
resetGame();
nextRound();
});
buttons.forEach(btn => btn.addEventListener("click", handlePlayerClick));