From 6b2c17488b259e1a6a51e100c7f116c644735f2b Mon Sep 17 00:00:00 2001 From: Andrew Gillett Date: Tue, 25 Jan 2022 14:45:41 +0000 Subject: [PATCH] Fix for issue where Pygame sometimes can't play Ogg Vorbis files - menu sounds in Boing and Soccer will no longer crash the game in that circumstance --- boing-master/boing.py | 14 ++++++++------ soccer-master/soccer.py | 15 +++++++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/boing-master/boing.py b/boing-master/boing.py index 1254673..ce9e6aa 100644 --- a/boing-master/boing.py +++ b/boing-master/boing.py @@ -350,10 +350,12 @@ class Game: image = "digit" + colour + str(score[i]) screen.blit(image, (255 + (160 * p) + (i * 55), 46)) - def play_sound(self, name, count=1): + def play_sound(self, name, count=1, menu_sound=False): # Some sounds have multiple varieties. If count > 1, we'll randomly choose one from those # We don't play any in-game sound effects if player 0 is an AI player - as this means we're on the menu - if self.bats[0].move_func != self.bats[0].ai: + # Updated Jan 2022 - some Pygame installations have issues playing ogg sound files. play_sound can skip sound + # errors without stopping the game, but it previously couldn't be used for menu-only sounds + if self.bats[0].move_func != self.bats[0].ai or menu_sound: # Pygame Zero allows you to write things like 'sounds.explosion.play()' # This automatically loads and plays a file named 'explosion.wav' (or .ogg) from the sounds folder (if # such a file exists) @@ -362,7 +364,7 @@ class Game: # to access an attribute of Pygame Zero's sounds object, we must use Python's built-in function getattr try: getattr(sounds, name + str(random.randint(0, count - 1))).play() - except: + except Exception as e: pass def p1_controls(): @@ -416,10 +418,10 @@ def update(): else: # Detect up/down keys if num_players == 2 and keyboard.up: - sounds.up.play() + game.play_sound("up", menu_sound=True) num_players = 1 elif num_players == 1 and keyboard.down: - sounds.down.play() + game.play_sound("down", menu_sound=True) num_players = 2 # Update the 'attract mode' game in the background (two AIs playing each other) @@ -459,7 +461,7 @@ try: music.play("theme") music.set_volume(0.3) -except: +except Exception: # If an error occurs (e.g. no sound device), just ignore it pass diff --git a/soccer-master/soccer.py b/soccer-master/soccer.py index 0e663bd..c823f77 100644 --- a/soccer-master/soccer.py +++ b/soccer-master/soccer.py @@ -708,7 +708,8 @@ class Game: # No players - we must be on the menu. Play title music. music.play("theme") sounds.crowd.stop() - except: + except Exception: + # Ignore sound errors pass self.score_timer = 0 @@ -945,6 +946,7 @@ class Game: try: getattr(sounds, name+str(random.randint(0, c-1))).play() except: + # Ignore sound errors pass @@ -1039,7 +1041,11 @@ def update(): elif key_just_pressed(keys.UP): selection_change = -1 if selection_change != 0: - sounds.move.play() + try: + sounds.move.play() + except Exception: + # Ignore sound errors + pass if menu_state == MenuState.NUM_PLAYERS: menu_num_players = 2 if menu_num_players == 1 else 1 else: @@ -1098,11 +1104,12 @@ def draw(): img = "l" + str(i) + str(game.teams[i].score) screen.blit(img, (HALF_WINDOW_W + 25 - 125 * i, 144)) -# Set up sound +# Set up sound system try: pygame.mixer.quit() pygame.mixer.init(44100, -16, 2, 1024) -except: +except Exception: + # Ignore sound errors pass # Set the initial game state