diff --git a/assets/audio/applause.wav b/assets/audio/applause.wav new file mode 100644 index 0000000..ab77e8f Binary files /dev/null and b/assets/audio/applause.wav differ diff --git a/js/app.js b/js/app.js index f29dbc3..0d88bb9 100644 --- a/js/app.js +++ b/js/app.js @@ -1,11 +1,11 @@ -// console.log('overall test!'); /*------------ Constants ------------*/ import { questionsImportCat1, questionsImportCat2, questionsImportCat3 } from "../js/data.js" const questionsCat1 = Object.values(questionsImportCat1); const questionsCat2 = Object.values(questionsImportCat2); const questionsCat3 = Object.values(questionsImportCat3); -// console.log(questionsCat1); +import * as gameAudio from "../js/audio.js" + /*------------ Variables ------------*/ let playerScore = 0 // can reset in init or render @@ -22,6 +22,7 @@ const question = document.getElementById("question-p") const answers = document.getElementById("answers-ul") const btnReset = document.getElementById("btn-reset") const rulesBtnDiv = document.getElementById("rules-btn-container") +const btnContainer = document.getElementById("buttons-container") // for playClick audio purposes only const playerScoreContainer = document.getElementById("player-score") let initialScoreState = playerScoreContainer.innerHTML = `score` const countdown = document.getElementById("countdown") // TIMER @@ -29,19 +30,19 @@ const countdown = document.getElementById("countdown") // TIMER /*--------- Event Listeners ---------*/ btnCategory1.addEventListener('click', renderQuestionCat1) -btnCategory1.addEventListener('click', startTimer) // TIMER +btnCategory1.addEventListener('click', startTimer) btnCategory2.addEventListener('click', renderQuestionCat2) -btnCategory2.addEventListener('click', startTimer) // TIMER +btnCategory2.addEventListener('click', startTimer) btnCategory3.addEventListener('click', renderQuestionCat3) -btnCategory3.addEventListener('click', startTimer) // TIMER +btnCategory3.addEventListener('click', startTimer) +btnContainer.addEventListener('click', gameAudio.playClick) btnReset.addEventListener('click', resetGame) /*------------ Functions ------------*/ - function startTimer() { // Reset the timer - timerSeconds = 5 + timerSeconds = 80 // Set up the interval to update the timer every second timerInterval = setInterval(updateTimer, 1000) } @@ -114,9 +115,11 @@ function playerChooseAnswer(evt){ let selectedAnsIdx = Array.from(answers.children).indexOf(evt.target); // this line answers.children from ChatGPT! if (selectedAnsIdx == currQuesCorrAnsIdx){ // console.log('correct answer selected'); + gameAudio.playDing() playerScore += 1 } else { playerScore += 0 + gameAudio.playPianoWrong() // console.log('incorrect answer selected'); } @@ -158,15 +161,22 @@ function roundOver(){ stopTimer() countdown.textContent = `` if (playerScore >= 5) { + gameAudio.playLevelSucceed() playerScoreContainer.innerHTML = `Your score is:
${ playerScore }
Perfect. You are an evolved human.` - } else if (playerScore < 5 && playerScore > 1) { + } else if (playerScore < 5 && playerScore > 0) { + gameAudio.playApplause() playerScoreContainer.innerHTML = `Your score is:
${ playerScore }
Nice work. Keep practicing (at life).` } else { + gameAudio.playLevelFail() playerScoreContainer.innerHTML = `Your score is:
${ playerScore }
No offense but you might be a bad person.` } } + + function resetGame(){ + gameAudio.stopAudio() + gameAudio.playClick() rulesBtnDiv.style.display = '' playerScoreContainer.innerHTML = initialScoreState; countdown.textContent = `` diff --git a/js/audio.js b/js/audio.js index fd3fc9d..df5ec46 100644 --- a/js/audio.js +++ b/js/audio.js @@ -1,3 +1,4 @@ +let applause = new Audio('../assets/audio/applause.wav') let click = new Audio('../assets/audio/click.wav') let ding = new Audio('../assets/audio/ding.wav') let sadTrombone = new Audio('../assets/audio/sadtrombone.wav') @@ -5,6 +6,11 @@ let pianoWrong = new Audio('../assets/audio/pianowrong.wav') let levelSuccess = new Audio('../assets/audio/levelsuccesstrimmed.mp3') let levelFail = new Audio('../assets/audio/levelfailtrimmed.mp3') + +function playApplause() { + applause.volume = 0.15 + applause.play() +} function playClick() { click.volume = 0.15 click.play() @@ -29,12 +35,20 @@ function playLevelFail() { levelFail.volume = 0.15 levelFail.play() } +function stopAudio() { + levelFail.pause() + levelFail.currentTime = 0 + levelSuccess.pause() + levelSuccess.currentTime = 0 +} export { + playApplause, playClick, playDing, playTrombone, playPianoWrong, playLevelSucceed, - playLevelFail + playLevelFail, + stopAudio }