-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (57 loc) · 1.63 KB
/
index.js
File metadata and controls
69 lines (57 loc) · 1.63 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
const color = ['red','blue','green','yellow'];
const compData = [];
let level = 1;
let index = 0;
let started = 0;
function showCompData(){
let ran = Math.ceil(Math.random() * 4);
compData.push(ran);
setTimeout(()=>{
showColor(ran);
},1000)
}
document.addEventListener("keypress", () => {
if(!started){
started = 1;
document.querySelector(".h1").innerHTML = `Level ${level}`;
showCompData();
for (let i = 0; i < 4; i++) {
document.querySelectorAll("button")[i].addEventListener("click", () => {
showColor(i+1);
checkInput(i+1);
});
}
}});
function checkInput(clickedIndex){
if (clickedIndex === compData[index]) {
index++;
if(index === compData.length){
index = 0;
level++;
document.querySelector(".h1").innerHTML = `Level ${level}`;
showCompData();
}
}
else {
document.querySelector("body").style.backgroundColor = "red";
let audio = new Audio(`./sounds/wrong.mp3`);
audio.play();
index = 0;
started = 0;
level = 1;
document.querySelector("h1").innerHTML ="Game Over , Press any key to start it again";
setTimeout(() => {
document.querySelector("body").style.backgroundColor = "rgb(37, 37, 37)";
}, 200);
}
}
function showColor(x) {
if(started){
let audio = new Audio(`./sounds/${color[x-1]}.mp3`);
audio.play();
document.querySelectorAll("button")[x-1].style.backgroundColor = "black";
setTimeout(function () {
document.querySelectorAll("button")[x-1].style.backgroundColor = `${color[x-1]}`;
}, 100);
}
}