-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtype.js
More file actions
217 lines (176 loc) · 3.96 KB
/
type.js
File metadata and controls
217 lines (176 loc) · 3.96 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
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
var word = document.querySelector('#word');
var text = document.querySelector('#text');
var scoreEl = document.querySelector('#score');
var timeEl = document.querySelector('#time');
var endgameEl = document.querySelector('#end-game-container');
var settingsBtn = document.querySelector('#settings-btn');
var settings = document.querySelector('#settings');
var settingsForm = document.querySelector('#settings-form');
var difficultySelect = document.querySelector('#difficulty');
// List of words for game
var words = [
'sigh',
'tense',
'adieu',
'tares',
'soare',
'ducat',
'ouija',
'carom',
'ergot',
'craic',
'squab',
'enoki',
'azure',
'remuda',
'coulee',
'chador',
'ihotse',
'ajijic',
'twibil',
'wapiti',
'snazzy',
'north',
'quince',
'airplane',
'warlike',
'feeble',
'crime',
'drag',
'loving',
'dependent',
'steer',
'silver',
'richard',
'speeding',
'independent',
'caring',
'javascript',
'highfalutin',
'superficial',
'accommodation',
'verborois',
'difficulty',
'onomatopoeia',
'maltulsian',
'totalitarianism',
'synedoche',
'oxymoron',
'peculiar'
];
var scores=[];
// Init score
var score = 0;
function neutralize(){
scoreEl.innerHTML=0;
score=0;
}
var audio = new Audio();
audio.src="https://www.soundjay.com/clock/clock-ticking-2.mp3";
window.onclick=function(){
audio.play();
audio.loop=true;
}
let startgame=()=>{
var randomWord;
var time = 15;
// Set difficulty to value in ls or medium
var difficulty ='medium';
// Set difficulty select value
difficultySelect.value ='medium';
// Focus on text on start
text.focus();
endgameEl.style.display = 'none';
// Start counting down
var timeInterval = setInterval(updateTime, 1000);
// Generate random word from array
function getRandomWord() {
if(difficulty==='easy'){
return words[Math.floor(Math.random() * 16)];
}
else if(difficulty==='medium'){
return words[(Math.floor(Math.random() * 16))+16];
}
else{
return words[(Math.floor(Math.random() * 16))+32];
}
}
// Add word to DOM
function addWordToDOM() {
randomWord = getRandomWord();
word.innerHTML = randomWord;
}
// Update score
function updateScore() {
if(time!=0){
score++;
scoreEl.innerHTML = score;
}
else if(time===0){
}
else{}
}
// Update time
function updateTime() {
time--;
timeEl.innerHTML = time + 's';
if (time === 0) {
clearInterval(timeInterval);
score=0;
// end game
gameOver();
}
}
addWordToDOM();
// Event listeners
// Typing
text.addEventListener('input', e => {
var insertedText = e.target.value;
if (insertedText === randomWord) {
addWordToDOM();
updateScore();
responsiveVoice.speak("Great","US English Female",{rate:1.1,volume:90,pitch:0.6});
// Clear
e.target.value = '';
if (difficulty === 'hard') {
time += 1;
} else if (difficulty === 'medium') {
time += 2;
} else {
time += 3;
}
updateTime();
}
});
}
setTimeout(()=>{
document.querySelector("#loader").style.display="none" ;
startgame();
},5000);
// Settings btn click
settingsBtn.addEventListener('click', () => settings.classList.toggle('hide'));
// Settings select
settingsForm.addEventListener('change', e => {
difficulty = e.target.value;
gameOver();
});
function gameOver() {
endgameEl.innerHTML = `
<h1>Time ran out</h1>
<p>Your final score is ${scoreEl.textContent}</p>
<button onclick="startgame()">Restart</button>`;
responsiveVoice.speak("Game Over","US English Female",{rate:1.1,volume:90,pitch:0.6});
scores.push(scoreEl.textContent);
scores.push(scoreEl.textContent);
audio.pause();
neutralize();
neutralize();
document.querySelector("#high").innerHTML="Your high score is "+ Math.max(...scores);
endgameEl.style.display = 'flex';
}
document.querySelector("#loader").style=`
height:${window.innerHeight};
width:${window.innerWidth};
align-items:center;
justify-content:center;
`