Skip to content

Commit

Permalink
Fix snake localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
gamalielhere committed Feb 3, 2016
1 parent c1eb192 commit b760429
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// $(document).ready(function() {
$(document).ready(function() {
// CANVAS PART
var canvas = $("#canvas")[0];
var canvasContext = canvas.getContext("2d");
Expand All @@ -13,7 +13,7 @@
var snake;
var score;
var speed;
var highscore = 0;
var highscore = localStorage.getItem("highscore") || 0;
var headColor = "green",
colorFood = "yellow",
bodyColor = "white",
Expand Down Expand Up @@ -144,17 +144,11 @@
die.pause();
die.play();
}
var storedHighscore = 0;
localStorage.setItem("highscore", highscore);

$(".currentScore").text("Your score: " + score);
if(score > highscore) {
highscore ++;
if (score > storedHighscore && localStorage.highscore !== undefined) {
localStorage.highscore = storedHighscore;
} else if(score > storedHighscore && localStorage.highscore === undefined){
storedHighscore++;
localStorage.highscore = storedHighscore;
}
highscore = parseInt(score);
localStorage.setItem("highscore", highscore);
}
$("#highScore").text(highscore);

Expand Down Expand Up @@ -221,4 +215,4 @@
direction = "down";
}
});
// });
});

0 comments on commit b760429

Please sign in to comment.