Skip to content

Commit 3bf22b3

Browse files
committed
Duplications
Fixed some duplications
1 parent 5dd1720 commit 3bf22b3

1 file changed

Lines changed: 22 additions & 31 deletions

File tree

Games/Pac_Man_Game/script.js

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ document.addEventListener('DOMContentLoaded', () => {
196196
powerUpSound.play(); // Play the power-up sound
197197
console.log('Score:', score);
198198

199-
activatePowerPellet();
199+
activatePowerPelletEffect();
200200
}
201201

202202
checkCollision();
@@ -209,21 +209,12 @@ document.addEventListener('DOMContentLoaded', () => {
209209
}
210210
};
211211

212-
const activatePowerPellet = () => {
213-
powerPelletActive = true;
214-
clearTimeout(powerPelletTimer);
215-
clearInterval(powerPelletSpawnTimer); // Stop spawning new power-pellets during the effect
216-
217-
ghosts.forEach(ghost => ghost.scare());
218-
219-
powerPelletTimer = setTimeout(() => {
220-
powerPelletActive = false;
221-
ghosts.forEach(ghost => ghost.unscare());
222-
// Restart the power-pellet spawn timer
223-
powerPelletSpawnTimer = setInterval(turnPacDotIntoPowerPellet, 30000);
224-
powerUpSound.pause(); // Stop the power-up sound
225-
powerUpSound.currentTime = 0; // Reset the power-up sound
226-
}, 10000); // Power pellet effect lasts for 10 seconds
212+
const addRestartButtonListener = () => {
213+
// Event listener to the restart button
214+
document.getElementById('restartGameButton').addEventListener('click', () => {
215+
console.log('Restart button clicked');
216+
window.location.reload(); // Reload the page to restart the game
217+
});
227218
};
228219

229220
const checkForWin = () => {
@@ -250,11 +241,7 @@ document.addEventListener('DOMContentLoaded', () => {
250241
`;
251242
document.body.appendChild(gameOverOverlay);
252243

253-
// Event listener for restart button
254-
document.getElementById('restartGameButton').addEventListener('click', () => {
255-
console.log('Restart button clicked');
256-
window.location.reload(); // Reload the page
257-
});
244+
addRestartButtonListener();
258245
}
259246
};
260247

@@ -282,11 +269,7 @@ document.addEventListener('DOMContentLoaded', () => {
282269

283270
gameOverSound.play(); // Play the game over sound
284271

285-
// Event listener for restart button
286-
document.getElementById('restartGameButton').addEventListener('click', () => {
287-
console.log('Restart button clicked');
288-
window.location.reload(); // Reload the page
289-
});
272+
addRestartButtonListener();
290273
};
291274

292275
let direction = null;
@@ -533,19 +516,27 @@ document.addEventListener('DOMContentLoaded', () => {
533516

534517
// Function to make ghosts dark blue and slow
535518
const activatePowerPelletEffect = () => {
536-
powerPelletActive = true;
537-
clearTimeout(powerPelletTimer);
538-
clearInterval(powerPelletSpawnTimer); // Stop spawning new power-pellets during the effect
539-
519+
powerPelletActive = true; // Set the power-pellet flag to true
520+
clearTimeout(powerPelletTimer); // Stop the previous power-pellet timer
521+
clearInterval(powerPelletSpawnTimer); // Stop spawning power-pellets
522+
523+
// Scare the ghosts
540524
ghosts.forEach(ghost => ghost.scare());
541-
525+
526+
// After 10 seconds, deactivate the power-pellet effect
542527
powerPelletTimer = setTimeout(() => {
543528
powerPelletActive = false;
544529
ghosts.forEach(ghost => ghost.unscare());
530+
545531
// Restart the power-pellet spawn timer
546532
powerPelletSpawnTimer = setInterval(turnPacDotIntoPowerPellet, 30000);
533+
534+
// Stop the power-up sound
535+
powerUpSound.pause();
536+
powerUpSound.currentTime = 0;
547537
}, 10000); // Power pellet effect lasts for 10 seconds
548538
};
539+
549540

550541
// Event listener for Pac-Man eating a power-pellet
551542
grid.forEach(cell => {

0 commit comments

Comments
 (0)