comparison 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
comparison
equal deleted inserted replaced
89:c0455e6c99f4 90:23a8b2e49e9f
1 import os
2 import pygame
3
4 import data
5 import constants
6
7 SOUND_INITIALIZED = False
8
9 def init_sound():
10 """initialize the sound system"""
11 global SOUND_INITIALIZED
12 try:
13 pygame.mixer.init(constants.FREQ, constants.BITSIZE, constants.CHANNELS, constants.BUFFER)
14 SOUND_INITIALIZED = True
15 except pygame.error, exc:
16 print >>sys.stderr, "Could not initialize sound system: %s" % exc
17
18 def play_sound(filename):
19 """plays the sound with the given filename from the data sounds directory"""
20 if not SOUND_INITIALIZED:
21 return
22 file_path = data.filepath("sounds", filename)
23 if not os.path.exists(file_path):
24 return
25 pygame.mixer.music.load(file_path)
26 pygame.mixer.music.play()
27
28