-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (61 loc) · 1.7 KB
/
script.js
File metadata and controls
75 lines (61 loc) · 1.7 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
btnColors = ["red", "blue", "green", "yellow"];
let rand = () => Math.floor(Math.random() * 4);
let seq = [];
let userSeq = [];
let keypressed = false;
//create seq
function nextSeq() {
let randomChoosenColor = btnColors[rand()];
seq.push(randomChoosenColor);
$("#" + randomChoosenColor).fadeIn(100).fadeOut(100).fadeIn(100);
playSound(randomChoosenColor);
$("h1").text("Level " + seq.length)
}
//key press for first seq
$(document).on("keypress", function() {
if (!keypressed) {
keypressed = !keypressed;
nextSeq();
}
})
$(".btn").on("click", function(event) {
if (!keypressed) {
return;
}
let randColor = event.target.id;
userSeq.push(randColor);
$("#" + randColor).fadeIn(100).fadeOut(100).fadeIn(100);
playSound(randColor);
//logic to catch wrong :
// us[us.l - 1] != seq[us.l - 1] -> wrongState()
// us[us.l - 1] == seq[us.l - 1] -> right
if (userSeq[userSeq.length - 1] != seq[userSeq.length - 1]) {
wrongState();
return;
}
//us.l == seq.l -> nextSeq()
if (userSeq.length == seq.length) {
userSeq.length = 0;
$("body").addClass("level-up");
setTimeout(() => {
$("body").removeClass("level-up");
}, 100);
setTimeout(nextSeq, 1000);
return;
}
})
function playSound(key) {
let audio = new Audio("sounds/" + key + ".mp3");
audio.play();
}
function wrongState() {
seq.length = 0;
userSeq.length = 0;
playSound("wrong");
keypressed = false;
$("h1").text("Game over, Press any key to Restart.")
$("body").addClass("game-over");
setTimeout(() => {
$("body").removeClass("game-over");
}, 100);
}