-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenius.js
210 lines (194 loc) · 6.56 KB
/
genius.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Global Variables
let userPoints = 0;
const notes = ['c', 'e', 'g', 'a'];
let random4 = () => Math.round(Math.random() * 3);
let melodyTry = [];
let newMel = [];
let playButton = document.querySelectorAll('.btn');
let redBtn = document.querySelector('.red');
let yellowBtn = document.querySelector('.yellow');
let blueBtn = document.querySelector('.blue');
let greenBtn = document.querySelector('.green');
let game = document.getElementById('show-game');
let startButton = document.getElementById('start-game');
let gameFinish = false;
let youWin = document.getElementById('you-win');
let gameOver = document.getElementById('game-over');
// Initial Elements Style
game.style.display ='flex';
gameOver.style.display ='none';
youWin.style.display ='none';
redBtn.classList.remove('red');
yellowBtn.classList.remove('yellow');
blueBtn.classList.remove('blue');
greenBtn.classList.remove('green');
// Start the game
function startGame() {
startButton.style.display = 'none';
game.style.display = 'flex';
gameOver.style.display = 'none';
youWin.style.display = 'none';
gameFinish = false;
playButton.forEach(e => {
e.classList.remove('red');
e.classList.remove('yellow');
e.classList.remove('blue');
e.classList.remove('green');
})
picSamplers();
playMelody();
}
// Change buttons opacity while cliking
function lightButton(z) {
for ( let i = 0; i < playButton.length; i += 1){
if (z === 'c'){
playButton[0].classList.add('red')
setTimeout(() => {
playButton[0].classList.remove('red')
}, 500);
} else if (z === 'e'){
playButton[1].classList.add('yellow')
setTimeout(() => {
playButton[1].classList.remove('yellow')
}, 500);
} else if (z === 'g'){
playButton[2].classList.add('blue')
setTimeout(() => {
playButton[2].classList.remove('blue')
}, 500);
} else if (z === 'a'){
playButton[3].classList.add('green')
setTimeout(() => {
playButton[3].classList.remove('green')
}, 500);
}
}
}
// Answer the call from the button and play the note
function playNote(note) {
if (newMel.length > 2) {
lightButton(note);
melodyTry.push(note);
const noteAudio = new Audio(`${note}.mp3`);
noteAudio.play();
noteAudio.addEventListener('ended', () => {
compareMelodies();
});
}
}
// Create random melodies & control game levels
function picSamplers(){
if (gameFinish === false) {
if (userPoints <= 2) {
for (let i = 0; i < 3; i += 1) {
newMel.push(notes[random4()]);
}
} else if (userPoints > 2 && userPoints <= 4) {
for (let i = 0; i < 5; i += 1) {
newMel.push(notes[random4()]);
}
} else if (userPoints >= 5 && userPoints <= 6) {
for (let i = 0; i < 7; i += 1) {
newMel.push(notes[random4()]);
}
} else if (userPoints >= 7 && userPoints < 9) {
for (let i = 0; i < 3; i += 1) {
newMel.push(notes[random4()]);
}
} else if (userPoints === 9) {
melodyTry = [];
newMel = [];
userPoints = 0;
gameFinish === true;
youWin.style.display = 'flex';
return;
}
}
}
// Play random melodie & Give opactiy to the keys while playing
function playMelody() {
if (gameFinish === false) {
const audioSeq = [];
newMel.forEach ((e, idx) => {
const note = new Audio(`${newMel[idx]}.mp3`);
audioSeq.push(note);
})
// --- add end eventListener on each audio object of the audioSeq Melody; they are trigged by the end of the last melody note and also change the key class (opacity)--- //
for (let i = 0; i < audioSeq.length - 1; i += 1) {
audioSeq[i].addEventListener('ended', () => { audioSeq[i + 1].play();
if (userPoints <= 7) { // => Don't let key opacity changes in last round
switch (audioSeq[i + 1].src) {
case 'https://dvidotti.github.io/genius2.0-game/c.mp3':
playButton[0].classList.add('red')
setTimeout(() => { playButton[0].classList.remove('red')}, 500);
break;
case 'https://dvidotti.github.io/genius2.0-game/e.mp3':
playButton[1].classList.add('yellow')
setTimeout(() => { playButton[1].classList.remove('yellow')}, 500);
break;
case 'https://dvidotti.github.io/genius2.0-game/g.mp3':
playButton[2].classList.add('blue')
setTimeout(() => { playButton[2].classList.remove('blue')}, 500);
break;
case 'https://dvidotti.github.io/genius2.0-game/a.mp3':
playButton[3].classList.add('green')
setTimeout(() => { playButton[3].classList.remove('green')}, 500);
break;
default:
console.log("Wrong Logic")
}
}
});
}
if (audioSeq.length !== 0) {
audioSeq[0].play();
if (userPoints <= 7) {
switch (audioSeq[0].src) {
case 'https://dvidotti.github.io/genius2.0-game/c.mp3':
playButton[0].classList.add('red')
setTimeout(() => { playButton[0].classList.remove('red')}, 500);
break;
case 'https://dvidotti.github.io/genius2.0-game/e.mp3':
playButton[1].classList.add('yellow')
setTimeout(() => { playButton[1].classList.remove('yellow')}, 500);
break;
case 'https://dvidotti.github.io/genius2.0-game/g.mp3':
playButton[2].classList.add('blue')
setTimeout(() => { playButton[2].classList.remove('blue')}, 500);
break;
case 'https://dvidotti.github.io/genius2.0-game/a.mp3':
playButton[3].classList.add('green')
setTimeout(() => { playButton[3].classList.remove('green')}, 500);
break;
default:
console.log("Wrong Logic")
}
}
}
}
}
// Compare Melodies & Condition for game over
function compareMelodies() {
if(gameFinish === false) {
if (newMel.length === melodyTry.length) {
if (JSON.stringify(newMel) !== JSON.stringify(melodyTry)) {
gameFinish = true;
newMel = [];
melodyTry = [];
userPoints = 0;
gameOver.style.display = 'flex';
} else if (JSON.stringify(newMel) === JSON.stringify(melodyTry)){
if (userPoints === 9) {
gameFinish = true;
return;
} else {
userPoints += 1;
newMel = [];
melodyTry = [];
picSamplers();
setTimeout(playMelody,1000);
}
}
}
}
}