Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ac-unefm/snake-game/llms.txt

Use this file to discover all available pages before exploring further.

The only data persisted across sessions is the player’s best score, stored in localStorage under the key snakeBest. Both the read and write operations are wrapped in try/catch blocks so that storage errors — including QuotaExceededError, security restrictions in private-browsing mode, or any other browser-level failure — never crash the game or interrupt play.

readBestScore()

Called once at startup to initialise the best variable and the #bestEl HUD element:
function readBestScore() {
  try {
    return parseInt(localStorage.getItem('snakeBest') || '0', 10);
  } catch (storageError) {
    console.error('No se pudo leer la puntuación guardada:', storageError);
    return 0;
  }
}
  • localStorage.getItem('snakeBest') returns null when the key has never been set. The || '0' short-circuit converts that null to the string '0' before parsing.
  • parseInt(..., 10) ensures the result is always a safe integer, even if the stored value was somehow corrupted.
  • On any error, the function returns 0 and logs to the console. The game starts with a best score of 0 and continues normally.

saveBestScore(score)

Called inside update() each time the player sets a new best score:
function saveBestScore(puntuacion) {
  try {
    localStorage.setItem('snakeBest', puntuacion);
  } catch (storageError) {
    console.error('No se pudo guardar la puntuación:', storageError);
  }
}
  • If storage is unavailable (e.g., quota exceeded, private browsing restrictions), the error is caught and logged. The in-memory best variable has already been updated before this call, so the current session’s HUD continues to display the correct value — only persistence fails, silently and without affecting gameplay.

Storage key

The only localStorage key the game uses is snakeBest. Clearing it resets the best score display to 0 on the next page load. You can reset it manually in the browser console:
localStorage.removeItem('snakeBest');
Reloading the page after running this command will show BEST: 0 in the HUD.

When best score is updated

Inside update(), immediately after incrementing score when food is eaten:
if (score > best) {
  best = score;
  bestEl.textContent = best;
  saveBestScore(best);
}
The update order is:
  1. The in-memory best variable is overwritten with the new value.
  2. The #bestEl DOM element is updated so the HUD reflects the new best immediately.
  3. saveBestScore(best) writes the value to localStorage.
This ordering guarantees that even if saveBestScore throws, both the in-memory state and the visible HUD are already correct for the remainder of the session.

Browser compatibility

localStorage is available in all modern browsers and is specified by the HTML Living Standard. The game works correctly even when localStorage is unavailable — for example, in some browsers’ private-browsing modes that block storage writes, or in sandboxed iframes with restricted storage access. In those cases the best score resets to 0 on each page load, but all gameplay features function normally.

Build docs developers (and LLMs) love