-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
199 lines (171 loc) · 4.82 KB
/
script.js
File metadata and controls
199 lines (171 loc) · 4.82 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
//constants
const cluePauseTime = 333; //time between each clue
const nextClueWaitTime = 1000; //time to wait before starting playback of the clue sequence
const countDown = document.getElementById("timer");
//variables
var clueHoldTime = 1000; //1 sec for the light and sound
var pattern = [];
var progress = 0;
var gamePlaying = false;
var tonePlaying = false;
var volume = 0.5;
var guessCounter = 0;
var mistakes = 0; //tracks the num of mistakes done by the user
var timer; //timer
var timeLeft = 60; //1 min of game
//generate pattern 1-8 --> uses a for loop to randomly generate numbers and adds them to the empty pattern list
function cluePattern(min, max){
for (let i =0; i <= 15; i++){
pattern.push(Math.floor(Math.random()* (max-min) + min));
}
}
//timer function for updating time and showing the time
function updateTime(){
timeLeft--;
if(timeLeft >= 0)
countDown.innerHTML = `${"Timer"}:${timeLeft}`; //Source: Florin Pop on Youtube
else {
stopGame();
}
}
//start game function
function startGame(){
progress = 0;
gamePlaying = true;
document.getElementById("startBtn").classList.add("hidden");
document.getElementById("endBtn").classList.remove("hidden");
cluePattern(1,8); //calls function that will generate the random pattern from nums 1-8
playClueSequence();
mistakes = 0;
timer = setInterval(updateTime, 1000); //sets timer
updateTime();
}
//stop game function
function stopGame(){
gamePlaying = false;
document.getElementById("startBtn").classList.remove("hidden");
document.getElementById("endBtn").classList.add("hidden");
clearInterval(timer);
document.getElementById('timer').innerHTML='Done'; //changes timer to Done
}
//sound portion
//pitch for each sound(4 buttons) - higher nums -> higher pitches, lower nums --> lower pitches
const freqMap= {
1: 261.6,
2: 329.6,
3: 392,
4: 466.2,
5: 336.3,
6: 500,
7: 273.7,
8: 310
}
//playing the tone
function playTone(btn, len){
o.frequency.value = freqMap[btn]
g.gain.setTargetAtTime(volume, context.currentTime + 0.05,0.025)
context.resume()
tonePlaying = true
setTimeout(function(){
stopTone()
},len)
}
//start to play
function startTone(btn){
if(!tonePlaying){
context.resume()
o.frequency.value = freqMap[btn]
g.gain.setTargetAtTime(volume, context.currentTime + 0.05, 0.025)
context.resume()
tonePlaying = true
}
}
//ends tone
function stopTone(){
g.gain.setTargetAtTime(0,context.currentTime + 0.5, 0.025)
tonePlaying = false
}
//initialization
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)
//lighting the button
function lightButton(btn){
document.getElementById("bt" + btn).classList.add("lit");
//shows image when button is lit
showImage(btn);
//document.getElementById("img1").classList.remove("hidden")
}
//clearing the button
function clearButton(btn){
document.getElementById("bt" + btn).classList.remove("lit");
//hides image when button is not lit
hideImage(btn);
}
//playing single clue
function playSingleClue(btn){
if (gamePlaying){
lightButton(btn);
playTone(btn, clueHoldTime);
setTimeout(clearButton, clueHoldTime,btn);
}
}
//function for the clue sequence
function playClueSequence(){
guessCounter = 0;
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;
clueHoldTime -= 20; //decreases hold time after each clue
}
}
//function for losing game
function loseGame(){
stopGame();
alert("Game Over. You lost. Try Again.");
}
function winGame(){
stopGame();
alert("You won!");
}
//guess
function guess(btn){
console.log("user guessed: " + btn);
if (!gamePlaying){
return;
}
if (pattern[guessCounter] == btn){ //correct guess
if (progress == guessCounter){ //turn is still going
if (progress == pattern.length-1){ //last turn (length-1)
winGame(); //wins the game
}else{ //not last turn therefore a clue sequence is played
progress++;
playClueSequence();
}
}else{ //turn is not over
guessCounter++;
}
}else{ //incorrect guess ==> lose game
mistakes += 1;
if (mistakes == 3){
loseGame();
}
}
}
//image functions!
//shows the function when button is clicked
function showImage(btn){
document.getElementById("image" + [btn]).style.visibility = "visible";
}
//hides function when button is not clicked
function hideImage(btn){
document.getElementById("image" + [btn]).style.visibility = "hidden";
}