Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30f1217

Browse files
committed
Implement high-scores... again...
Implement high-scores on localStorage.
1 parent b4345ee commit 30f1217

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

js/snake.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,15 @@ SNAKE.Snake = SNAKE.Snake || (function() {
327327
* @method handleDeath
328328
*/
329329
me.handleDeath = function() {
330+
function recordScore () {
331+
var highScore = localStorage.jsSnakeHighScore;
332+
if (highScore == undefined) highScore = me.snakeLength;
333+
if (me.snakeLength > highScore) {
334+
alert('Congratulations! You have beaten your previous high score, which was ' + highScore + '.');
335+
localStorage.setItem('jsSnakeHighScore', me.snakeLength);
336+
}
337+
}
338+
recordScore();
330339
me.snakeHead.elm.style.zIndex = getNextHighestZIndex(me.snakeBody);
331340
me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-alive\b/,'')
332341
me.snakeHead.elm.className += " snake-snakebody-dead";
@@ -973,3 +982,10 @@ SNAKE.Board = SNAKE.Board || (function() {
973982

974983
}; // end return function
975984
})();
985+
function getHighScore () {
986+
document.getElementById('high-score').addEventListener('click', function () {
987+
if (localStorage.jsSnakeHighScore == undefined) alert('You have not played this game yet!');
988+
else
989+
alert('Your current high score is ' + localStorage.jsSnakeHighScore + '.'); });
990+
}
991+
getHighScore();

0 commit comments

Comments
 (0)