Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 30f1217

Browse filesBrowse files
committed
Implement high-scores... again...
Implement high-scores on localStorage.
1 parent b4345ee commit 30f1217
Copy full SHA for 30f1217

File tree

Expand file treeCollapse file tree

1 file changed

+16
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-0
lines changed
Open diff view settings
Collapse file

‎js/snake.js‎

Copy file name to clipboardExpand all lines: js/snake.js
+16Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.