-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
85 lines (65 loc) · 1.68 KB
/
sketch.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
let gameSeq = [];
let userSeq = [];
let btns = ['pink','orange','blue','green'];
let started = false;
let level = 0;
let guide = document.querySelector('.guide')
document.addEventListener('keypress',function(){
if(started == false){
started = true;
setInterval(levelUp(),5000);
}
})
let allbtn = document.querySelectorAll('.btn');
for(btn of allbtn){
btn.addEventListener('click', btnPress);
}
function levelUp(){
userSeq=[];
level++;
guide.innerHTML = `Level ${level}`;
let ranIdx = Math.floor(Math.random() * 3);
let ranCol = btns[ranIdx];
let ranBtn = document.querySelector(`.${ranCol}`);
gameSeq.push(ranCol);
gameFlash(ranBtn);
}
function checkAns(idx) {
if(userSeq[idx] === gameSeq[idx]){
if(userSeq.length === gameSeq.length){
setTimeout(levelUp,1000);
}
}else{
document.querySelector('body').style.backgroundColor = 'red';
reset();
}
}
function gameFlash(btn){
btn.classList.add('flash');
setTimeout(function (){
btn.classList.remove('flash');
},200);
}
function userFlash(btn){
btn.classList.add('userFlash');
setTimeout(function (){
btn.classList.remove('userFlash');
},200);
}
function btnPress(){
let btn = this;
userFlash(btn);
userColor = btn.getAttribute('id');
userSeq.push(userColor);
checkAns(userSeq.length-1);
}
function reset(){
guide.innerHTML = `Game Over! Your score was </b>${level}</b> <br>Press any key to start.`;
gameSeq=[];
userSeq=[];
started=false;
level=0;
setTimeout(function(){
document.querySelector("body").style.backgroundColor = '#DDDDDD';
}, 150);
}