-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
207 lines (185 loc) · 6.38 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import characters from "./characters.js";
import correctMessages from "./correctMessages.js";
import incorrectMessages from "./incorrectMessages.js";
let timer;
let gameOver = 0;
let score = 0;
let usedHintsCount = 0;
let currentCharacter;
let percent = 100;
const menuDiv = document.querySelector(".menu");
const settingsDiv = document.querySelector(".settings");
const settingsButton = document.querySelector(".settingsButton");
const backToMenuButton = document.querySelector(".backtomenuButton");
const audio = document.querySelector(".audio");
const volumeSlider = document.querySelector(".volumeSlider");
const volumeDisplay = document.querySelector(".volumeDisplay");
const gameDiv = document.querySelector(".game");
const characterImage = document.querySelector(".characterImage");
const form = document.querySelector(".form");
const answerInput = document.querySelector(".answerInput");
const hintButton = document.querySelector(".hintButton");
const messageDiv = document.querySelector(".message");
const hintDiv = document.querySelector(".hint");
const timerPercent = document.querySelector(".timerPercent");
const timerBar = document.querySelector(".timerBar");
const gameOverDiv = document.querySelector(".gameOver");
const gameOverMessage = document.querySelector(".gameOverMessage");
const gameOverSubmessage = document.querySelector(".gameOverSubmessage");
const startButton = document.querySelector(".startButton");
const restartButton = document.querySelector(".restartButton");
const startGame = () => {
menuDiv.style.display = "none";
gameDiv.style.display = "flex";
currentCharacter = characters[Math.floor(Math.random() * characters.length)];
characterImage.style.backgroundImage = `url(${currentCharacter.image})`;
const index = characters.findIndex(
(character) => character.name === currentCharacter.name
);
characters.splice(index, 1);
if (characters.length === 0) {
endGame();
return;
}
percent = 100;
timer = setInterval(updateTimerBar, 1000);
};
const endGame = () => {
clearInterval(timer);
gameDiv.style.display = "none";
gameOverDiv.style.display = "flex";
gameOverMessage.innerHTML = "Congratulations!";
gameOverSubmessage.innerHTML = `Your score is: ${score}`;
};
const updateTimerBar = () => {
percent -= 5;
timerPercent.innerHTML = percent / 5 + " seconds left";
timerBar.style.width = percent + "%";
if (percent <= 0 && gameOver === 0) {
endGame();
}
};
const submitAnswer = (event) => {
event.preventDefault();
const answer = answerInput.value;
const correctAnswer = currentCharacter.name.toLowerCase();
if (answer.toLowerCase() === correctAnswer) {
handleCorrectGuess();
} else {
handleIncorrectGuess();
}
answerInput.value = "";
hintDiv.style.display = "none";
usedHintsCount = 0;
};
const handleCorrectGuess = () => {
score += 1;
const audioCorrect = new Audio("assets/music/correct.mp3");
audioCorrect.play();
showRandomMessage(correctMessages);
handleNextCharacter();
};
const handleIncorrectGuess = () => {
const audioIncorrect = new Audio("assets/music/incorrect.mp3");
audioIncorrect.play();
showRandomMessage(incorrectMessages);
};
const showRandomMessage = (messages) => {
const message = messages[Math.floor(Math.random() * messages.length)];
messageDiv.innerHTML = message;
setTimeout(() => {
messageDiv.style.display = "none";
showPoints();
}, 1500);
};
const showPoints = () => {
messageDiv.innerHTML = `Your score is: ${score}`;
messageDiv.style.display = "block";
};
const useHint = () => {
if (score >= 1) {
showPoints();
messageDiv.innerHTML = `Hint used!`;
score -= 1;
usedHintsCount++;
hintDiv.innerHTML = currentCharacter.name
.split("")
.map((char, index) =>
index < usedHintsCount ? char : char === " " ? " " : "_"
)
.join("");
hintDiv.style.display = "block";
} else {
showPoints();
messageDiv.innerHTML = `You don't have enough points!`;
}
};
const handleNextCharacter = () => {
currentCharacter = characters[Math.floor(Math.random() * characters.length)];
characterImage.style.backgroundImage = `url(${currentCharacter.image})`;
const index = characters.findIndex(
(character) => character.name === currentCharacter.name
);
characters.splice(index, 1);
if (characters.length === 0) {
endGame();
}
percent = 100;
timerPercent.innerHTML = "20 seconds left";
timerBar.style.width = "100%";
};
const restartGame = () => {
gameOverDiv.style.display = "none";
menuDiv.style.display = "flex";
score = 0;
hintDiv.innerHTML = "";
messageDiv.innerHTML = "";
usedHintsCount = 0;
startButton.classList.remove("menuAnimation");
percent = 100;
timerPercent.innerHTML = "20 seconds left";
timerBar.style.width = "100%";
messageDiv.innerHTML = `Your score is: ${score}`;
};
const settings = () => {
menuDiv.style.display = "none";
settingsDiv.style.display = "flex";
};
const menuBack = () => {
settingsDiv.style.display = "none";
menuDiv.style.display = "flex";
};
const handleVolumeChange = () => {
audio.volume = volumeSlider.value / 100;
volumeDisplay.innerHTML = `${volumeSlider.value}%`;
};
volumeSlider.addEventListener("input", (event) => {
handleVolumeChange();
audio.volume = event.target.value;
});
function init() {
audio.volume = volumeSlider.value / 100;
volumeDisplay.innerHTML = `${volumeSlider.value}%`;
startButton.addEventListener("click", (event) => {
event.target.classList.add("menuAnimation");
setTimeout(startGame, 500);
restartButton.classList.remove("menuAnimation");
});
settingsButton.addEventListener("click", (event) => {
event.target.classList.add("menuAnimation");
setTimeout(settings, 500);
backToMenuButton.classList.remove("menuAnimation");
});
backToMenuButton.addEventListener("click", (event) => {
event.target.classList.add("menuAnimation");
setTimeout(menuBack, 400);
settingsButton.classList.remove("menuAnimation");
});
restartButton.addEventListener("click", (event) => {
event.target.classList.add("menuAnimation");
setTimeout(restartGame, 400);
});
form.addEventListener("submit", submitAnswer);
hintButton.addEventListener("click", useHint);
}
window.addEventListener("load", init);