-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (127 loc) · 4.07 KB
/
index.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
/**
* Get game controller buttons
*/
const controllerWrapper = document.getElementById('controller-wrapper');
const aButton = document.getElementById('a-button');
const sButton = document.getElementById('s-button');
const resetButton = document.querySelector('.reset');
const startButton = document.querySelector('.start');
const upButton = document.getElementById('up-button');
const rightButton = document.getElementById('right-button');
const downButton = document.getElementById('down-button');
const leftButton = document.getElementById('left-button');
const streakCounter = document.getElementById('streak-counter');
const keys = document.querySelectorAll('.key');
keys.forEach(key => key.addEventListener('transitionEnd', removeTransition));
window.addEventListener('keydown', playSound);
const playerKeyList = [];
let computerKeyList = [];
// Get the Keyboard ascii code
const keyList = [37, 38, 39, 40, 65, 83];
let iterator = 0;
window.addEventListener('keydown', event => {
playerKeyList.push(event.keyCode);
});
// for (iterator; iterator < 10; iterator++) {
// const randiterator = Math.floor(Math.random() * 6);
// console.log(keyList[randiterator]);
// }
async function myLoop() {
if (iterator >= 6) {
// clear iterator and array
iterator = 0;
computerKeyList = [];
}
// get an index from 0 to 5
const randIndex = Math.floor(Math.random() * 6);
computerKeyList.push(keyList[randIndex]);
const audio = document.querySelector(
`audio[data-key="${computerKeyList[iterator]}"]`
);
if (randIndex < 4) {
const changeArrowImg = document.querySelector(
`div[data-key="${computerKeyList[iterator]}"] img`
);
changeArrowImg.src = `images/${changeArrowImg.name}ArrowAfter.png`;
audio.currentTime = 0;
await audio.play();
setTimeout(() => {
changeArrowImg.src = `images/${changeArrowImg.name}ArrowBefore.png`;
}, 100);
} else {
const changeClass = document.querySelector(
`div[data-key="${computerKeyList[iterator]}"]`
);
const startSound = new Audio(`audio/${changeClass.id}.wav`);
changeClass.classList.add(`${changeClass.id}-active`);
startSound.currentTime = 0;
startSound.play();
setTimeout(() => {
changeClass.classList.remove(`${changeClass.id}-active`);
}, 300);
}
// create a loop function
setTimeout(() => {
iterator++; // increment the counter
if (iterator < 6) {
myLoop();
}
}, 1000);
}
// myLoop();
function playSound(event) {
const audio = document.querySelector(`audio[data-key="${event.keyCode}"]`);
const key = document.querySelector(`.key[data-key="${event.keyCode}"]`);
if (!audio) return;
audio.currentTime = 0;
audio.play();
key.classList.add(`${key.id}-active`);
setInterval(function() {
key.classList.remove(`${key.id}-active`);
}, 800);
}
window.addEventListener('keydown', event => {
const changeArrowImg = document.querySelector(
`div[data-key="${event.keyCode}"] img`
);
if (!changeArrowImg) return;
changeArrowImg.src = `images/${changeArrowImg.name}ArrowAfter.png`;
});
window.addEventListener('keyup', event => {
const changeArrowImg = document.querySelector(
`div[data-key="${event.keyCode}"] img`
);
if (!changeArrowImg) return;
changeArrowImg.src = `images/${changeArrowImg.name}ArrowBefore.png`;
});
window.addEventListener(
'keydown',
event => {
// space and arrow keys
if ([32, 37, 38, 39, 40].indexOf(event.keyCode) > -1) {
event.preventDefault();
}
},
false
);
// ! START: These two functions do the exact same thing
resetButton.addEventListener('click', () => {
const resetSound = new Audio('audio/reset.wav');
resetSound.currentTime = 0;
resetSound.play();
});
startButton.addEventListener('click', () => {
const startSound = new Audio('audio/start.wav');
startSound.currentTime = 0;
startSound.play();
setTimeout(() => {
myLoop();
}, 3500);
});
// ! END
// ? We will have to transition what we have at top for changing
// ? class to the function
function removeTransition(event) {
if (event.propertyName !== 'transform') return;
this.className.remove('');
}