-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.js
More file actions
128 lines (115 loc) · 2.75 KB
/
Game.js
File metadata and controls
128 lines (115 loc) · 2.75 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
// A Typing Speed Game
let __author__ = 'Michael Khoshahang';
window.addEventListener('load', init);
// Global variables
// Available Levels
const levels = {
easy: 5,
medium: 3,
hard: 1
};
// getting random level
const currentLevel = Object.values(levels)[Math.floor(Math.random() * Object.keys(levels).length)];
let time = currentLevel;
let score = 0;
let isPlaying;
// DOM Elements
const wordInput = document.querySelector('#word-input');
const currentWord = document.querySelector('#current-word');
const scoreDisplay = document.querySelector('#score');
const timeDisplay = document.querySelector('#time');
const message = document.querySelector('#message');
const seconds = document.querySelector('#seconds');
const highscoreDisplay = document.querySelector('#highscore');
const words = [
'hat',
'river',
'lucky',
'statue',
'generate',
'stubborn',
'cocktail',
'runaway',
'joke',
'developer',
'establishment',
'hero',
'javascript',
'nutrition',
'revolver',
'echo',
'siblings',
'investigate',
'horrendous',
'symptom',
'laughter',
'magic',
'master',
'space',
'definition'
];
// Initializing game
function init() {
// Show number of seconds in UI
seconds.innerHTML = currentLevel;
// Loading random word from array
showWord(words);
// Start match when a word is inputed
wordInput.addEventListener('input', startMatch);
// Call countdown every second
setInterval(countdown, 1000);
// Checking the status of the game
setInterval(checkStatus, 50)
}
// Start match
function startMatch() {
if(matchWords()){
isPlaying = true;
time = currentLevel + 1;
showWord(words);
wordInput.value = '';
score++;
}
// if the score is -1, display 0
if (score === -1) {
scoreDisplay.innerHTML = 0;
}
else {
scoreDisplay.innerHTML = score;
}
}
// Match the current word to the inputed word
function matchWords() {
if(wordInput.value === currentWord.innerHTML) {
message.innerHTML = 'Correct!';
return true;
}
else {
message.innerHTML = '';
return false;
}
}
// The function shows the random word from the array
function showWord(array) {
currentWord.innerHTML = array[Math.floor(Math.random() * array.length)]
}
// Countdown timer
function countdown() {
// Make sure time is not run out
if (time > 0) {
// Decrement
time--;
} else if (time === 0) {
// Game is over
isPlaying = false;
}
// Show time
timeDisplay.innerHTML = time;
}
// Check game status
function checkStatus() {
if (!isPlaying && time === 0) {
message.innerHTML = 'Game Over!!!';
score = -1;
}
}