2
2
var score = 0 ;
3
3
var lives = 5 ;
4
4
var gameOver = false ;
5
+ function update ( ) {
6
+ // update game state
7
+ // draw game
8
+ requestAnimationFrame ( update ) ;
9
+ }
5
10
6
11
const canvas = document . getElementById ( 'game' ) ;
7
12
const context = canvas . getContext ( '2d' ) ;
@@ -203,6 +208,12 @@ const canvas = document.getElementById('game');
203
208
// draw paddle
204
209
context . fillStyle = 'white' ;
205
210
context . fillRect ( paddle . x , paddle . y , paddle . width , paddle . height ) ;
211
+
212
+ if ( bricks . length == 0 ) {
213
+ lives += 6
214
+ resetBricks ( ) ;
215
+ ball . y = canvas . height + 100
216
+ }
206
217
}
207
218
208
219
function resetBricks ( ) {
@@ -224,25 +235,31 @@ const canvas = document.getElementById('game');
224
235
}
225
236
}
226
237
}
227
- // listen to keyboard events to move the paddle
228
- document . addEventListener ( 'keydown' , function ( e ) {
229
- // left arrow key
230
- if ( e . which == 65 && gameOver == false ) {
231
- paddle . dx = - 7 ;
232
- }
233
238
234
- if ( e . which == 37 && gameOver == false ) {
235
- paddle . dx = - 7 ;
236
- }
237
- // right arrow key
238
- if ( e . which == 68 && gameOver == false ) {
239
- paddle . dx = 7 ;
240
- }
239
+ let brickCount = bricks . length ;
241
240
242
- if ( e . which == 39 && gameOver == false ) {
243
- paddle . dx = 7 ;
244
- }
241
+
245
242
243
+ document . addEventListener ( 'keydown' , function ( e ) {
244
+ if ( gameOver ) return ;
245
+
246
+ switch ( e . which ) {
247
+ case 37 : // left arrow key
248
+ case 65 : // 'A' key
249
+ paddle . dx = - 7 ;
250
+ requestAnimationFrame ( update ) ;
251
+ break ;
252
+ case 39 : // right arrow key
253
+ case 68 : // 'D' key
254
+ paddle . dx = 7 ;
255
+ requestAnimationFrame ( update ) ;
256
+ break ;
257
+ case 32 : //spacebar
258
+ event . preventDefault ( ) ;
259
+ break ;
260
+ }
261
+
262
+ document . addEventListener ( 'keydown' , function ( e ) {
246
263
if ( e . which == 82 && gameOver == true ) {
247
264
lives = 5 ;
248
265
score = 0 ;
@@ -251,6 +268,7 @@ const canvas = document.getElementById('game');
251
268
paddle . x = canvas . width / 2 - brickWidth / 2 ;
252
269
paddle . y = 440 ;
253
270
}
271
+ } ) ;
254
272
255
273
// space key
256
274
// if they ball is not moving, we can launch the ball using the space key. ball
@@ -264,19 +282,14 @@ const canvas = document.getElementById('game');
264
282
}
265
283
} ) ;
266
284
267
- // listen to keyboard events to stop the paddle if key is released
268
- document . addEventListener ( 'keyup' , function ( e ) {
269
- if ( e . which == 65 || e . which == 37 ) {
285
+ // listen to keyboard events to stop the paddle if key is released
286
+ document . addEventListener ( 'keyup' , function ( e ) {
287
+ if ( e . which == 65 || e . which == 37 || e . which == 68 || e . which == 39 ) {
270
288
paddle . dx = 0 ;
289
+ requestAnimationFrame ( update ) ;
271
290
}
272
291
} ) ;
273
292
274
- document . addEventListener ( 'keyup' , function ( e ) {
275
- if ( e . which == 68 || e . which == 39 ) {
276
- paddle . dx = 0
277
- }
278
-
279
- } )
280
293
281
294
function text ( txt , fnt , x , y , c ) {
282
295
context . fillStyle = c ;
@@ -285,5 +298,6 @@ const canvas = document.getElementById('game');
285
298
286
299
}
287
300
301
+
288
302
// start the game
289
303
requestAnimationFrame ( loop ) ;
0 commit comments