Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sound button added #539

Merged
merged 3 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions js/board.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
sound = false

class Board {
static ROWS; //number of rows
static COLUMNS; //number of columns
Expand Down Expand Up @@ -50,8 +52,14 @@ class Board {
//
addEdgeClickEventListener() {
this.uiRoot.addEventListener("click", (e) => {
let click = new Audio("../assets/sounds/click.mp3");
click.play();

let click = new Audio('../assets/sounds/click.mp3');
if (sound)
//If sound is true, it means the sound is currently on
{
click.play();
}

if (!this.isFillingAdjacentBoxes) {
if (e.target.classList.contains("edge")) {
let edgePosition = e.target.getAttribute("data-position");
Expand Down Expand Up @@ -206,8 +214,14 @@ class Board {
}

fillBoxes() {
let fill = new Audio("../assets/sounds/fill.mp3");
fill.play();

let fill = new Audio('../assets/sounds/fill.mp3');
if (sound)
//If sound is true, it means the sound is currently on
{
fill.play();
}

if (this.adjacentBoxesToFill.length != 0) {
setTimeout(() => {
const box = this.adjacentBoxesToFill.shift();
Expand All @@ -223,3 +237,16 @@ class Board {
}
}
}

document.addEventListener("DOMContentLoaded", () => {
const soundToggleBtn = document.getElementById("sound-toggle");
soundToggleBtn.addEventListener("click", () => {
if (soundToggleBtn.innerText === "Sound Off") {
soundToggleBtn.innerText = "Sound On";
sound = true
} else {
soundToggleBtn.innerText = "Sound Off";
sound = false
}
});
});
8 changes: 4 additions & 4 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ document.addEventListener("DOMContentLoaded", () => {
const video = document.getElementById("myVideo");
video.src = `/assets/videos/${storedTheme}.mp4`;

const soundToggleBtn = document.getElementById("sound-toggle");
soundToggleBtn.addEventListener("click", () => {
const musicToggleBtn = document.getElementById("music-toggle");
musicToggleBtn.addEventListener("click", () => {
if (bgMusic.paused) {
bgMusic.play();
soundToggleBtn.innerText = "Sound On";
musicToggleBtn.innerText = "Music On";
} else {
bgMusic.pause();
soundToggleBtn.innerText = "Sound Off";
musicToggleBtn.innerText = "Music Off";
}
});
});
Expand Down
15 changes: 13 additions & 2 deletions pages/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@
transition: background-color 0.3s ease;
}

.button-container #sound-toggle {
.button-container #sound-toggle{
background-color: rgb(12, 1, 1);
border-radius: 4px;
padding: 10px 20px;
}

.button-container #music-toggle{
background-color: rgb(12, 1, 1);
border-radius: 4px;
padding: 10px 20px;
Expand All @@ -68,7 +74,11 @@
background-color: rgb(69, 133, 254);
}

.button-container #sound-toggle:hover {
.button-container #sound-toggle:hover {
background-color: rgb(21, 39, 77);
}

.button-container #music-toggle:hover {
background-color: rgb(21, 39, 77);
}

Expand Down Expand Up @@ -199,6 +209,7 @@

<div class="button-container">
<button id="sound-toggle" title="Toggle Sound">Sound Off</button>
<button id="music-toggle" title="Toggle Music">Music Off</button>

<a href="../pages/game.html" id="reset-button" title="Reset Game">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
Expand Down