-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
292 lines (266 loc) · 7.48 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// global constants
const initialClueHoldTime = 500;
const cluePauseTime = 250; //how long to pause in between clues
const nextClueWaitTime = 1000; //how long to wait before starting playback of the clue sequence
//Global Variables
var clueHoldTime; //how long to hold each clue's light/sound
var pattern = []; // empty pattern, fill when game starts
var patternLength = 5;
var buttonCount = 4;
var strikes = 0;
var guessCounter = 0;
var timerIsOn = true;
var timePerGuess = 4;
var progress = 0;
var gamePlaying = false;
var tonePlaying = false;
var volume = 0.3; //must be between 0.0 and 1.0
function createPattern() {
// ensure pattern is empty
pattern = [];
// populate pattern with random values between 1 and the buttonCount
for (var i = 0; i < patternLength; i++) {
pattern.push(Math.floor(Math.random() * buttonCount) + 1);
}
}
function setButtonCount(newCount) {
// if newCount is less than buttonCount, hide all buttons after newCount
if (newCount < buttonCount) {
for (var i = newCount + 1; i <= buttonCount; i++) {
document.getElementById("button" + i).classList.add("hidden");
}
buttonCount = newCount;
// otherwise newCount is more than current buttonCount, show the hidden button(s)
} else {
for (var i = buttonCount; i <= newCount; i++) {
document.getElementById("button" + i).classList.remove("hidden");
}
buttonCount = newCount;
}
}
function setTimePerGuess(time) {
timePerGuess = time;
document.getElementById("time").innerHTML = time;
}
function setDifficulty(difficulty) {
document.getElementById("currentMove").innerHTML = 1;
if (difficulty == 1) {
// easy
setButtonCount(4);
setTimePerGuess(4);
patternLength = 5;
} else if (difficulty == 2) {
// medium
setButtonCount(5);
setTimePerGuess(3);
patternLength = 8;
} else {
// hard
setButtonCount(6);
setTimePerGuess(2);
patternLength = 10;
}
}
function displayVariables() {
document.getElementById("time").innerHTML = 3;
document.getElementById("currentMove").innerHTML = 1;
}
function startGame() {
//initialize game variables
strikes = 0;
clueHoldTime = initialClueHoldTime;
createPattern();
progress = 0;
gamePlaying = true;
// swap the Start and Stop buttons
document.getElementById("startBtn").classList.add("hidden");
document.getElementById("stopBtn").classList.remove("hidden");
playClueSequence();
}
function stopGame() {
gamePlaying = false;
document.getElementById("startBtn").classList.remove("hidden");
document.getElementById("stopBtn").classList.add("hidden");
}
function lightButton(btn) {
document.getElementById("button" + btn).classList.add("lit");
}
function clearButton(btn) {
document.getElementById("button" + btn).classList.remove("lit");
}
function playSingleClue(btn) {
if (gamePlaying) {
lightButton(btn);
playTone(btn, clueHoldTime);
setTimeout(clearButton, clueHoldTime, btn);
}
}
function playClueSequence() {
// speed up, unless clueHoldTime is less than 150 ms
if (clueHoldTime > 150) {
clueHoldTime -= 50;
}
guessCounter = 0;
let delay = nextClueWaitTime; //set delay to initial wait time
for (let i = 0; i <= progress; i++) {
// for each clue that is revealed so far
console.log("play single clue: " + pattern[i] + " in " + delay + "ms");
setTimeout(playSingleClue, delay, pattern[i]); // set a timeout to play that clue
delay += clueHoldTime;
delay += cluePauseTime;
}
stopCount();
setTimeout(startCount, delay - cluePauseTime);
}
function lostGame(reason) {
stopGame();
if (reason == "strikes") {
alert("You ran out of strikes! Game over.");
} else {
alert("Times up! Game over.");
}
document.getElementById("currentMove").innerHTML = 1;
}
function wonGame() {
stopGame();
alert("Game Over. You won!");
}
function guess(btn) {
stopCount();
console.log("user guessed: " + btn);
if (!gamePlaying) {
return;
}
// add game logic here
if (btn == pattern[guessCounter]) {
if (guessCounter == progress) {
if (progress == pattern.length - 1) {
wonGame();
} else {
progress++;
document.getElementById("currentMove").innerHTML = progress + 1;
playClueSequence();
}
} else {
guessCounter++;
setTimeout(startCount, 250);
}
} else {
if (strikes == 2) {
lostGame("strikes");
} else {
strikes++;
alert("Strike " + strikes + "! You have " + (3 - strikes) + " left.");
clueHoldTime += 50;
playClueSequence();
}
}
}
var timer;
var timeLeft = timePerGuess;
function timedCount() {
if (!gamePlaying) {
return;
}
if (timeLeft == 0) {
document.getElementById("time").innerHTML = 0;
clearTimeout(timer);
timerIsOn = false;
lostGame("time");
}
document.getElementById("time").innerHTML = timeLeft;
timeLeft--;
timer = setTimeout(timedCount, 1000);
}
function startCount() {
timeLeft = timePerGuess;
if (!timerIsOn) {
timerIsOn = false;
timedCount();
}
}
function stopCount() {
timeLeft = timePerGuess;
document.getElementById("time").innerHTML = timeLeft;
clearTimeout(timer);
timerIsOn = false;
}
// Sound Synthesis Functions
const freqMap = {
1: 50,
2: 100,
3: 150,
4: 200,
5: 250,
6: 300
};
function playTone(btn, len) {
console.log(o1.frequency.value);
o1.frequency.value = freqMap[btn];
o2.frequency.value = freqMap[btn] + 100;
o3.frequency.value = freqMap[btn] + 200;
g1.gain.setTargetAtTime(volume, context1.currentTime + 0.05, 0.025);
g2.gain.setTargetAtTime(volume, context2.currentTime + 0.05, 0.025);
g3.gain.setTargetAtTime(volume, context3.currentTime + 0.05, 0.025);
tonePlaying = true;
setTimeout(function() {
stopTone();
}, len);
}
function stopTone() {
g1.gain.setTargetAtTime(0, context1.currentTime + 0.05, 0.025);
g2.gain.setTargetAtTime(0, context2.currentTime + 0.05, 0.025);
g3.gain.setTargetAtTime(0, context3.currentTime + 0.05, 0.025);
tonePlaying = false;
}
function startChord(btn) {
if (!tonePlaying) {
o1.frequency.value = freqMap[btn];
o2.frequency.value = freqMap[btn] + 100;
o3.frequency.value = freqMap[btn] + 200;
g1.gain.setTargetAtTime(volume, context1.currentTime + 0.05, 0.025);
g2.gain.setTargetAtTime(volume, context2.currentTime + 0.2, 0.025);
g3.gain.setTargetAtTime(volume, context3.currentTime + 0.35, 0.025);
tonePlaying = true;
}
}
function stopChord() {
g1.gain.setTargetAtTime(0, context1.currentTime + 0.05, 0.025);
g2.gain.setTargetAtTime(0, context2.currentTime + 0.2, 0.025);
g3.gain.setTargetAtTime(0, context3.currentTime + 0.35, 0.025);
tonePlaying = false;
}
//Page Initialization
// Init Sound Synthesizer
var context1 = new AudioContext();
var o1 = context1.createOscillator();
o1.type = "sine";
o1.frequency.value = 100;
var g1 = context1.createGain();
g1.connect(context1.destination);
g1.gain.setValueAtTime(0, context1.currentTime);
o1.connect(g1);
o1.start(0);
var context2 = new AudioContext();
var o2 = context2.createOscillator();
o2.type = "sine";
o2.frequency.value = 100;
var g2 = context2.createGain();
g2.connect(context2.destination);
g2.gain.setValueAtTime(0, context2.currentTime);
o2.connect(g2);
o2.start(0);
var context3 = new AudioContext();
var o3 = context3.createOscillator();
o3.type = "sine";
o3.frequency.value = 100;
var g3 = context3.createGain();
g3.connect(context3.destination);
g3.gain.setValueAtTime(0, context3.currentTime);
o3.connect(g3);
o3.start(0);
function enableSound() {
context1.resume();
context2.resume();
context3.resume();
}