-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
222 lines (157 loc) · 7.31 KB
/
game.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
const words = 'typing games are interactive software applications designed to improve typing skills while making the learning process engaging and entertaining. These games typically present players with various challenges, such as typing words, sentences, or paragraphs within a time limit. The games often include features like accuracy tracking, speed measurement, and progression levels, motivating players to enhance their typing proficiency. By combining the practice of typing with elements of fun and competition, typing games have become a popular way for people of all ages to develop and refine their typing abilities while enjoying the process'.split(' ');
let wordCount = words.length;
const gameTime = 30 * 1000
window.timer = null;
window.gameStart = null
function addClass(element,name){
element.className += ' '+name
}
function removeClass(element,name){
element.className = element.className.replace(name,'')
}
function formatWord(word){
let main = document.createElement("div")
main.className = "word"
for(let i=0;i<word.length;i++){
let letterSpan = document.createElement("span")
letterSpan.className= 'letter'
letterSpan.textContent = word[i]
main.appendChild(letterSpan)
}
return main
}
//adding words to screen
function newGame() {
document.getElementById('words').innerHTML = '';
for (let i = 0; i < words.length; i++) {
const random = Math.floor(Math.random() * words.length);
let x = formatWord(words[random])
document.getElementById('words').appendChild(x)
}
addClass(document.querySelector('.word'),'current')
addClass(document.querySelector('.letter'),'current')
document.getElementById('info').innerHTML = (gameTime/1000)+""
window.timer = null;
}
function getWpm(){
const words = [...document.querySelectorAll('.word')]
const lastType = document.querySelector('.word.current')
const lastIndex = words.indexOf(lastType)
const typeWords = words.slice(0,lastIndex)
const correctWords = typeWords.filter(word=>{
const letters = [...word.children]
const incorrectletters = letters.filter(letter=>letter.className.includes('incorrect'))
const correctlettters = letters.filter(letter=>letter.className.includes('correct'))
return incorrectletters.length ===0 && correctlettters.length===letters.length
})
return correctWords.length / gameTime *60000
}
function gameOver(){
clearInterval(window.timer)
addClass(document.getElementById('game'),'over')
const result = getWpm();
document.getElementById('info').innerHTML = `WPN ${result}`
}
document.getElementById('game').addEventListener('keyup',(event)=>{
const key = event.key;
const currentWord = document.querySelector('.word.current')
const currentletter = document.querySelector('.letter.current')
const expected = currentletter?.innerHTML ||' ';
const isLetter = key.length ===1 &&key !== ' ';
const isSpace = key === ' '
const isBackSpace = key === 'Backspace'
const isFirstLetter = currentletter ===currentWord.firstChild
if(document.querySelector('#game.over')){
return;
}
console.log({key,expected})
if(!window.timer && isLetter){
window.timer = setInterval(()=>{
if(!window.gameStart){
window.gameStart = (new Date()).getTime();
}
const currentTime = (new Date()).getTime()
const msPassed = currentTime - window.gameStart
const sPassed = Math.round(msPassed/1000)
const secLeft = (gameTime/1000)- sPassed
if(secLeft<=0){
gameOver();
return;
}
document.getElementById('info').innerHTML = secLeft + ''
},1000)
}
if(isLetter){
if(currentletter){
addClass(currentletter,key===expected?"correct":"incorrect")
removeClass(currentletter,'current')
if(currentletter.nextSibling){
addClass(currentletter.nextSibling,'current')
}
}else{
const incorrectLetter = document.createElement('span')
incorrectLetter.innerHTML = key
incorrectLetter.className = 'letter incorrect extra'
currentWord.appendChild(incorrectLetter)
}
}
if(isSpace){
if(expected !== ' '){
const letterToInvalidate = [...document.querySelectorAll('.word.current .letter:not(.correct)')]
letterToInvalidate.forEach((letter)=>{
addClass(letter,'incorrect')
})
}
removeClass(currentWord,'current')
addClass(currentWord.nextSibling,'current')
if(currentletter){
removeClass(currentletter,'current')
}
addClass(currentWord.nextSibling.firstChild,'current')
}
if(isBackSpace){
if(currentletter && isFirstLetter){
removeClass(currentWord, 'current');
addClass(currentWord.previousSibling, 'current');
removeClass(currentletter, 'current');
addClass(currentWord.previousSibling.lastChild, 'current');
removeClass(currentWord.previousSibling.lastChild, 'incorrect');
removeClass(currentWord.previousSibling.lastChild, 'correct');
}
if(currentletter && !isFirstLetter){
removeClass(currentletter, 'current');
addClass(currentletter.previousSibling, 'current');
removeClass(currentletter.previousSibling, 'incorrect');
removeClass(currentletter.previousSibling, 'correct');
}
if(!currentletter){
addClass(currentWord.lastChild,'current')
removeClass(currentletter.previousSibling,'incorrect')
removeClass(currentletter.previousSibling,'correct')
}
}
//move lines
if(currentWord.getBoundingClientRect().top>400){
const words = document.getElementById('words')
const margin = parseInt(words.style.marginTop || '0px')
words.style.marginTop = (margin- 35) + 'px'
alert('game')
}
//move cursour
const nextLetter = document.querySelector('.letter.current')
const cursor = document.getElementById('cursor')
const nextWord = document.querySelector('.word.current')
if (nextLetter) {
const nextLetterRect = nextLetter.getBoundingClientRect();
// Adjust cursor position using the next letter's coordinates
cursor.style.top = nextLetterRect.top + 3+ 'px';
cursor.style.left = nextLetterRect.left +'px';
}else{
cursor.style.top = nextWord.getBoundingClientRect().top+2+'px'
cursor.style.left = nextWord.getBoundingClientRect().right+'px'
}
})
document.getElementById('restart').addEventListener('click',()=>{
window.location.reload();
})
newGame();