File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed
Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff 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 ( / \b s n a k e - s n a k e b o d y - a l i v e \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 ( ) ;
You can’t perform that action at this time.
0 commit comments