Skip to content

Commit 54ce445

Browse files
committed
quitting while paused bug fixed
1 parent 2d5da5e commit 54ce445

File tree

6 files changed

+83
-33
lines changed

6 files changed

+83
-33
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115

116116

117117
<p id="start-countdown" hidden>3</p>
118+
<p id="pause-div" hidden>3</p>
118119

119120

120121
<!-- character area -->

src/invader.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class Invader {
99

1010

1111
//add the initial values of position and dimensions
12-
this.width = 37
13-
this.height = 76
12+
this.width = 23.6
13+
this.height = 49
1414
this.x = 941 + randomX
15-
this.y = 497
15+
this.y = 523
1616

1717
//adjust the node with initial values
1818
this.node.style.width = `${this.width}px`
@@ -37,21 +37,24 @@ class Invader {
3737
this.node.style.top = `${this.y}px`
3838

3939

40-
this.width = this.width + (this.width * (0.2 / 100))
41-
this.height = this.height + (this.height * (0.2 / 100))
40+
this.width = this.width + (this.width * (0.4 / 100))
41+
this.height = this.height + (this.height * (0.4 / 100))
4242

4343
this.node.style.width = `${this.width}px`
4444
this.node.style.height = `${this.height}px`
4545
}
4646

4747
die(){
48+
this.node.src = "./assets/img/enemy-explosion.png"
49+
this.width = this.height
50+
this.node.style.width = `${this.width}px`
51+
4852
playSfx(this.dieFx)
4953
currentScore = currentScore + 5
5054
let formattedCurrentScore = currentScore.toString().padStart(6, '0')
5155
gbCurrentScoreNode.innerHTML = formattedCurrentScore
52-
this.node.src = "../assets/img/ground-alien.png"
5356
setTimeout(() => {
5457
this.node.remove()
55-
}, 1000)
58+
}, 2000)
5659
}
5760
}

src/light-ball.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class LightBall {
2121
this.node.style.left = `${this.x}px`
2222

2323
//movements
24-
this.speed = 1
24+
this.speed = 3
2525

2626
//life
2727
}
2828

2929
automaticMovement(){
3030
this.y -= this.speed
3131
this.node.style.top = `${this.y}px`
32-
this.width = this.width - (this.width * (0.2 / 100))
33-
this.height = this.height - (this.height * (0.2 / 100))
32+
this.width = this.width - (this.width * (1 / 100))
33+
this.height = this.height - (this.height * (1 / 100))
3434

3535
this.node.style.width = `${this.width}px`
3636
this.node.style.height = `${this.height}px`

src/main.js

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const gbHighScoreNode = document.querySelector('#gb-high-score')
2323
const baseLifeNode = document.querySelector('#base-inner-bar')
2424

2525
const 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
111112
let countDownFX = new Audio('../assets/audio/fx/game-countdown.mp3')
113+
let lightBallSound = new Audio('../assets/audio/fx/light-ball-sound.mp3')
112114
let gameOverMusic = new Audio('../assets/audio/fx/game-over-music.mp3')
113115
let gameOverVoice = new Audio('../assets/audio/fx/game-over-voice.mp3')
114116

@@ -161,7 +163,7 @@ function stopAllMusic() {
161163
function 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+
179186
function showGameBox() {
180187
stopAllMusic()
181188
playMusic(gameBoxMusic)
@@ -196,6 +203,8 @@ function showGameBox() {
196203

197204
function 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

225235
function 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

239252
function 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

285299
function 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

303317
function 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

327348
function 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-
388412
document.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

405431
document.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

452482
quitConfirmYes.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

460492
toStartMenuBtn.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')

src/prof.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ class Prof {
2020
this.node.style.left = `${this.x}px`
2121

2222
//movements
23-
this.moveSpeed = 10
23+
this.moveSpeed = 12
2424
}
2525
}

styles/style.css

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@
397397
background-position: center;
398398
background-repeat: no-repeat;
399399
position: relative;
400-
display: none;
400+
display: block;
401401
}
402402

403403
/* music switch */
@@ -642,6 +642,17 @@
642642
text-align: center;
643643
}
644644

645+
#pause-div {
646+
position: absolute;
647+
width: 1920px;
648+
height: 230px;
649+
left: 0;
650+
top: 367px;
651+
font-size: 256px;
652+
color: white;
653+
text-align: center;
654+
}
655+
645656
/* quit confirmation */
646657

647658
#quit-confirm {

0 commit comments

Comments
 (0)