Skip to content

Commit

Permalink
fixed high score bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispsang committed Jun 13, 2024
1 parent bde9d0a commit 799c2d0
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions games/Snakegame/snakegame.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ let apple = {
};

let score = 0;
let highScore = 0;
let isGuest = localStorage.getItem("userEmail") === "Guest";
let gameOver = false;
let gameStarted = false;
let gamePaused = false; // Variable to track whether the game is paused
Expand All @@ -39,23 +41,17 @@ var firebaseConfig = {
};


let isGuest = localStorage.getItem("userEmail") === "Guest";


if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
} else {
firebase.app();
}


let highScore = 0;
if (isGuest) {
highScore = parseInt(localStorage.getItem("highScore"));
highScore = parseInt(localStorage.getItem("snakeGameHighScore")) || 0;
} else {
getHighScore();
}
getHighScore();
// const highScoreDisplay = document.getElementById("high-score");
// highScoreDisplay.innerText = highScore;

const scoreBox = document.getElementById("score");

Expand Down Expand Up @@ -102,13 +98,12 @@ function resetGame() {
function updateScore() {
if (score > highScore) {
highScore = score;
// highScoreDisplay.innerText = score;
if (!isGuest) {
updateFirebase();
updateLeaderboard(userEmail, score);
}
else {
localStorage.setItem("highScore", score);
localStorage.setItem("snakeGameHighScore", score);
}

}
Expand Down Expand Up @@ -423,7 +418,7 @@ function fetchLeaderboard() {
});

// Always include the guest score if it exists in local storage
const guestScore = parseInt(localStorage.getItem("highScore")) || 0;
const guestScore = parseInt(localStorage.getItem("snakeGameHighScore")) || 0;
if (guestScore) {
highestScores["Guest"] = guestScore;
}
Expand Down

0 comments on commit 799c2d0

Please sign in to comment.