forked from bharvey88/july4-game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
90 lines (79 loc) · 2.69 KB
/
Copy pathscript.js
File metadata and controls
90 lines (79 loc) · 2.69 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
const IMAGES = [
'images/A Mark Blue-Green.png',
'images/A Mark Blue-Orange.png',
'images/A Mark Blue-Red.png',
'images/A Mark Greyscale.png',
'images/A Mark Navy-Green.png',
'images/A Mark White.png'
];
const NUM_HOLES = 9, TAPS_TO_WIN = 5, DISPLAY_TIME = 1100;
let taps = 0, currentTimeout = null;
const intro = document.getElementById('intro');
const game = document.getElementById('game');
const result = document.getElementById('result');
const board = document.getElementById('game-board');
const tapsCount = document.getElementById('taps-count');
const pipsWrap = document.getElementById('pips');
const startBtn = document.getElementById('start');
const holes = [];
// fireworks background (optional — guard so the game still works if the CDN is blocked)
let fireworks = null;
if (typeof Fireworks !== 'undefined') {
fireworks = new Fireworks(document.body, {
speed: 3, acceleration: 1.05, friction: 0.97, gravity: 1.5,
particles: 50, trace: 3, autoresize: true,
hue: { min: 0, max: 360 }, delay: { min: 12, max: 36 },
rocketsPoint: { min: 0, max: 100 }
});
fireworks.start();
}
// build board + progress pips
for (let i = 0; i < NUM_HOLES; i++) {
const hole = document.createElement('div');
hole.className = 'hole';
board.appendChild(hole);
holes.push(hole);
}
for (let i = 0; i < TAPS_TO_WIN; i++) {
const pip = document.createElement('span');
pip.className = 'pip';
pipsWrap.appendChild(pip);
}
const pips = Array.from(pipsWrap.children);
function updateProgress() {
tapsCount.textContent = `${taps} / ${TAPS_TO_WIN}`;
pips.forEach((pip, i) => pip.classList.toggle('on', i < taps));
}
function showMole() {
holes.forEach(h => h.innerHTML = '');
const hole = holes[Math.floor(Math.random() * NUM_HOLES)];
const img = document.createElement('img');
img.src = IMAGES[Math.floor(Math.random() * IMAGES.length)];
img.className = 'mole';
img.alt = 'Apollo mark';
hole.appendChild(img);
img.addEventListener('click', () => {
if (fireworks) {
const r = img.getBoundingClientRect();
fireworks.fire({ x: r.left + r.width / 2, y: r.top + r.height / 2, particles: 24, trace: 2 });
}
taps++;
updateProgress();
hole.innerHTML = '<div class="celebration">🎆</div>';
if (taps >= TAPS_TO_WIN) { endGame(); return; }
});
currentTimeout = setTimeout(showMole, DISPLAY_TIME);
}
function startGame() {
intro.classList.add('hidden');
game.classList.remove('hidden');
updateProgress();
showMole();
}
function endGame() {
clearTimeout(currentTimeout);
game.classList.add('hidden');
result.classList.remove('hidden');
result.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
startBtn.addEventListener('click', startGame);