-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimon.js
More file actions
106 lines (86 loc) · 2.19 KB
/
Copy pathsimon.js
File metadata and controls
106 lines (86 loc) · 2.19 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
let gmaeSeq=[];
let userSeq=[];
let level=0;
let heighScore=0;
let started=true;
let btns=["one","two","three","four"];
let h2=document.querySelector("h2");
let h3=document.querySelector("h3");
let body=document.querySelector("body");
//start after key preee
document.addEventListener("keypress",function(){
if(started==true){
started=false;
// console.log("started");
h2.innerText=`Level ${level}`;
levelUp();
}
});
//starting the next level
function levelUp(){
level++;
h2.innerText=`Level ${level}`;
let btn=ranBtn();
blink(btn);
gmaeSeq.push(btn);
// console.log(gmaeSeq);
}
//button are blinking
function blink(btn){
let bt=document.querySelector(`#${btn}`);
bt.classList.add("blink");
setTimeout(() => {
bt.classList.remove("blink");
}, 250);
}
//selecting the random button
function ranBtn(){
let ranIdx=Math.floor(Math.random()*4);
let btn=btns[ranIdx];
return btn;
}
//check the user which button press
for(btn of btns){
let bt=document.querySelector(`.${btn}`);
bt.addEventListener("click",function(){
let btn=bt.getAttribute("id");
blink(btn);
userSeq.push(btn);
checkAns(userSeq.length-1);
})
}
//check the user answer
function checkAns(indx){
if(gmaeSeq.length==0){
h2.innerText="PLEASE STRAT GAME ! Press any Key to start game";
h2.style.color="red";
setTimeout(() => {
h2.style.color="black";
}, 1000);
}else if(userSeq[indx]==gmaeSeq[indx]){
if(indx==gmaeSeq.length-1){
setTimeout(() => {
levelUp();
}, 700);
userSeq=[];
}
}else{
h2.innerText="Gmae Over ! Press any Key to start game";
body.style.backgroundColor="red";
setTimeout(()=>{
body.style.backgroundColor="white";
},250);
if(level-1>heighScore){
heighScore=level-1;
h3.innerText=`Your High Score ${level}`;
}
rest();
}
}
//after wrong answer rest some value
function rest(){
userSeq=[];
gmaeSeq=[];
level=0;
started=true;
}