From e272da117756c538e3eed45127e4c09ac9dd2b90 Mon Sep 17 00:00:00 2001
From: Storm Dragon <stormdragon2976@gmail.com>
Date: Sun, 16 Mar 2025 17:13:02 -0400
Subject: [PATCH] learn_sounds needed updating too. Hopefully this attempt
 works.

---
 menu.py | 51 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 39 insertions(+), 12 deletions(-)

diff --git a/menu.py b/menu.py
index 8a3cbbb..bf2af20 100644
--- a/menu.py
+++ b/menu.py
@@ -260,6 +260,11 @@ def learn_sounds(sounds):
     - Play selected sounds
     - Return to menu with escape key
     
+    Excluded sounds:
+    - Files in folders named 'ambience' (at any level)
+    - Files in any directory starting with '.'
+    - Files starting with 'game-intro', 'music_menu', or '_'
+    
     Args:
         sounds (dict): Dictionary of available sound objects
         
@@ -271,18 +276,39 @@ def learn_sounds(sounds):
     
     currentIndex = 0
     
-    # Get list of available sounds, excluding special sounds
-    soundFiles = [f for f in listdir("sounds/")
-                   if isfile(join("sounds/", f))
-                   and (f.split('.')[1].lower() in ["ogg", "wav"])
-                   and (f.split('.')[0].lower() not in ["game-intro", "music_menu"])
-                   and (not f.lower().startswith("_"))]
+    # Get list of available sound keys (excluding special sounds)
+    soundKeys = []
     
-    # Sort the sound files alphabetically
-    soundFiles.sort()
+    # Define exclusion criteria
+    excludedPrefixes = ["game-intro", "music_menu", "_"]
+    excludedDirs = ["ambience", "."]
+    
+    # Process each sound key in the dictionary
+    for soundKey in sounds.keys():
+        # Skip if key has any excluded prefix
+        if any(soundKey.lower().startswith(prefix.lower()) for prefix in excludedPrefixes):
+            continue
+            
+        # Split key into path parts
+        parts = soundKey.split('/')
+        
+        # Skip if any part of the path is an excluded directory
+        if any(part.lower() == dirName.lower() or part.startswith('.') for part in parts for dirName in excludedDirs):
+            continue
+            
+        # This sound passed all exclusion criteria
+        soundKeys.append(soundKey)
+    
+    # Sort sound keys alphabetically
+    soundKeys.sort()
     
     # Total number of sound files
-    totalSounds = len(soundFiles)
+    totalSounds = len(soundKeys)
+    
+    # If no sounds found, inform the user and return
+    if totalSounds == 0:
+        speech.speak("No sounds available to learn.")
+        return "menu"
     
     # Track last spoken index to avoid repetition
     lastSpoken = -1
@@ -293,7 +319,7 @@ def learn_sounds(sounds):
     while not returnToMenu:
         if currentIndex != lastSpoken:
             # Speak the sound name followed by its position in the list
-            speech.speak(f"{soundFiles[currentIndex][:-4]}, {currentIndex + 1} of {totalSounds}")
+            speech.speak(f"{soundKeys[currentIndex]}, {currentIndex + 1} of {totalSounds}")
             lastSpoken = currentIndex
             
         event = pygame.event.wait()
@@ -301,7 +327,7 @@ def learn_sounds(sounds):
             if event.key == pygame.K_ESCAPE:
                 returnToMenu = True
                 
-            if event.key in [pygame.K_DOWN, pygame.K_s] and currentIndex < len(soundFiles) - 1:
+            if event.key in [pygame.K_DOWN, pygame.K_s] and currentIndex < len(soundKeys) - 1:
                 pygame.mixer.stop()
                 currentIndex += 1
                 
@@ -311,7 +337,7 @@ def learn_sounds(sounds):
                 
             if event.key == pygame.K_RETURN:
                 try:
-                    soundName = soundFiles[currentIndex][:-4]
+                    soundName = soundKeys[currentIndex]
                     pygame.mixer.stop()
                     sounds[soundName].play()
                 except:
@@ -319,6 +345,7 @@ def learn_sounds(sounds):
                     speech.speak("Could not play sound.")
                     
         event = pygame.event.clear()
+        pygame.event.clear()
         time.sleep(0.001)
         
     return "menu"