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

This commit is contained in:
Andrew Gillett 2022-01-25 14:45:41 +00:00
parent 839adfc134
commit 6b2c17488b
2 changed files with 19 additions and 10 deletions

View File

@ -350,10 +350,12 @@ class Game:
image = "digit" + colour + str(score[i]) image = "digit" + colour + str(score[i])
screen.blit(image, (255 + (160 * p) + (i * 55), 46)) 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 # 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 # 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()' # 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 # This automatically loads and plays a file named 'explosion.wav' (or .ogg) from the sounds folder (if
# such a file exists) # 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 # to access an attribute of Pygame Zero's sounds object, we must use Python's built-in function getattr
try: try:
getattr(sounds, name + str(random.randint(0, count - 1))).play() getattr(sounds, name + str(random.randint(0, count - 1))).play()
except: except Exception as e:
pass pass
def p1_controls(): def p1_controls():
@ -416,10 +418,10 @@ def update():
else: else:
# Detect up/down keys # Detect up/down keys
if num_players == 2 and keyboard.up: if num_players == 2 and keyboard.up:
sounds.up.play() game.play_sound("up", menu_sound=True)
num_players = 1 num_players = 1
elif num_players == 1 and keyboard.down: elif num_players == 1 and keyboard.down:
sounds.down.play() game.play_sound("down", menu_sound=True)
num_players = 2 num_players = 2
# Update the 'attract mode' game in the background (two AIs playing each other) # Update the 'attract mode' game in the background (two AIs playing each other)
@ -459,7 +461,7 @@ try:
music.play("theme") music.play("theme")
music.set_volume(0.3) music.set_volume(0.3)
except: except Exception:
# If an error occurs (e.g. no sound device), just ignore it # If an error occurs (e.g. no sound device), just ignore it
pass pass

View File

@ -708,7 +708,8 @@ class Game:
# No players - we must be on the menu. Play title music. # No players - we must be on the menu. Play title music.
music.play("theme") music.play("theme")
sounds.crowd.stop() sounds.crowd.stop()
except: except Exception:
# Ignore sound errors
pass pass
self.score_timer = 0 self.score_timer = 0
@ -945,6 +946,7 @@ class Game:
try: try:
getattr(sounds, name+str(random.randint(0, c-1))).play() getattr(sounds, name+str(random.randint(0, c-1))).play()
except: except:
# Ignore sound errors
pass pass
@ -1039,7 +1041,11 @@ def update():
elif key_just_pressed(keys.UP): elif key_just_pressed(keys.UP):
selection_change = -1 selection_change = -1
if selection_change != 0: if selection_change != 0:
sounds.move.play() try:
sounds.move.play()
except Exception:
# Ignore sound errors
pass
if menu_state == MenuState.NUM_PLAYERS: if menu_state == MenuState.NUM_PLAYERS:
menu_num_players = 2 if menu_num_players == 1 else 1 menu_num_players = 2 if menu_num_players == 1 else 1
else: else:
@ -1098,11 +1104,12 @@ def draw():
img = "l" + str(i) + str(game.teams[i].score) img = "l" + str(i) + str(game.teams[i].score)
screen.blit(img, (HALF_WINDOW_W + 25 - 125 * i, 144)) screen.blit(img, (HALF_WINDOW_W + 25 - 125 * i, 144))
# Set up sound # Set up sound system
try: try:
pygame.mixer.quit() pygame.mixer.quit()
pygame.mixer.init(44100, -16, 2, 1024) pygame.mixer.init(44100, -16, 2, 1024)
except: except Exception:
# Ignore sound errors
pass pass
# Set the initial game state # Set the initial game state