Skip to content

Commit 2cba8b0

Browse files
authored
Merge pull request #14 from NotStonee/testing
High score
2 parents 0bdd400 + 4f2231a commit 2cba8b0

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

script.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
requestAnimationFrame(update);
99
}
1010

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+
1124
const canvas = document.getElementById('game');
1225
const context = canvas.getContext('2d');
1326

@@ -94,15 +107,20 @@ const canvas = document.getElementById('game');
94107

95108
// game loop
96109
function loop() {
110+
if (score > highScore) {
111+
highScore = score;
112+
}
113+
// save high score to local storage
114+
localStorage.setItem('highScore', highScore);
97115
requestAnimationFrame(loop);
98116
context.clearRect(0,0,canvas.width,canvas.height);
99117

100118
text('Score: '+score, '30px Cosmic Sans MS',20,35,'white')
101119
text('Lives: '+lives, '30px Cosmic Sans MS',280,35,'white')
102120
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')
106124
}
107125
// move paddle by it's velocity
108126
paddle.x += paddle.dx;
@@ -171,6 +189,7 @@ const canvas = document.getElementById('game');
171189
// remove brick from the bricks array
172190
bricks.splice(i, 1);
173191
score++
192+
174193
// ball is above or below the brick, change y velocity
175194
// account for the balls speed since it will be inside the brick when it
176195
// collides

0 commit comments

Comments
 (0)