Skip to content

Commit

Permalink
Fix the error that occurs on some computers without audio output devices
Browse files Browse the repository at this point in the history
  • Loading branch information
chyok committed May 17, 2024
1 parent ff1ebb6 commit 90a4369
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions ten_drops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pygame

from pygame.mixer import SoundType

from os.path import join, dirname
from dataclasses import dataclass

Expand All @@ -24,7 +26,13 @@
]

pygame.init()
pygame.mixer.init()

# To solve the error that occurs on some computers without audio output devices
_OPEN_SOUND = True
try:
pygame.mixer.init()
except Exception as e: # noqa
_OPEN_SOUND = False

pygame.display.set_caption("ten drops")

Expand All @@ -48,6 +56,16 @@ class Status:
static: Surface | None = None


class Sound:
def __init__(self, sound_file: str):
if _OPEN_SOUND:
self.sound = pygame.mixer.Sound(join(AudioFolderPath, sound_file))

def play(self):
if _OPEN_SOUND:
self.sound.play()


def get_drop_images() -> list[Status]:
drop_image_path = join(ImageFolderPath, "drop")
drop_image_paths = [join(drop_image_path, f"{i}.png") for i in range(134)]
Expand Down Expand Up @@ -108,6 +126,6 @@ def get_droplet_images() -> list[Status]:

DROPLET_IMAGES = get_droplet_images()

BREAK_SOUND = pygame.mixer.Sound(join(AudioFolderPath, "break.mp3"))
GROW_SOUND = pygame.mixer.Sound(join(AudioFolderPath, "grow.mp3"))
HP_SOUND = pygame.mixer.Sound(join(AudioFolderPath, "hp.mp3"))
BREAK_SOUND = Sound("break.mp3")
GROW_SOUND = Sound("grow.mp3")
HP_SOUND = Sound("hp.mp3")

0 comments on commit 90a4369

Please sign in to comment.