-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
125 lines (112 loc) · 3.19 KB
/
game.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
let j = 0;
seq = [];
$(document).on("keydown", main);
let i = 0;
let off = true;
$(".container > div").click((event) => {
let i = seq.length - 1;
if (seq.length === 0 || j > i){
$("body").css("background-color", "red");
setTimeout(() => {
$("body").css("background-color", "navy");
}, 500);
playSound();
lost();
return;
}
if (j <= i){
if (seq[j] === $(event.target).attr("id").slice(0,1)){
$(event.target).addClass("lighten");
setTimeout(() => {
$(event.target).removeClass("lighten");
}, 500);
j++;
if (j > i){
setTimeout(main, 1000);
}
return;
}else{
$("body").css("background-color", "red");
setTimeout(() => {
$("body").css("background-color", "navy");
}, 500);
$(event.target).addClass("lighten");
setTimeout(() => {
$(event.target).removeClass("lighten");
}, 500);
playSound();
lost();
return;
}
}
})
function main(){
if (off){
$(document).off("keydown");
off = false;
}
j = 0;
let n = Math.floor(Math.random()* 4);
switch (n){
case 0:
$("#green").addClass("lighten");
seq.push("g");
var audio = new Audio("./sounds/green.mp3");
audio.play();
setTimeout(() => {
$("#green").removeClass("lighten");
}, 500);
changeHeading(seq.length);
break;
case 1:
$("#red").addClass("lighten");
seq.push("r");
var audio = new Audio("./sounds/red.mp3");
audio.play();
setTimeout(() => {
$("#red").removeClass("lighten");
}, 500);
changeHeading(seq.length);
break;
case 2:
$("#yellow").addClass("lighten");
seq.push("y");
var audio = new Audio("./sounds/yellow.mp3");
audio.play();
setTimeout(() => {
$("#yellow").removeClass("lighten");
}, 500);
changeHeading(seq.length);
break;
case 3:
$("#blue").addClass("lighten");
seq.push("b");
var audio = new Audio("./sounds/blue.mp3");
audio.play();
setTimeout(() => {
$("#blue").removeClass("lighten");
}, 500);
changeHeading(seq.length);
break;
}
}
function playSound(){
let audio = new Audio("./sounds/wrong.mp3");
audio.play();
}
function changeHeading(s) {
$("h1").text("Level " + s);
}
function lost(){
$("h1").text("Game Over, Press Any Key to Restart");
j = 0;
removeAll();
$(document).on("keydown", main);
off = true;
}
function removeAll(){
let len = seq.length;
for (let v = 0; v < len; v++){
seq.pop();
}
}