-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
242 lines (206 loc) · 5.12 KB
/
script.js
File metadata and controls
242 lines (206 loc) · 5.12 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
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
const cluePauseTime = 333;
const nextClueWaitTime = 1000;
//Global Variables
//Global constants
var clueHoldTime = 1000;
var pattern = [];
var progress = 0;
var gamePlaying = false;
var tonePlaying = false;
var volume = 0.5;
var guessCounter = 0;
var mistakeCounter = 3;
var drum;
var time;
var seconds = 0;
function randomPattern() {
let num = 0;
for (let i=0; i<8; i++) {
num = Math.floor(Math.random() * 5)+1;
pattern[i]=num;
}
}
function startGame(){
//initialize game variables
progress = 0;
gamePlaying = true;
randomPattern();
mistakeCounter = 3;
time = 25;
seconds = 0;
// swap the Start and Stop buttons
document.getElementById("startBtn").classList.add("hidden");
document.getElementById("stopBtn").classList.remove("hidden");
document.getElementById("strikes").innerHTML = "Chances left: " + mistakeCounter;
countdownTimer()
document.getElementById("timer").innerHTML = time + ' Seconds Left';
playClueSequence();
}
function stopGame(){
gamePlaying = false;
// swap the Start and Stop buttons
document.getElementById("startBtn").classList.remove("hidden");
document.getElementById("stopBtn").classList.add("hidden");
clearInterval()
}
// Sound Synthesis Functions
const freqMap = {
1: 261.6,
2: [112,200,300],
3: 329.6,
4: 392,
5: 466.2
}
var drum = freqMap[2];
var counter = 0;
var index = 0;
function startDrum(index) {
if (index == 3) {
index = 0
}
console.log(index)
context.resume()
o.frequency.value = drum[index]
g.gain.setTargetAtTime(volume,context.currentTime + 0.05,0.025)
tonePlaying=true
setTimeout(function(){stopTone()},500)
index++
}
function playDrum() {
var counter = 0;
var drumInterval = setInterval(function(){
startDrum(counter)
counter++;
if(counter === 3) {
clearInterval(drumInterval);
}
}, 200);
}
function playTone(btn,len){
if (btn == 2) {
playDrum()
}
else {
o.frequency.value = freqMap[btn]
g.gain.setTargetAtTime(volume,context.currentTime + 0.05,0.025)
context.resume()
tonePlaying = true
setTimeout(function(){
stopTone()
},len)
}
}
function startTone(btn){
if(!tonePlaying){
if (btn == 2) {
playDrum()
}
else {
o.frequency.value = freqMap[btn]
g.gain.setTargetAtTime(volume,context.currentTime + 0.05,0.025)
context.resume()
tonePlaying = true
}
}
}
function stopTone(){
g.gain.setTargetAtTime(0,context.currentTime + 0.05,0.025)
tonePlaying = false
}
// Page Initialization
// Init Sound Synthesizer
var AudioContext = window.AudioContext || window.webkitAudioContext
var context = new AudioContext()
var o = context.createOscillator()
var g = context.createGain()
g.connect(context.destination)
g.gain.setValueAtTime(0,context.currentTime)
o.connect(g)
o.start(0)
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);
clueHoldTime = clueHoldTime/1.03;
playTone(btn,clueHoldTime);
setTimeout(clearButton,clueHoldTime,btn);
}
}
function playClueSequence(){
guessCounter = 0;
context.resume();
let delay = nextClueWaitTime;
for(let i=0;i<=progress;i++){
console.log("play single clue: " + pattern[i] + " in " + delay + "ms")
setTimeout(playSingleClue,delay,pattern[i]);
delay += clueHoldTime;
delay += cluePauseTime;
}
}
function loseGame(){
stopGame();
alert("Game Over. You lost.");
}
function winGame(){
stopGame();
alert("Game Over. You won!");
}
document.getElementById("button1").onclick = function() {guess('1')};
document.getElementById("button2").onclick = function() {guess('2')};
document.getElementById("button3").onclick = function() {guess('3')};
document.getElementById("button4").onclick = function() {guess('4')};
document.getElementById("button5").onclick = function() {guess('5')};
function guess(btn){
console.log("user guessed: " + btn);
if (!gamePlaying) {
return;
}
if (pattern[guessCounter] == btn) {
if (progress == guessCounter) {
if (progress == pattern.length-1){
winGame();
}
else {
progress++;
playClueSequence();
seconds = 0 }
}
else {
guessCounter++;
seconds = 0
}
}
else {
if (mistakeCounter>1){
mistakeCounter--;
document.getElementById("strikes").innerHTML = "Chances left: "+mistakeCounter;
playClueSequence();
seconds = 0
}
else {
loseGame();
}
}
}
function countdownTimer () {
// Update the count down every 1 second
var x = setInterval(function() {
time = 25 - progress*2 - seconds;
seconds++
// Display the result in the element with id="demo"
document.getElementById("timer").innerHTML = time + ' Seconds Left';
if (gamePlaying == false) {
clearInterval(x)
}
if (time < 0) {
clearInterval(x);
document.getElementById("timer").innerHTML = "You lost, you ran out of time!";
loseGame()
}
}, 1000);
}