-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
192 lines (169 loc) · 5.53 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
const allInputKeys = [
{ input: document.querySelector(".input1") },
{ input: document.querySelector(".input2") },
{ input: document.querySelector(".input3") },
{ input: document.querySelector(".input4") },
{ input: document.querySelector(".input5") },
{ input: document.querySelector(".input6") },
{ input: document.querySelector(".input7") },
{ input: document.querySelector(".input8") },
];
const buttonStart = document.querySelector("button");
const badResult = document.querySelector(".bad_result");
const good_result = document.querySelector(".good_result");
const containerChallenge = document.querySelector(".container_challenge");
const progBar = document.querySelector(".current_bar");
const wrongMoveSound = new Audio("./sound/sound_wrong_move_sound.mp3");
const keyPressSound = new Audio("./sound/key_press_sound.mp3");
wrongMoveSound.volume = 0.7;
keyPressSound.volume = 0.4;
window.addEventListener("keydown", handleEventKey);
buttonStart.addEventListener("click", startStopGame);
const TIME_CHALLENGE = 6; //seconds
const QUANTITY_TIMES = 3; //times
const LETTERS = ["A", "S", "D", "Q", "W", "E"];
const currentSequence = [];
const userInput = [];
let isStart = false;
let currentPositionTyping = 0;
let currentTimer = 0;
let currentProgBar = 100;
let timerId = null;
function timerBar() {
timerId = setInterval(() => {
if (currentProgBar < 0) {
clearInterval(timerId);
badResult.style.display = "flex";
containerChallenge.style.display = "none";
isStart = false;
buttonStart.classList = "button-start";
buttonStart.textContent = "Começar";
currentSequence.length = 0; //empty array
userInput.length = 0; //empty array
resetChallenge();
allInputKeys.forEach((key) =>
key.input.classList.remove("key-input-right")
);
//Empty the key display
allInputKeys.forEach((key, index) => {
key.input.textContent = "";
});
}
if (currentProgBar > 30 && currentProgBar < 60) {
progBar.style.backgroundColor = "#F58002";
}
if (currentProgBar <= 30) {
progBar.style.backgroundColor = "#FF3E24";
}
currentProgBar = currentProgBar - 1;
progBar.style.width = `${currentProgBar}%`;
}, 53);
}
function getRandomLetter(arr) {
const min = 0;
const max = arr.length - 1;
const randomNumber = Math.floor(Math.random() * (max - min + 1) + min);
return arr[randomNumber];
}
function startStopGame() {
isStart = !isStart;
buttonStart.classList = isStart ? "button-stop" : "button-start";
buttonStart.textContent = isStart ? "Parar" : "Começar";
if (isStart) {
badResult.style.display = "none";
good_result.style.display = "none";
containerChallenge.style.display = "grid";
currentSequence.push(...getCodeSequence());
resetChallenge();
timerBar();
//Fill the key display
allInputKeys.forEach((key, index) => {
key.input.textContent = currentSequence[index];
});
} else {
currentSequence.length = 0; //empty array
userInput.length = 0; //empty array
resetChallenge();
allInputKeys.forEach((key) =>
key.input.classList.remove("key-input-right")
);
//Empty the key display
allInputKeys.forEach((key, index) => {
key.input.textContent = "";
});
}
}
function getCodeSequence() {
return (codeSequenceArray = new Array(8)
.fill()
.map(() => getRandomLetter(LETTERS)));
}
function resetChallenge() {
progBar.style.backgroundColor = "#a3ef52";
currentProgBar = 100;
progBar.style.width = "100%";
currentPositionTyping = 0;
clearInterval(timerId);
}
function handleEventKey(event) {
const letterInput = event.key.toUpperCase();
if (letterInput === "ENTER") {
console.log("right");
startStopGame();
return;
}
if (isStart && LETTERS.includes(letterInput) && userInput.length < 8) {
userInput.push(letterInput);
if (userInput.every((e, i) => e === currentSequence[i])) {
keyPressSound.play();
allInputKeys[currentPositionTyping].input.classList.add(
"key-input-right"
);
currentPositionTyping++;
if (userInput.length === 8) {
clearInterval(timerId);
good_result.style.display = "flex";
containerChallenge.style.display = "none";
isStart = false;
buttonStart.classList = "button-start";
buttonStart.textContent = "Começar";
currentSequence.length = 0; //empty array
userInput.length = 0; //empty array
resetChallenge();
allInputKeys.forEach((key) =>
key.input.classList.remove("key-input-right")
);
//Empty the key display
allInputKeys.forEach((key, index) => {
key.input.textContent = "";
});
}
} else {
wrongMoveSound.play();
currentPositionTyping = 0;
allInputKeys.forEach((key) =>
key.input.classList.remove("key-input-right")
);
userInput.length = 0;
currentSequence.length = 0;
currentSequence.push(...getCodeSequence()); //get a new sequence
allInputKeys.forEach((key, index) => {
key.input.textContent = currentSequence[index];
});
}
} else {
if (isStart) {
wrongMoveSound.play();
currentPositionTyping = 0;
allInputKeys.forEach((key) =>
key.input.classList.remove("key-input-right")
);
userInput.length = 0;
currentSequence.length = 0;
currentSequence.push(...getCodeSequence()); //get a new sequence
allInputKeys.forEach((key, index) => {
key.input.textContent = currentSequence[index];
});
}
}
}