|
8 | 8 | requestAnimationFrame(update);
|
9 | 9 | }
|
10 | 10 |
|
| 11 | + let highScore = 0; |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +// retrieve high score from local storage |
| 16 | +const storedHighScore = localStorage.getItem('highScore'); |
| 17 | +if (storedHighScore) { |
| 18 | + highScore = storedHighScore; |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | + |
11 | 24 | const canvas = document.getElementById('game');
|
12 | 25 | const context = canvas.getContext('2d');
|
13 | 26 |
|
@@ -94,15 +107,20 @@ const canvas = document.getElementById('game');
|
94 | 107 |
|
95 | 108 | // game loop
|
96 | 109 | function loop() {
|
| 110 | + if (score > highScore) { |
| 111 | + highScore = score; |
| 112 | + } |
| 113 | + // save high score to local storage |
| 114 | + localStorage.setItem('highScore', highScore); |
97 | 115 | requestAnimationFrame(loop);
|
98 | 116 | context.clearRect(0,0,canvas.width,canvas.height);
|
99 | 117 |
|
100 | 118 | text('Score: '+score, '30px Cosmic Sans MS',20,35,'white')
|
101 | 119 | text('Lives: '+lives, '30px Cosmic Sans MS',280,35,'white')
|
102 | 120 | if (gameOver == true) {
|
103 |
| - text('Game Over', '30px Cosmic Sans MS', canvas.width/2-60,350,'white') |
104 |
| - text('Press R to restart', '18px Cosmic Sans MS', canvas.width/2-50,380,'white') |
105 |
| - |
| 121 | + text('Game Over', '30px Cosmic Sans MS', canvas.width/2-60,340,'white') |
| 122 | + text('High Score: '+highScore, '36px Cosmic Sans MS', canvas.width/2-90,300,'white') |
| 123 | + text('Press R to restart', '18px Cosmic Sans MS', canvas.width/2-50,365,'white') |
106 | 124 | }
|
107 | 125 | // move paddle by it's velocity
|
108 | 126 | paddle.x += paddle.dx;
|
@@ -171,6 +189,7 @@ const canvas = document.getElementById('game');
|
171 | 189 | // remove brick from the bricks array
|
172 | 190 | bricks.splice(i, 1);
|
173 | 191 | score++
|
| 192 | + |
174 | 193 | // ball is above or below the brick, change y velocity
|
175 | 194 | // account for the balls speed since it will be inside the brick when it
|
176 | 195 | // collides
|
|
0 commit comments