-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
124 lines (115 loc) · 3.3 KB
/
Copy pathscript.js
File metadata and controls
124 lines (115 loc) · 3.3 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
//Add your global variables here
let play = false;
let pattern = [];
let level = 7;
let gameStarted = false
let highlightProgress = 0;
// Add your functions here
//Random integer function
const randomNums = (pMin, pMax) => {
let min = Math.ceil(pMin);
let max = Math.floor(pMax);
return Math.floor(Math.random() * (max - min) + min);
}
// Function to add 8 random numbers to the pattern array to get a random pattern for the sound hint
const randomPattern = () => {
for (let i = 0; i < 7; i++) {
pattern.push(randomNums(0, 3));
}
}
//Function to start the game and begin usability of the start and stop button
const startGame = () => {
gameStarted = !gameStarted;
if (gameStarted == true) {
document.getElementById("start").innerHTML = "Stop";
pattern = [];
randomPattern();
playHint();
}
else {
document.getElementById("start").innerHTML = "Start";
}
}
// Function to toggle the play and pause button when the game has started
const toggleGame = () => {
play = !play;
if (play == true) {
document.getElementById("pause").innerHTML = "Pause";
}
else {
document.getElementById("pause").innerHTML = "Play";
}
}
//Function to show highlights to the buttons, that follows the pattern array nums (0,1,2,3), also added a pause function
const playHint = async () => {
for (let i = highlightProgress; i < level; i++) {
if (play == true) {
if (i >= level) {
highlightProgress = 0;
}
else {
highlightProgress = i;
}
await highlightButton(pattern[i]);
}
else {
return;
}
}
}
// Function that adds active to the classlist and so if it has GreenS it adds active to it and highlights the button sets to whatever class
const highlightButton = async (button) => {
const docButton = document.getElementById('button-' + button);
docButton.classList.add('active');
await new Promise((resolve) => {
setTimeout(() => {
docButton.classList.remove('active');
resolve();
}, 1000)
})
await new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 500)
})
}
// Sound Synthesis Functions for Steps 6-8
// You do not need to edit the below code
const freqMap = {
1: 261.6,
2: 329.6,
3: 392,
4: 466.2
}
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)
}
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
}
}
function stopTone() {
g.gain.setTargetAtTime(0, context.currentTime + 0.05, 0.025)
tonePlaying = false
}
// Page Initialization
// Init Sound Synthesizer
let AudioContext = window.AudioContext || window.webkitAudioContext
let context = new AudioContext()
let o = context.createOscillator()
let g = context.createGain()
g.connect(context.destination)
g.gain.setValueAtTime(0, context.currentTime)
o.connect(g)
o.start(0)