Skip to content

Commit 266b39c

Browse files
committed
Sound effects
Added sound effects for start screen, eating and power up
1 parent 66410e1 commit 266b39c

5 files changed

Lines changed: 20 additions & 1 deletion

File tree

Games/Pac_Man_Game/index.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,17 @@
2828
<img src="./pixels/PacMan-title.png" id="startGameButton" alt="Start Game">
2929
</div>
3030

31+
<!-- Audio element for start screen sound -->
32+
<audio id="startSound" src="./sounds/game_start.wav"></audio>
33+
34+
<!-- Audio element for Pac-Man eating sound -->
35+
<audio id="eatSound" src="./sounds/pacman_eat.wav"></audio>
36+
37+
<!-- Audio element for power-up sound -->
38+
<audio id="powerUpSound" src="./sounds/power_up.wav"></audio>
39+
3140
<!-- Link to the JavaScript file -->
3241
<script src="script.js"></script>
3342
</body>
3443

35-
</html>
44+
</html>

Games/Pac_Man_Game/script.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
document.addEventListener('DOMContentLoaded', () => {
22
// Get the game board element
33
const gameBoard = document.getElementById('gameBoard');
4+
const startSound = document.getElementById('startSound'); // Start screen audio element
5+
const eatSound = document.getElementById('eatSound'); // Eat sound element
6+
const powerUpSound = document.getElementById('powerUpSound'); // Power-up sound element
47

58
// Initialize variables
69
const grid = []; // Array to hold the grid cells
@@ -124,6 +127,7 @@ document.addEventListener('DOMContentLoaded', () => {
124127
score += 10;
125128
totalPacDots--;
126129
document.getElementById('scoreValue').textContent = score;
130+
eatSound.play(); // Play the eat sound
127131
console.log('Score:', score);
128132

129133
checkForWin();
@@ -132,6 +136,8 @@ document.addEventListener('DOMContentLoaded', () => {
132136
score += 50;
133137
totalPowerPellets--;
134138
document.getElementById('scoreValue').textContent = score;
139+
eatSound.play(); // Play the eat sound
140+
powerUpSound.play(); // Play the power-up sound
135141
console.log('Score:', score);
136142

137143
activatePowerPellet();
@@ -159,6 +165,8 @@ document.addEventListener('DOMContentLoaded', () => {
159165
ghosts.forEach(ghost => ghost.unscare());
160166
// Restart the power-pellet spawn timer
161167
powerPelletSpawnTimer = setInterval(turnPacDotIntoPowerPellet, 30000);
168+
powerUpSound.pause(); // Stop the power-up sound
169+
powerUpSound.currentTime = 0; // Reset the power-up sound
162170
}, 10000); // Power pellet effect lasts for 10 seconds
163171
};
164172

@@ -462,4 +470,6 @@ document.getElementById('startGameButton').addEventListener('click', () => {
462470
document.getElementById('startGameOverlay').style.display = 'none'; // Hide the start overlay
463471
initializeGame(); // Initialize the game
464472
});
473+
// Play the start sound when the start overlay is displayed
474+
startSound.play();
465475
});
364 KB
Binary file not shown.
7.29 KB
Binary file not shown.
182 KB
Binary file not shown.

0 commit comments

Comments
 (0)