Add sound effects for correct and wrong answers in FlagQuiz and include sound settings

This commit is contained in:
sHa
2025-08-11 18:23:08 +03:00
parent 41e3785202
commit 4dd1784e00

View File

@@ -46,6 +46,7 @@
let showResetConfirmation = false; let showResetConfirmation = false;
let focusWrongAnswers = false; let focusWrongAnswers = false;
let reduceCorrectAnswers = false; let reduceCorrectAnswers = false;
let soundEnabled = true;
// Theme // Theme
let theme = 'system'; let theme = 'system';
@@ -63,7 +64,7 @@
// Save settings when they change (after initial load) // Save settings when they change (after initial load)
$: if (settingsLoaded && typeof reduceCorrectAnswers !== 'undefined') { $: if (settingsLoaded && typeof reduceCorrectAnswers !== 'undefined') {
localStorage.setItem('flagQuizSettings', JSON.stringify({ autoAdvance, focusWrongAnswers, reduceCorrectAnswers })); localStorage.setItem('flagQuizSettings', JSON.stringify({ autoAdvance, focusWrongAnswers, reduceCorrectAnswers, soundEnabled }));
} }
// Load game stats from localStorage // Load game stats from localStorage
@@ -129,6 +130,7 @@
autoAdvance = settings.autoAdvance !== undefined ? settings.autoAdvance : true; autoAdvance = settings.autoAdvance !== undefined ? settings.autoAdvance : true;
focusWrongAnswers = settings.focusWrongAnswers !== undefined ? settings.focusWrongAnswers : false; focusWrongAnswers = settings.focusWrongAnswers !== undefined ? settings.focusWrongAnswers : false;
reduceCorrectAnswers = settings.reduceCorrectAnswers !== undefined ? settings.reduceCorrectAnswers : false; reduceCorrectAnswers = settings.reduceCorrectAnswers !== undefined ? settings.reduceCorrectAnswers : false;
soundEnabled = settings.soundEnabled !== undefined ? settings.soundEnabled : true;
} catch (e) { } catch (e) {
console.error('Error loading settings:', e); console.error('Error loading settings:', e);
} }
@@ -289,6 +291,9 @@
gameStats.correct++; gameStats.correct++;
currentStreak++; currentStreak++;
// Play correct sound
playCorrectSound();
// Track correct answer for this flag // Track correct answer for this flag
if (currentQuestion.correct?.name) { if (currentQuestion.correct?.name) {
const flagName = currentQuestion.correct.name; const flagName = currentQuestion.correct.name;
@@ -315,6 +320,9 @@
gameStats.wrong++; gameStats.wrong++;
currentStreak = 0; // Reset streak on wrong answer currentStreak = 0; // Reset streak on wrong answer
// Play wrong sound
playWrongSound();
// Track wrong answer for this flag // Track wrong answer for this flag
if (currentQuestion.correct?.name) { if (currentQuestion.correct?.name) {
const flagName = currentQuestion.correct.name; const flagName = currentQuestion.correct.name;
@@ -456,6 +464,58 @@
function handleAchievementsUnlocked() { function handleAchievementsUnlocked() {
updateAchievementCount(); updateAchievementCount();
} }
// Sound functions
function playCorrectSound() {
if (!soundEnabled) return;
try {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
// Pleasant ascending tone for correct answer
oscillator.frequency.setValueAtTime(523.25, audioContext.currentTime); // C5
oscillator.frequency.setValueAtTime(659.25, audioContext.currentTime + 0.1); // E5
oscillator.frequency.setValueAtTime(783.99, audioContext.currentTime + 0.2); // G5
gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.4);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.4);
} catch (e) {
console.log('Audio not supported:', e);
}
}
function playWrongSound() {
if (!soundEnabled) return;
try {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
// Descending tone for wrong answer
oscillator.frequency.setValueAtTime(400, audioContext.currentTime); // Lower frequency
oscillator.frequency.setValueAtTime(300, audioContext.currentTime + 0.15);
gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);
gainNode.gain.exponentialRampToValueAtTime(0.001, audioContext.currentTime + 0.3);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.3);
} catch (e) {
console.log('Audio not supported:', e);
}
}
</script> </script>
<svelte:head> <svelte:head>
@@ -528,6 +588,16 @@
</label> </label>
</div> </div>
<div class="setting-item">
<label>
<input
type="checkbox"
bind:checked={soundEnabled}
/>
Enable sound effects for answers
</label>
</div>
<div class="setting-actions"> <div class="setting-actions">
<button class="reset-stats-btn" on:click={resetAllStats}> <button class="reset-stats-btn" on:click={resetAllStats}>
Reset All Statistics Reset All Statistics