-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
241 lines (195 loc) · 7.66 KB
/
app.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
const keys = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
const gameBoard = document.getElementById("game-board");
const speedController = document.getElementById("speed-controller");
const speedLabel = document.getElementById("speed-label");
const speedInput = document.getElementById("speed-input");
const speedValue = document.getElementById("speed-value");
const scoreDisplay = document.getElementById("scoreDisplay");
let startTime = new Date();
let seconds = 0;
let minutes = 0;
let hours = 0;
let fallingSpeed = 5000;
let numKeysFalling = 0;
let fallingKeys = [];
let numKeysKnockedOut = 0;
setInterval(updateTime, 1000);
setInterval(createKey, 500);
const resetButton = document.getElementById("reset-button");
function resetGame() {
// Clear falling keys
fallingKeys.forEach((key) => key.remove());
fallingKeys = [];
numKeysFalling = 0;
numKeysKnockedOut = 0;
// Reset time
startTime = new Date();
// Reset score display
updateTime();
updateKeyOrder();
}
resetButton.addEventListener("click", resetGame);
function updateTime() {
const now = new Date();
const timeDiffInSeconds = Math.floor((now - startTime) / 1000);
hours = Math.floor(timeDiffInSeconds / 3600);
minutes = Math.floor((timeDiffInSeconds % 3600) / 60);
seconds = timeDiffInSeconds % 60;
const timeString = `${hours > 0 ? hours + " hour" + (hours > 1 ? "s" : "") + " " : ""}${minutes > 0 ? minutes + " minute" + (minutes > 1 ? "s" : "") + " " : ""}${seconds} second${seconds > 1 ? "s" : ""}`;
scoreDisplay.innerHTML = `${numKeysKnockedOut} keys knocked out <br> within ${timeString}`;
}
function createKey() {
if (numKeysFalling >= 20) return;
numKeysFalling++;
const key = document.createElement("div");
key.classList.add("key");
const isLetter = Math.floor(Math.random() * 2) === 0;
if (isLetter) {
key.classList.add("letter");
key.style.backgroundColor = "#F7DC6F";
} else {
key.classList.add("digit");
key.style.backgroundColor = "#D7BDE2";
}
key.innerHTML = keys[Math.floor(Math.random() * keys.length)];
key.style.left = Math.floor(Math.random() * (gameBoard.offsetWidth - key.offsetWidth *2)) + "px";
gameBoard.appendChild(key);
const keySize = 90;
key.style.width = `${keySize}px`;
key.style.height = `${keySize}px`;
key.style.lineHeight = `${keySize}px`;
key.style.borderRadius = `${keySize / 2}px`;
// set the maximum X position to the game board's width minus the key's width
const maxPosX = gameBoard.offsetWidth - key.offsetWidth;
// set a random X position within the visible area
key.style.left = Math.floor(Math.random() * maxPosX) + "px";
// Set random falling speed between 80% and 120% of the default value (5000ms)
const fallingSpeed = 5000 * (Math.random() * 1.5 + 0.1);
// add event listeners for both click and touchstart
key.addEventListener("click", onKeyClick);
key.addEventListener("touchstart", onKeyClick);
setTimeout(() => {
numKeysFalling--;
fallingKeys = fallingKeys.filter((k) => k !== key);
key.remove();
updateKeyOrder();
}, fallingSpeed);
key.animate(
[
{ transform: "translateY(0)" },
{ transform: `translateY(${gameBoard.offsetHeight}px)` },
],
{
duration: fallingSpeed,
easing: "linear",
fill: "forwards",
}
);
fallingKeys.push(key);
updateKeyOrder();
}
document.addEventListener("keydown", (event) => {
if (!event.key.match(/^[a-zA-Z0-9]$/)) return;
const lowestKey = getLowestKey();
if (lowestKey && lowestKey.innerHTML === event.key.toUpperCase()) {
const success = document.createElement("div");
success.classList.add("success");
success.innerHTML = "✔";
success.style.top = lowestKey.offsetTop + "px";
success.style.left = lowestKey.offsetLeft + "px";
gameBoard.appendChild(success);
numKeysKnockedOut++;
setTimeout(() => {
success.remove();
lowestKey.classList.add("success-highlight", "blink");
setTimeout(() => {
numKeysFalling--;
fallingKeys = fallingKeys.filter((k) => k !== lowestKey);
lowestKey.remove();
updateKeyOrder();
}, 100);
}, 200);
lowestKey.style.backgroundColor = "#87cefa";
setTimeout(() => {
lowestKey.style.backgroundColor = "#ffffff";
}, 200);
}
});
function getLowestKey() {
const sortedKeys = fallingKeys.slice().sort((a, b) => {
return b.getBoundingClientRect().bottom - a.getBoundingClientRect().bottom;
});
return sortedKeys[0];
}
// function updateKeyOrder() {
// for (let i = 0; i < fallingKeys.length; i++) {
// const key = fallingKeys[i];
// key.classList.remove("closest-to-bottom");
// }
// const lowestKey = getLowestKey();
// if (lowestKey) {
// lowestKey.classList.add("closest-to-bottom");
// }
// }
function onKeyClick(event) {
const clickedElement = document.elementFromPoint(
event.clientX,
event.clientY
);
if (!clickedElement.classList.contains("key")) {
return;
}
if (clickedElement.classList.contains("clicked")) {
return;
}
clickedElement.classList.add("clicked");
setTimeout(() => {
clickedElement.classList.remove("clicked");
}, 300);
const success = document.createElement("div");
success.classList.add("success");
success.innerHTML = "✔";
success.style.top = clickedElement.offsetTop + "px";
success.style.left = clickedElement.offsetLeft + "px";
gameBoard.appendChild(success);
numKeysKnockedOut++;
setTimeout(() => {
success.remove();
clickedElement.classList.add("success-highlight", "blink");
setTimeout(() => {
numKeysFalling--;
fallingKeys = fallingKeys.filter((k) => k !== clickedElement);
clickedElement.remove();
updateKeyOrder();
allowClick = true; // Set allowClick to true again
}, 100);
}, 200);
clickedElement.style.backgroundColor = "#87cefa";
setTimeout(() => {
clickedElement.style.backgroundColor = "#ffffff";
clickedElement.classList.remove("success-highlight"); // Add this line to remove the class after the delay
}, 200);
}
const modal = document.querySelector('.modal');
const aboutButton = document.querySelector('#about-button');
const closeButton = document.querySelector('.modal-close');
// Show the modal when the About Game button is clicked
aboutButton.addEventListener('click', () => {
modal.style.display = 'flex';
});
// Hide the modal when the close button is clicked
closeButton.addEventListener('click', () => {
modal.style.display = 'none';
});
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
// When the user taps anywhere outside of the modal, close it
window.addEventListener('touchend', (event) => {
if (event.target == modal) {
modal.style.display = "none";
}
});