Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
146 lines (123 loc) · 4.81 KB
/
Copy pathscript.js
File metadata and controls
146 lines (123 loc) · 4.81 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
document.addEventListener('DOMContentLoaded', () => {
// --- Floating Menu (works on all pages) ---
const menuCircle = document.getElementById('menu-circle');
const slideMenu = document.getElementById('slide-menu');
if (menuCircle && slideMenu) {
menuCircle.addEventListener('click', () => {
slideMenu.style.left = slideMenu.style.left === '0px' ? '-250px' : '0px';
});
document.addEventListener('click', (e) => {
if (!slideMenu.contains(e.target) && e.target !== menuCircle) {
slideMenu.style.left = '-250px';
}
});
slideMenu.querySelectorAll('a').forEach(link => {
link.addEventListener('click', () => {
slideMenu.style.left = '-250px';
});
});
}
// --- Game (only runs if game elements exist) ---
const target = document.getElementById('target');
if (!target) return; // stop here on non-game pages
const gameContainer = document.getElementById('game-container');
const scoreBoard = document.getElementById('scoreboard');
const timerDisplay = document.getElementById('timer');
const tipSection = document.getElementById('tip-section');
const restartBtn = document.getElementById('restart-btn');
const scoresTable = document.getElementById('scores-table');
const scoresTbody = scoresTable.querySelector('tbody');
let score = 0;
let timeLeft = 30;
let gameInterval;
let timerInterval;
let gameActive = false;
const STORAGE_KEY = 'ankkrypt_fireclicker_scores';
function loadScores() {
return JSON.parse(localStorage.getItem(STORAGE_KEY)) || [];
}
function saveScores(scores) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(scores));
}
function addScoreRecord(score) {
const scores = loadScores();
const now = new Date();
scores.unshift({ timestamp: now.toISOString(), score });
saveScores(scores);
renderScores();
}
function deleteScoreRecord(index) {
const scores = loadScores();
scores.splice(index, 1);
saveScores(scores);
renderScores();
}
function renderScores() {
const scores = loadScores();
scoresTbody.innerHTML = '';
if (scores.length === 0) {
scoresTable.style.display = 'none';
return;
}
scoresTable.style.display = 'table';
scores.forEach((record, i) => {
const tr = document.createElement('tr');
const date = new Date(record.timestamp);
tr.innerHTML = `
<td>${date.toLocaleString()}</td>
<td>${record.score}</td>
<td><button class="delete-btn">Delete</button></td>
`;
tr.querySelector('button').addEventListener('click', () => deleteScoreRecord(i));
scoresTbody.appendChild(tr);
});
}
function moveTarget() {
const containerRect = gameContainer.getBoundingClientRect();
const maxX = containerRect.width - target.offsetWidth;
const maxY = containerRect.height - target.offsetHeight;
const randomX = Math.random() * maxX;
const randomY = Math.random() * maxY;
target.style.opacity = 0;
setTimeout(() => {
target.style.left = randomX + 'px';
target.style.top = randomY + 'px';
target.style.opacity = 1;
}, 200);
}
function updateScore() { scoreBoard.textContent = `Score: ${score}`; }
function updateTimer() { timerDisplay.textContent = `Time left: ${timeLeft}s`; }
function onTargetClick() { if (!gameActive) return; score++; updateScore(); moveTarget(); }
function startGame() {
score = 0;
timeLeft = 30;
gameActive = true;
tipSection.style.display = 'none';
target.style.display = 'block';
updateScore();
updateTimer();
moveTarget();
clearInterval(gameInterval);
clearInterval(timerInterval);
gameInterval = setInterval(moveTarget, 600);
timerInterval = setInterval(() => {
timeLeft--;
updateTimer();
if (timeLeft <= 0) endGame();
}, 1000);
}
function endGame() {
gameActive = false;
clearInterval(gameInterval);
clearInterval(timerInterval);
timerDisplay.textContent = `Game Over! Final Score: ${score}`;
target.style.display = 'none';
tipSection.style.display = 'block';
addScoreRecord(score);
}
target.addEventListener('click', () => { if (!gameActive) startGame(); onTargetClick(); });
target.addEventListener('keydown', e => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); target.click(); } });
restartBtn.addEventListener('click', startGame);
renderScores();
startGame();
});