Add Highscore Feature to Fake Insects Game

#1
Files changed (1) hide show
  1. index.html +16 -1
index.html CHANGED
@@ -99,6 +99,10 @@
99
  You found 0 fake insects in 25 seconds.
100
  </p>
101
  <p id="performance-percentage" class="text-md text-gray-500">You've outperformed 0% of players.</p>
 
 
 
 
102
  </div>
103
 
104
  <button
@@ -121,6 +125,7 @@
121
  const gameOverMessageElement = document.getElementById('game-over-message');
122
  const image1 = document.getElementById('image1');
123
  const image2 = document.getElementById('image2');
 
124
 
125
  let score = 0;
126
  let timeLeft = 25;
@@ -132,6 +137,10 @@
132
  // Start preloading images immediately
133
  preloadImages();
134
 
 
 
 
 
135
  function preloadImages(count = 20) {
136
  for (let i = 1; i <= count; i++) {
137
  const index = Math.floor(Math.random() * totalImages) + 1;
@@ -228,6 +237,12 @@
228
  }
229
  const performancePercentage = calculatePerformancePercentage(score);
230
  document.getElementById('performance-percentage').textContent = `You've outperformed ${performancePercentage}% of players.`;
 
 
 
 
 
 
231
  gameOverOverlay.classList.remove('hidden');
232
  }
233
 
@@ -278,4 +293,4 @@
278
  // Initial call to hide images with no src
279
  hideImagesWithNoSrc();
280
  </script>
281
- </html>
 
99
  You found 0 fake insects in 25 seconds.
100
  </p>
101
  <p id="performance-percentage" class="text-md text-gray-500">You've outperformed 0% of players.</p>
102
+ <p class="text-md text-gray-500">
103
+ Overall Highscore:
104
+ <span id="highscore" class="rounded-xl bg-black px-2 text-white">0</span>
105
+ </p>
106
  </div>
107
 
108
  <button
 
125
  const gameOverMessageElement = document.getElementById('game-over-message');
126
  const image1 = document.getElementById('image1');
127
  const image2 = document.getElementById('image2');
128
+ const highscoreElement = document.getElementById('highscore');
129
 
130
  let score = 0;
131
  let timeLeft = 25;
 
137
  // Start preloading images immediately
138
  preloadImages();
139
 
140
+ // Load highscore from localStorage
141
+ let highscore = localStorage.getItem('highscore') || 0;
142
+ highscoreElement.textContent = highscore;
143
+
144
  function preloadImages(count = 20) {
145
  for (let i = 1; i <= count; i++) {
146
  const index = Math.floor(Math.random() * totalImages) + 1;
 
237
  }
238
  const performancePercentage = calculatePerformancePercentage(score);
239
  document.getElementById('performance-percentage').textContent = `You've outperformed ${performancePercentage}% of players.`;
240
+ // Update highscore if needed
241
+ if (score > highscore) {
242
+ highscore = score;
243
+ localStorage.setItem('highscore', highscore);
244
+ highscoreElement.textContent = highscore;
245
+ }
246
  gameOverOverlay.classList.remove('hidden');
247
  }
248
 
 
293
  // Initial call to hide images with no src
294
  hideImagesWithNoSrc();
295
  </script>
296
+ </html>