-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimon.js
103 lines (83 loc) · 3.08 KB
/
simon.js
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
94
95
96
97
98
99
100
101
102
103
let gameseq = [];
let userseq = [];
let btns = ["yellow", "red", "purple", "green"];
let started = false;
let level = 0;
let highestScore = 0;
let h2 = document.querySelector("h2");
let h1 = document.querySelector("h1");
let h3 = document.querySelector("h3");
function highscore() {
if (level > highestScore) {
highestScore = level; // Update the highestScore with the current level
localStorage.setItem("highestScore", highestScore); // Save to localStorage
}
}
window.onload = function () {
highestScore = parseInt(localStorage.getItem("highestScore")) || 0; // Load the highest score from localStorage
document.getElementById("highest-score").innerText = `Highest Score: ${highestScore}`;
};
// Replace keypress event with button click event for game start
document.getElementById("start-btn").addEventListener("click", function () {
if (!started) {
console.log("Game is started");
started = true;
levelUp();
}
});
function gameFlash(btn) {
btn.classList.add("flash");
setTimeout(function () {
btn.classList.remove("flash"); // After 250ms, remove the flash class
}, 250);
}
function userFlash(btn) {
btn.classList.add("userflash");
setTimeout(function () {
btn.classList.remove("userflash"); // After 250ms, remove the userflash class
}, 250);
}
function levelUp() {
userseq = []; // Reset user sequence when a new level starts
level++;
console.log(level);
h2.innerText = `Level ${level}`;
let randIdx = Math.floor(Math.random() * 4); // Generate random index (0-3)
let randColor = btns[randIdx]; // Get the corresponding color
let randBtn = document.querySelector(`.${randColor}`); // Select that button
gameseq.push(randColor); // Add the random color to the game sequence
gameFlash(randBtn); // Flash the random button
}
function checkAns(idx) {
if (userseq[idx] === gameseq[idx]) {
// If the user's choice matches the game sequence
if (userseq.length === gameseq.length) {
setTimeout(levelUp, 1000); // Move to the next level if the user completed the current level
}
} else {
h2.innerHTML = `Game Over! Your Score is <b>${level}</b> <br> Press the button to Start again`;
document.querySelector("body").style.backgroundColor = "red"; // Flash the screen red on wrong input
setTimeout(function () {
document.querySelector("body").style.backgroundColor = "white"; // Reset the background color
}, 150);
highscore(); // Check and update high score
reset(); // Reset the game
}
}
function btnPress() {
let btn = this;
userFlash(btn);
let userColor = btn.getAttribute("id"); // Fetch the color from the button's id
userseq.push(userColor); // Add to the user's sequence
checkAns(userseq.length - 1); // Check if the user's sequence is correct
}
let allBtns = document.querySelectorAll(".btn");
for (let btn of allBtns) {
btn.addEventListener("click", btnPress); // Add event listeners to all buttons
}
function reset() {
started = false;
userseq = [];
gameseq = [];
level = 0;
}