-
Notifications
You must be signed in to change notification settings - Fork 14
/
Hangman.js
342 lines (310 loc) · 12 KB
/
Hangman.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//Style constants
var score = 0;
var TurnsleftStyle =
"cursor: url('./src/files/pokeball-hover.png'), pointer;font-size: 55px; border-radius: 7%; font-family: Brush Script MT, cursive; transition-duration: 0.5s;";
var style =
"box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19); font-size: 25px;transition-duration: 0.8s;cursor: url('./src/files/pokeball-hover.png'), pointer;border-radius: 8px;";
const SpriteLink = "https://img.pokemondb.net/artwork/large/";
const InfoHyperLink = "https://bulbapedia.bulbagarden.net/wiki/";
var music = new Audio(
"http://play.pokemonshowdown.com/audio/hgss-kanto-trainer.mp3"
);
var lost = new Audio("/assets/loose-sound.wav");
var win = new Audio("http://play.pokemonshowdown.com/audio/cries/pikachu.mp3");
var incorrect = new Audio(
"http://play.pokemonshowdown.com/audio/cries/litwick.mp3"
);
var correct = new Audio(
"http://play.pokemonshowdown.com/audio/cries/chimecho.mp3"
);
var ArrayOfWords = data; //array of data
//Array containing the words.
var Word, WordLength, WordLetters, TurnsLeft, Letters, id;
function play() {
document.getElementById("welcome").style.cssText = `Display:none;`;
document.getElementById("play").style.cssText = `Display:none;`;
document.getElementById("pica-pic").style.cssText = `Display:none;`;
document.querySelector(".catch-phrase").style = `Display:none`;
document.getElementById("logo").style.cssText = `Display:none`;
document.querySelector("main").style = `
Display:flex;
flex-direction:column;
`;
if (window.innerWidth < 1000) {
document.querySelector("main").style.cssText = `
background : #7b7b7b url("https://raw.githubusercontent.com/Aatmaj-Zephyr/Hangman/main/WallpaperDog-743770.jpg");
background-blend-mode: multiply;
`;
}
document.getElementById("welcome_text").style.cssText = `Display:none`;
start();
}
function start() {
music.play();
music.loop = "loop";
music.volume = 0.1;
document.getElementById("body").style = null;
document.getElementById("body").className = "gameBodyBackground";
// document.getElementById("body").style =
// "text-align:center;opacity:0.95;transition-duration:1.0s;background-blend-mode: screen;background-image: url('https://raw.githubusercontent.com/Aatmaj-Zephyr/Hangman/main/WallpaperDog-743770.jpg');background-color: rgba(255,255,255,0.8);";
//PascalCase followed.
id = Math.floor(Math.random() * ArrayOfWords.length);
Word = ArrayOfWords[id];
//One random word from the Array.
WordLength = Word.length;
//Length of the word.
WordLetters = [];
//Array to store the letters of the Word.
WordLetters.push(Word.charAt(0)); //Push the First letter.
//In the current version, only the first letter is visible at a hint.
//Modify it further to include random letters, but be careful of the capital letter.
for (let i = WordLength - 1; i > 0; i--) {
WordLetters.push("_");
//Push blank underscores in the rest of the array according to the length of the word.
}
document.getElementById("Word").style = `
cursor: url('./pokeball.png'),pointer;
color:blue;
background-color:Pink;
border-radius: 12px;
transition-duration: 0.8s;
font-size:70px;padding:10px;`;
if (window.innerWidth < 1000) {
document.getElementById("Word").style = `
cursor: url('./pokeball.png'),pointer;
color:blue;
background-color:Pink;
border-radius: 12px;
transition-duration: 0.8s;
font-size:2.7rem;`;
}
document.getElementById("Word").innerHTML = WordLetters.join(" ");
//Display the word in the HTML file
TurnsLeft = Math.floor(WordLength * 0.8);
//Number of turns left. Modify the formula in the later versions.
document.getElementById("TurnsLeft").innerHTML = TurnsLeft;
document.getElementById("TurnsLeft").style.display = "";
SetTurnsLeftStyle();
//Display the number of turns left
Letters = []; //Array to store the Letters
//Setting the Letters
for (var k of Word) {
if (!Letters.includes(k) && k !== Word.charAt(0)) {
Letters.push(k.toString());
//Push the letters of word into the "Letters" array without repetition
}
}
while (Letters.length < 16) {
r = Math.floor(Math.random() * (122 - 97 + 1)) + 97; //Character set from unicode.
v = String.fromCharCode(r);
if (!Letters.includes(v)) {
Letters.push(v);
//Push random characters into the "Letters" array without repetition.
}
}
Letters.sort(() => 0.5 - Math.random()); //Random shuffling of Letters.
Letters.push(Letters[0]); //Ignore the card at 0th position.
for (var f = 1; f <= 16; f++) {
document.getElementById("button" + f).innerHTML = Letters[f];
document.getElementById("button" + f).style = style;
document.getElementById("button" + f).disabled = "";
document.getElementById("button" + f).display = "";
//Dispay the letters into the individual buttons.
}
document.getElementById("image").style = "display:none;width:5%";
}
function ButtonClicked(a) {
win.pause();
win.currentTime = 0;
lost.pause();
lost.currentTime = 0;
correct.pause();
correct.currentTime = 0;
incorrect.pause();
incorrect.currentTime = 0;
//The trailinng muist Stop
//Function to be executed once button is clicked.
//a is the parameter which tells which button is clicked.
document.getElementById("button" + a).disabled = "disabled";
//Disable clicking the same button after it has been clicked.
checkscore(a);
//Check if it matches or not.
}
function checkscore(a) {
if (Word.includes(Letters[a])) {
//If the letter matches......
for (let i = Word.length; i >= 0; i--) {
if (Word.charAt(i) == Letters[a]) {
WordLetters[i] = Letters[a];
//Put the letter in the WordLetters array at all positions.
//This means even repeated letters are put in all the positions.
}
}
document.getElementById("button" + a).style =
"transition-duration: 0.8s;background-color:lime;font-size: 25px;color:white;cursor: not-allowed;opacity:0.95";
correct.play();
correct.volume = 0.3;
//Set the background to green for sucessfull match.
document.getElementById("Word").innerHTML = WordLetters.join(" ");
} else {
document.getElementById("button" + a).style =
"transition-duration: 0.8s;background-color:Red;font-size: 25px;animation-name: effect; animation-duration: 0.1s; animation-iteration-count: 7;color:White;cursor: not-allowed;opacity:0.8;";
incorrect.play();
incorrect.volume = 0.3;
//Set the background to red for unsucessfull match.
TurnsLeft = TurnsLeft - 1;
//reduce the number of tunrns left.
}
document.getElementById("TurnsLeft").innerHTML = TurnsLeft;
SetTurnsLeftStyle(); //this must be before game over or game won.
if (WordLetters.join("") == Word) {
GameWon(); //Game won if all letters correctly guessed.
}
if (TurnsLeft == 0) {
//Game lost if turns over.
GameOver(); //game over.
}
}
function SetTurnsLeftStyle() {
//Set the colour of the turns left according to the theme
if (TurnsLeft <= 4 && TurnsLeft > 2) {
document.getElementById("TurnsLeft").style =
TurnsleftStyle +
"color:green; border: 4px solid #008CBA; background-color:Yellow";
} else if (TurnsLeft <= 2) {
document.getElementById("TurnsLeft").style =
TurnsleftStyle +
"color:white; border: 4px solid #4CAF50; animation: effect 0.2s infinite; background-color:Red";
} else {
document.getElementById("TurnsLeft").style =
TurnsleftStyle +
"color:voilet;background-color:lime; border: 3px solid #f44336;";
}
if (TurnsLeft == 0) {
document.getElementById("TurnsLeft").style.display = "none";
}
}
function GameOver() {
resetValuesEndGame();
lost.play();
lost.volume = 0.6;
document.getElementById("Word").innerHTML = Word;
// document.getElementById("Word").style = "text-shadow: 2px 2px black;color:Red;transition-duration: 1.0s;font-size:150px;";
document.getElementById("Word").style =
"cursor: url('./pokeball.png'), pointer;text-shadow: 2px 2px black;color:Red;transition-duration: 1.0s;font-size:5rem;";
document.getElementById("Word").className = "gameLostPokemonName";
// Pokemon name speech synthesis
if ("speechSynthesis" in window) {
console.log("Web Speech API supported!");
} else {
console.log("Web Speech API not supported :-(");
}
const synth = window.speechSynthesis;
let ourText = "This Pokemon is named";
const utterThis = new SpeechSynthesisUtterance(ourText + Word);
utterThis.volume = 0.7;
synth.speak(utterThis);
// End of speech synth
window.setTimeout(function () {
//Alert that the game is over after a delay
document.getElementById("welcome").innerHTML =
"Game over! Total score is " + score;
score = 0;
document.getElementById("welcome").style =
"color:Tomato;font-size:30px;animation: slideMe .70s ease-in;";
document.getElementById("play").innerHTML = "Retry";
document.getElementById("play").style = null;
document.getElementById("play").className = "retryButton";
}, 7000);
//Show message first, then letter
imshow();
}
function GameWon() {
resetValuesEndGame();
music.pause();
music.currentTime = 0;
win.play();
document.getElementById("Word").className = "winText";
// Pokemon name speech synthesis
if ("speechSynthesis" in window) {
console.log("Web Speech API supported!");
} else {
console.log("Web Speech API not supported :-(");
}
const synth = window.speechSynthesis;
let ourText = "This Pokemon is named";
const utterThis = new SpeechSynthesisUtterance(ourText + Word);
synth.speak(utterThis);
// End of speech synth
//Show word first, then message with delay
window.setTimeout(function () {
score = score + TurnsLeft;
//Alert that the game is over after a delay
document.getElementById("welcome").innerHTML =
"Round passed! Current score is " + score;
document.getElementById("welcome").style = null;
document.getElementById("welcome").className = "roundPassed";
document.getElementById("play").innerHTML = "Next Round";
document.getElementById("play").style = null;
document.getElementById("play").className = "winButton";
correct.pause();
correct.currentTime = 0;
//pause trailing sounds
}, 7000);
imshow();
}
function resetValuesEndGame() {
document.getElementById("image").src =
SpriteLink + Word.toLowerCase() + ".jpg"; //to load image beforehand;
document.getElementById("hyperlink").href = InfoHyperLink + Word; //Camelcase word and not lowercade
win.pause();
win.currentTime = 0;
lost.pause();
lost.currentTime = 0;
correct.pause();
correct.currentTime = 0;
incorrect.pause();
incorrect.currentTime = 0;
document.getElementById("Word").innerHTML = Word;
document.getElementById("Word").style = null;
}
function imshow() {
music.pause();
music.currentTime = 0;
document.getElementById("TurnsLeft").style.display = "none";
for (var counter_gameover = 1; counter_gameover <= 16; counter_gameover++) {
document.getElementById("button" + counter_gameover).style.display = "none";
//Dispay the letters into the indivisual buttons.
}
//show the image
document.getElementById("body").style = null;
document.getElementById("body").className = "bodyImshow";
document.getElementById("image").style = null;
document.getElementById("image").className = "imageImshow";
}
window.addEventListener(
"beforeunload",
function (event) {
window.alert("Exit?");
},
false
);
const bg_pic = document.querySelector(".fg-pic");
const front_text = document.querySelector(".fg-elements");
const main = document.querySelector("main");
if (window.innerWidth < 1000) {
bg_pic.remove();
front_text.innerHTML += `
<div class="tooltip-div">
<button class="btn tooltip-btn" style="display:none" id="TurnsLeft"> </button>
<span class="tooltiptext">Turns left</span>
</div>
<a target="_blank" id="hyperlink">
<img id="image" style="transition-duration: 3.0s;display:none;width:5%">
</a>
<img id="pica-pic" src="./Assets/pikachu.png" alt="">
`;
main.style = `
grid-template-columns:1fr;
`;
}