Mercurial > rinkhals
diff gamelib/sound.py @ 90:23a8b2e49e9f
Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
author | David Fraser <davidf@sjsoft.com> |
---|---|
date | Wed, 02 Sep 2009 10:34:22 +0000 |
parents | |
children | 529a4d41c67a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gamelib/sound.py Wed Sep 02 10:34:22 2009 +0000 @@ -0,0 +1,28 @@ +import os +import pygame + +import data +import constants + +SOUND_INITIALIZED = False + +def init_sound(): + """initialize the sound system""" + global SOUND_INITIALIZED + try: + pygame.mixer.init(constants.FREQ, constants.BITSIZE, constants.CHANNELS, constants.BUFFER) + SOUND_INITIALIZED = True + except pygame.error, exc: + print >>sys.stderr, "Could not initialize sound system: %s" % exc + +def play_sound(filename): + """plays the sound with the given filename from the data sounds directory""" + if not SOUND_INITIALIZED: + return + file_path = data.filepath("sounds", filename) + if not os.path.exists(file_path): + return + pygame.mixer.music.load(file_path) + pygame.mixer.music.play() + +