@@ -23,6 +23,7 @@ const gbHighScoreNode = document.querySelector('#gb-high-score')
2323const baseLifeNode = document . querySelector ( '#base-inner-bar' )
2424
2525const countdownNode = document . querySelector ( '#start-countdown' )
26+ const pauseDivNode = document . querySelector ( '#pause-div' )
2627
2728// top right
2829
@@ -109,6 +110,7 @@ let resultScreenMusic = new Audio('../assets/audio/music/end-game-music.mp3')
109110
110111// fx paths
111112let countDownFX = new Audio ( '../assets/audio/fx/game-countdown.mp3' )
113+ let lightBallSound = new Audio ( '../assets/audio/fx/light-ball-sound.mp3' )
112114let gameOverMusic = new Audio ( '../assets/audio/fx/game-over-music.mp3' )
113115let gameOverVoice = new Audio ( '../assets/audio/fx/game-over-voice.mp3' )
114116
@@ -161,7 +163,7 @@ function stopAllMusic() {
161163function showStartScreen ( ) {
162164 stopAllMusic ( )
163165
164- playMusic ( startScreenMusic )
166+ // playMusic(startScreenMusic)
165167
166168 clearInterval ( gameIntervalId )
167169 clearInterval ( invaderInternalId )
@@ -170,12 +172,17 @@ function showStartScreen() {
170172 gameBoxNode . style . display = "none"
171173 resultScreenNode . style . display = "none"
172174
175+ updateScore ( )
176+ }
177+
178+ function updateScore ( ) {
173179 let formattedHighScore = `${ highScore . toString ( ) . padStart ( 6 , '0' ) } `
174180 ssHighScoreNode . innerHTML = formattedHighScore
175181 let formattedLastScore = `${ lastScore . toString ( ) . padStart ( 6 , '0' ) } `
176182 ssLastScoreNode . innerHTML = formattedLastScore
177183}
178184
185+
179186function showGameBox ( ) {
180187 stopAllMusic ( )
181188 playMusic ( gameBoxMusic )
@@ -196,6 +203,8 @@ function showGameBox() {
196203
197204function countdownStart ( ) {
198205 setTimeout ( playSfx ( countDownFX ) , 1000 )
206+ pauseBtn . style . display = 'none'
207+ quitBtn . style . display = 'none'
199208 let count = 4
200209 countdownNode . style . display = 'block'
201210 const countInterval = setInterval ( ( ) => {
@@ -220,9 +229,13 @@ function gameLoop() {
220229
221230 checkCollisions ( invaderArr , lightBallArr )
222231 checkBaseLife ( )
232+ checkLightBallDespawn ( )
223233}
224234
225235function startGame ( ) {
236+ pauseBtn . style . display = 'inline-block'
237+ quitBtn . style . display = 'inline-block'
238+
226239 gameIntervalId = setInterval ( gameLoop , Math . floor ( 1000 / 60 ) )
227240 invaderInternalId = setInterval ( invaderSpawn , 2000 )
228241}
@@ -237,6 +250,7 @@ function invaderSpawn() {
237250}
238251
239252function createLightBall ( ) {
253+ playSfx ( lightBallSound )
240254 let profX = profObj . x
241255 let newLightBall = new LightBall ( profX )
242256 lightBallArr . push ( newLightBall )
@@ -284,7 +298,7 @@ function checkBaseLife() {
284298
285299function checkLightBallDespawn ( ) {
286300 lightBallArr . forEach ( ( lightBall , index ) => {
287- if ( lightBall . y < 500 ) {
301+ if ( lightBall . y < 552 ) {
288302 lightBall . node . remove ( )
289303 lightBallArr . splice ( index , 1 )
290304 }
@@ -301,6 +315,13 @@ function isNewHighScore() {
301315}
302316
303317function gameEnd ( ) {
318+ if ( isPause ) {
319+ isPause = false
320+ pauseDivNode . style . display = 'none'
321+ pauseDivNode . style . innerHTML = ''
322+ pauseBtn . innerHTML = 'pause'
323+ }
324+ quitConfirmDiv . style . display = 'none'
304325 playSfx ( gameOverMusic )
305326 setTimeout ( playSfx ( gameOverVoice ) , 3000 )
306327 countdownNode . style . display = 'block'
@@ -326,14 +347,19 @@ function showResultScreen() {
326347
327348function returnStartScreen ( ) {
328349 stopAllMusic ( )
329- invaderArr = [ ]
330- invader . forEach ( invader => {
350+ invaderArr . forEach ( invader => {
331351 invader . node . remove ( )
332352 } )
333- lightBallArr = [ ]
353+ invaderArr = [ ]
354+
355+
334356 lightBallArr . forEach ( lightBall => {
335357 lightBall . node . remove ( )
336358 } )
359+ lightBallArr = [ ]
360+
361+ profObj . node . remove ( )
362+
337363 lastScore = currentScore
338364 localStorage . setItem ( 'lastScore' , lastScore )
339365 currentScore = 0
@@ -383,29 +409,29 @@ gbMusicToggleNode.addEventListener('click', () => {
383409
384410// PROF ACTIONS
385411
386-
387-
388412document . addEventListener ( 'keydown' , e => {
389413 const key = e . key . toLowerCase ( )
390414
391415
392- if ( ( key === 'a' || key === 'arrowleft' ) && profObj . x > 0 ) {
393- profObj . x -= profObj . moveSpeed
394- profObj . node . style . left = `${ profObj . x } px`
395- } else if ( ( key === 'd' || key === 'arrowright' ) && profObj . x < gameBoxNode . offsetWidth - profObj . width ) {
396- profObj . x += profObj . moveSpeed
397- profObj . node . style . left = `${ profObj . x } px`
398- } else if ( ( key === 'w' || key === 'arrowup' ) ) {
399- profObj . node . src = "../assets/img/prof-hands-up.png"
400- createLightBall ( )
416+ if ( ! isPause ) {
417+ if ( ( key === 'a' || key === 'arrowleft' ) && profObj . x > 0 ) {
418+ profObj . x -= profObj . moveSpeed
419+ profObj . node . style . left = `${ profObj . x } px`
420+ } else if ( ( key === 'd' || key === 'arrowright' ) && profObj . x < gameBoxNode . offsetWidth - profObj . width ) {
421+ profObj . x += profObj . moveSpeed
422+ profObj . node . style . left = `${ profObj . x } px`
423+ } else if ( ( key === 'w' || key === 'arrowup' ) ) {
424+ profObj . node . src = "../assets/img/prof-hands-up.png"
425+ createLightBall ( )
426+ }
401427 }
402428}
403429)
404430
405431document . addEventListener ( 'keyup' , e => {
406432 const key = e . key . toLowerCase ( )
407433
408- if ( ( key === 'w' || key === 'arrowup' ) ) {
434+ if ( ( key === 'w' || key === 'arrowup' ) ) {
409435 profObj . node . src = "../assets/img/prof-stable.png"
410436 }
411437}
@@ -430,7 +456,11 @@ pauseBtn.addEventListener('click', () => {
430456 clearInterval ( gameIntervalId )
431457 clearInterval ( invaderInternalId )
432458 pauseBtn . innerHTML = 'resume'
459+ pauseDivNode . innerHTML = 'PAUSED'
460+ pauseDivNode . style . display = 'block'
433461 } else {
462+ pauseDivNode . style . display = 'none'
463+ pauseDivNode . style . innerHTML = ''
434464 gameBoxMusic . play ( )
435465 pauseBtn . innerHTML = 'pause'
436466 gameIntervalId = setInterval ( gameLoop , Math . floor ( 1000 / 60 ) )
@@ -451,10 +481,12 @@ quitBtn.addEventListener('click', () => {
451481
452482quitConfirmYes . addEventListener ( 'click' , gameEnd )
453483
454- quitConfirmNo . addEventListener ( 'click' , ( ) => {
484+ quitConfirmNo . addEventListener ( 'click' , ( ) => {
455485 quitConfirmDiv . style . display = 'none'
456- gameIntervalId = setInterval ( gameLoop , Math . floor ( 1000 / 60 ) )
457- invaderInternalId = setInterval ( invaderSpawn , 2000 )
486+ if ( ! isPause ) {
487+ gameIntervalId = setInterval ( gameLoop , Math . floor ( 1000 / 60 ) )
488+ invaderInternalId = setInterval ( invaderSpawn , 2000 )
489+ }
458490} )
459491
460492toStartMenuBtn . addEventListener ( 'click' , returnStartScreen )
@@ -463,7 +495,10 @@ toStartMenuBtn.addEventListener('click', returnStartScreen)
463495// *** APP FLOW ***
464496
465497// start screens
466- showStartScreen ( )
498+ window . addEventListener ( "DOMContentLoaded" , ( ) => {
499+ updateScore ( )
500+ } )
501+
467502
468503// -------------------------------------------------------------------------------------------------
469504// if (key === 'w' || key === 'arrowup')
0 commit comments