-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
75 lines (70 loc) · 1.98 KB
/
script.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
let title = document.querySelector("#level-title");
let container = document.querySelector(".container");
let colors = ["green", "blue", "yellow", "red"];
let gamePattern = [];
let userPattern = [];
let level = 0;
let index = 0;
let gameOn = false;
document.addEventListener("keydown", function() {
if (level == 0) {
level++;
gameOn = true;
nextSequence();
}
})
container.addEventListener("click", function(e) {
if(e.target.attributes[0].value == 'button' && gameOn) {
let color = e.target.classList[0];
e.target.classList.add('pressed');
playSound(color);
setTimeout(() => {
e.target.classList.remove('pressed');
}, 100)
userPattern.push(color);
console.log(userPattern + " User");
if (userPattern[index] == gamePattern[index]) {
index++;
if (index == gamePattern.length) {
userPattern = [];
index = 0;
level++;
setTimeout(() => {
nextSequence();;
}, 300)
}
}
else {
title.innerText = 'Game Over!!!';
gameOn = false;
playSound("wrong");
gamePattern = [];
userPattern = [];
index = 0;
setTimeout(() => {
level = 0;
title.innerText = 'Press any key to restart again';
}, 2000);
}
}
})
function nextSequence() {
title.innerText = "Level " + level;
let randomNumber = Math.floor(Math.random() * 4);
let color = colors[randomNumber];
gamePattern.push(color);
console.log(gamePattern + " Game");
document.querySelector('.' + color).animate(
[
{opacity: 0},
{opacity: 1},
],
{duration: 500,
iterations: 1}
);
playSound(color);
}
function playSound(color) {
var audio = new Audio('./assets/' + color + '.mp3');
audio.play();
}