forked from ritaban06/mini-js-games-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
279 lines (256 loc) · 8.21 KB
/
script.js
File metadata and controls
279 lines (256 loc) · 8.21 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
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
const games = [
{
name: "Tic Tac Toe",
path: "games/tictactoe/index.html",
icon: "❎",
description:
"Classic 3x3 strategy — outsmart your opponent before the grid fills up.",
category: "Strategy",
duration: "2 min rounds",
tags: ["2 players", "grid", "classic"],
},
{
name: "Snake Game",
path: "games/snake/index.html",
icon: "🐍",
description:
"Guide the snake, snack on pixels, and avoid hitting the walls or yourself.",
category: "Arcade",
duration: "Endless",
tags: ["arcade", "retro", "keyboard"],
},
{
name: "Memory Game",
path: "games/memory/index.html",
icon: "🧠",
description:
"Flip cards, remember emoji pairs, and clear the board in record time.",
category: "Brain Teaser",
duration: "5 min",
tags: ["memory", "solo", "matching"],
},
{
name: "Whack-a-Mole",
path: "games/whack-a-mole/index.html",
icon: "🔨",
description:
"Moles pop fast — keep your reflexes sharp to stack up the score.",
category: "Arcade",
duration: "30 sec",
tags: ["reflex", "timed", "mouse"],
},
{
name: "Reaction Timer",
path: "games/reaction-timer/index.html",
icon: "⚡",
description:
"Wait for green, tap quickly, and chase a new personal best reaction time.",
category: "Reflex",
duration: "Quick burst",
tags: ["speed", "focus", "solo"],
},
{
name: "Space Shooter",
path: "games/space-shooter/index.html",
icon: "🚀",
description:
"Fast-paced top-down shooter — dodge, weave and blast incoming waves.",
category: "Arcade",
duration: "Endless",
tags: ["arcade", "shooting", "keyboard"],
},
{
name: "2048",
path: "games/2048/index.html",
icon: "🔢",
description:
"Slide tiles to combine numbers and reach 2048. A relaxing puzzle of strategy and luck.",
category: "Puzzle",
duration: "10-20 min",
tags: ["puzzle", "singleplayer", "numbers"],
},
{
name: "15 Puzzle",
path: "games/15-puzzle/index.html",
icon: "🔳",
description:
"Arrange the numbered tiles in order by sliding them into the empty space. Classic spatial puzzle.",
category: "Puzzle",
duration: "5-15 min",
tags: ["puzzle", "tiles", "spatial"],
},
{
name: "Pong",
path: "games/pong/index.html",
icon: "🏓",
description:
"A tiny Pong clone — play against the CPU or another player. Use W/S and ↑/↓ to move paddles.",
category: "Arcade",
duration: "Endless",
tags: ["arcade", "retro", "multiplayer", "cpu"],
},
{
name: "Kong fights ",
path: "games/The Godzilla Fights game(html,css,js)/index.html",
icon: "🦍",
description: "A exciting fighting game where two cartoon gorillas stand on opposite rooftops in a cityscape at sunset. The player (on the left) aims and throws a bomb at the computer opponent by dragging to set the angle and velocity",
category: "Fighting",
duration: "Endless",
tags: ["Fighting", "Special", "multiplayer", "computer"],
},
{
name: "Simon Says",
path: "games/SimonSays/index.html",
icon: "🧠",
description: "A fun memory game where players repeat an increasingly complex sequence of colors.",
category: "Memory",
duration: "Progressive",
tags: ["memory", "focus", "puzzle", "challenge"],
},
{
name: "Typing Test",
path: "games/typing-test/index.html",
icon: "⌨️",
description:
"Test your typing speed and accuracy in 1 minute. Challenge yourself and improve!",
category: "Skill",
duration: "1 min",
tags: ["typing", "speed", "accuracy", "skill"],
},
{
name: "Balloon Pop",
path: "games/balloon-pop/index.html",
icon: "🎈",
description: "Click the balloons before they float away! Pop as many as you can.",
category: "Arcade",
duration: "Endless",
tags: ["arcade", "click", "fun", "score"],
}
];
const container = document.getElementById("games-container");
const searchInput = document.getElementById("game-search");
const emptyState = document.getElementById("empty-state");
const clearSearchButton = document.getElementById("clear-search");
const countTargets = document.querySelectorAll("[data-games-count]");
const latestTargets = document.querySelectorAll("[data-latest-game]");
const previewCount = document.querySelector("[data-preview-count]");
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
entry.target.classList.add("is-visible");
observer.unobserve(entry.target);
});
},
{ threshold: 0.4 }
);
const latestGameName = games.length ? games[games.length - 1].name : "--";
countTargets.forEach((el) => {
el.textContent = String(games.length);
});
latestTargets.forEach((el) => {
el.textContent = latestGameName;
});
if (previewCount) {
animateCount(previewCount, games.length, 920);
}
renderGames(games);
if (searchInput) {
searchInput.addEventListener("input", () => {
renderGames(filterGames(searchInput.value));
});
}
if (clearSearchButton) {
clearSearchButton.addEventListener("click", () => {
if (!searchInput) return;
searchInput.value = "";
searchInput.focus();
renderGames(games);
});
}
function renderGames(list) {
container.innerHTML = "";
if (!list.length) {
if (emptyState) emptyState.hidden = false;
return;
}
if (emptyState) emptyState.hidden = true;
list.forEach((game, index) => {
const card = document.createElement("article");
card.className = "game-card";
card.tabIndex = 0;
card.dataset.name = game.name.toLowerCase();
card.style.setProperty("--stagger", `${index * 60}ms`);
card.innerHTML = `
<div class="card-header">
<span class="card-pill">${game.icon} ${game.category}</span>
<span class="card-timing">${game.duration}</span>
</div>
<h3 class="card-title"><span>${game.icon}</span>${game.name}</h3>
<p class="card-body">${game.description}</p>
<div class="card-tags">
${game.tags.map((tag) => `<span>#${tag}</span>`).join("")}
</div>
<div class="card-actions">
<a class="play-button" href="${game.path}">Play now</a>
<a class="play-link" href="${game.path}" target="_blank" rel="noopener noreferrer">Open in new tab →</a>
</div>
`;
card.addEventListener("pointermove", handleCardTilt);
card.addEventListener("pointerleave", resetCardTilt);
card.addEventListener("focusout", resetCardTilt);
card.addEventListener("keydown", (event) => {
if (event.key !== "Enter" && event.key !== " ") return;
event.preventDefault();
window.open(game.path, "_blank", "noopener,noreferrer");
});
container.appendChild(card);
observer.observe(card);
});
}
function filterGames(rawTerm) {
const term = rawTerm.trim().toLowerCase();
if (!term) return games;
return games.filter((game) => {
const haystack = [
game.name,
game.category,
game.description,
...game.tags,
]
.join(" ")
.toLowerCase();
return haystack.includes(term);
});
}
function handleCardTilt(event) {
const card = event.currentTarget;
const rect = card.getBoundingClientRect();
const relativeX = (event.clientX - rect.left) / rect.width;
const relativeY = (event.clientY - rect.top) / rect.height;
const tiltX = (0.5 - relativeY) * 8;
const tiltY = (relativeX - 0.5) * 8;
card.style.setProperty("--tiltX", `${tiltX.toFixed(2)}deg`);
card.style.setProperty("--tiltY", `${tiltY.toFixed(2)}deg`);
}
function resetCardTilt(event) {
const card = event.currentTarget;
card.style.setProperty("--tiltX", "0deg");
card.style.setProperty("--tiltY", "0deg");
}
function animateCount(node, target, duration) {
const start = Number(node.textContent) || 0;
const startTime = performance.now();
const tick = (now) => {
const elapsed = now - startTime;
const progress = Math.min(elapsed / duration, 1);
const eased = easeOutCubic(progress);
const value = Math.round(start + (target - start) * eased);
node.textContent = value.toString().padStart(2, "0");
if (progress < 1) requestAnimationFrame(tick);
};
requestAnimationFrame(tick);
}
function easeOutCubic(t) {
return 1 - Math.pow(1 - t, 3);
}