-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
106 lines (94 loc) · 3.03 KB
/
Copy pathscript.js
File metadata and controls
106 lines (94 loc) · 3.03 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
94
95
96
97
98
99
100
101
102
103
104
105
106
// Selecting required elements
const countValue = document.querySelector(".levelcss"); // Level display
const colorPart = document.querySelectorAll(".red, .green, .blue, .yellow"); // Color buttons
const startButton = document.querySelector("button"); // Start button
const result = document.querySelector(".Level"); // Result display
// Mapping colors with their original and flash colors
const colors = {
red: {
current: "#950303",
new: "#fd2a2a",
},
green: {
current: "#068e06",
new: "#11e711",
},
blue: {
current: "#01018a",
new: "#2062fc",
},
yellow: {
current: "#919102",
new: "#fafa18",
},
};
let randomColors = [];
let pathGeneratorBool = false;
let count = 0, clickCount = 0;
// Function to start the game
startButton.addEventListener("click", () => {
count = 1; // Start from level 1
clickCount = 0;
randomColors = [];
pathGeneratorBool = false;
countValue.innerText = "01"; // Display Level 1
pathGenerate();
});
// Function to generate the color sequence
const pathGenerate = () => {
randomColors.push(generateRandomValue(colors));
count = randomColors.length; // Update count based on sequence length
countValue.innerText = count < 10 ? "0" + count : count; // Update UI
pathGeneratorBool = true;
pathDecide();
};
// Function to get a random color
const generateRandomValue = (obj) => {
let arr = Object.keys(obj);
return arr[Math.floor(Math.random() * arr.length)];
};
// Function to display the color sequence
const pathDecide = async () => {
for (let i of randomColors) {
let currentColor = document.querySelector(`.${i}`);
await delay(500);
currentColor.style.backgroundColor = colors[i]["new"];
await delay(600);
currentColor.style.backgroundColor = colors[i]["current"];
await delay(600);
}
pathGeneratorBool = false; // Allow user to click after sequence is shown
};
// Function for delay (blinking effect)
async function delay(time) {
return new Promise((resolve) => {
setTimeout(resolve, time);
});
}
// Event listener for user clicks
colorPart.forEach((element) => {
element.addEventListener("click", async (e) => {
if (pathGeneratorBool) return false; // Prevent clicking during sequence display
if (e.target.classList.contains(randomColors[clickCount])) {
// Blink effect on click
e.target.style.backgroundColor = colors[randomColors[clickCount]]["new"];
await delay(500);
e.target.style.backgroundColor = colors[randomColors[clickCount]]["current"];
// Move to next step in sequence
clickCount += 1;
// If user correctly follows sequence, start next level
if (clickCount === count) {
clickCount = 0; // Reset for next round
await delay(1000); // Small delay before next sequence
pathGenerate(); // Generate new sequence
}
} else {
lose();
}
});
});
// Function when player makes a mistake
const lose = () => {
result.innerHTML = `<span> Your Score: </span> ${count - 1}`; // Show last completed level
startButton.innerText = "Play Again";
};