PatchHub

← patchhub/snake

Snake: live best score saved instantly

Published ✓ Safe by @sam-k 0 users1 vote 1 file changed (+16 −4)

In the Snake game, the "Best" score now goes up as soon as you pass your old record, and it's saved right away instead of only when the game ends. Closing the page mid-game no longer loses a new record.

What it does

- Game already shows a "Best" score next to the current score and persists it in browser localStorage under the key "snakeBest" (default 0 if missing, unreadable, or storage unavailable). - Add a per-game flag (e.g. beatBest), reset to false whenever a new game starts, meaning this game set a new best. - Add an updateBest routine: if the current score is greater than the stored best, set best to score, set beatBest to true, update the Best display, and save it to localStorage ("snakeBest") inside a try/catch that ignores storage errors. - Call updateBest every time the player eats food and the score increases, so Best rises live during play and is saved immediately. - At game over (including the win case when the board is full), call updateBest once more, then show "New best score!" on the Game Over screen only if beatBest is true; otherwise hide that message. - The Game Over heading, final score display, and other behavior are unchanged. - Best score remains stored only in the current browser; a different browser or cleared data starts from 0.

Safety check ✓ Safe

The change is small and local to the game's scoring logic. It only reads and writes one value in the browser's localStorage, with no network requests, new dependencies, or unrelated changes.

Conversation that created this patch
@sam-k
Remember the high score between visits and show it next to the score.
AI
I made a small improvement to how the best score is saved. Your game already did most of this: it shows "Best" next to your score at the top and remembers it the next time you open the game. There was one gap. The best score only updated and saved when a game ended, so if you closed the page partway through a record-breaking game, that record was lost. Now "Best" goes up live as soon as you pass your old record, and it's saved right away. You'll still see "New best score!" on the Game Over screen when you beat it. I checked that the game's code has no errors, but I haven't played it in a browser. One thing to know: the best score is stored in your web browser. If you open the game in a different browser or on another computer, or clear your browsing data, it starts from zero.
Code changes (for the curious, 1 file) · raw
index.html +16 −4
@@ -122,6 +122,7 @@
let snake, direction, inputQueue, food, score, speed, timer;
let state = "start"; // "start" | "playing" | "paused" | "over"
+ let beatBest = false; // did this game set a new best score?
let best = 0;
try { best = parseInt(localStorage.getItem("snakeBest"), 10) || 0; } catch (e) {}
@@ -144,6 +145,7 @@
direction = { x: 1, y: 0 };
inputQueue = [];
score = 0;
+ beatBest = false;
speed = START_SPEED;
scoreEl.textContent = score;
placeFood();
@@ -200,6 +202,7 @@
if (eating) {
score++;
scoreEl.textContent = score;
+ updateBest();
speed = Math.max(MIN_SPEED, speed - 3);
placeFood();
if (!food) { draw(); gameOver(true); return; } // board full: you win!
@@ -217,10 +220,8 @@
draw(true);
finalScoreEl.textContent = score;
gameOverScreen.querySelector("h2").textContent = won ? "You Win!" : "Game Over";
− if (score > best) {
− best = score;
− bestEl.textContent = best;
− try { localStorage.setItem("snakeBest", String(best)); } catch (e) {}
+ updateBest();
+ if (beatBest) {
newBestEl.textContent = "New best score!";
newBestEl.classList.remove("hidden");
} else {
@@ -230,6 +231,17 @@
document.getElementById("restartBtn").focus();
}
+ // Update and save the best score as soon as it's beaten, so it's kept
+ // even if the page is closed in the middle of a game.
+ function updateBest() {
+ if (score > best) {
+ best = score;
+ beatBest = true;
+ bestEl.textContent = best;
+ try { localStorage.setItem("snakeBest", String(best)); } catch (e) {}
+ }
+ }
+
function togglePause() {
if (state === "playing") {
state = "paused";