Skip to content

Commit

Permalink
finish all audio functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rbozek committed Jan 4, 2024
1 parent 03c06c8 commit e598f50
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
Binary file added assets/audio/applause.wav
Binary file not shown.
26 changes: 18 additions & 8 deletions js/app.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,26 +22,27 @@ 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


/*--------- 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)
}
Expand Down Expand Up @@ -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');
}

Expand Down Expand Up @@ -158,15 +161,22 @@ function roundOver(){
stopTimer()
countdown.textContent = ``
if (playerScore >= 5) {
gameAudio.playLevelSucceed()
playerScoreContainer.innerHTML = `Your score is:<br> ${ playerScore }<br>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:<br> ${ playerScore }<br>Nice work. Keep practicing (at life).`
} else {
gameAudio.playLevelFail()
playerScoreContainer.innerHTML = `Your score is:<br> ${ playerScore }<br>No offense but you might be a bad person.`
}
}



function resetGame(){
gameAudio.stopAudio()
gameAudio.playClick()
rulesBtnDiv.style.display = ''
playerScoreContainer.innerHTML = initialScoreState;
countdown.textContent = ``
Expand Down
16 changes: 15 additions & 1 deletion js/audio.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
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')
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()
Expand All @@ -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
}

0 comments on commit e598f50

Please sign in to comment.