Mercurial > boomslang
annotate gamelib/sound.py @ 146:1be149cc42db
Allow can to be picked up.
author | Simon Cross <hodgestar+bzr@gmail.com> |
---|---|
date | Tue, 24 Aug 2010 23:11:13 +0200 |
parents | 3b293e3b8829 |
children | d2f94f42edf3 |
rev | line source |
---|---|
107 | 1 # Sound management for Suspended Sentence |
2 | |
3 # This re-implements some of the albow.resource code to | |
4 # a) work around an annoying bugs | |
5 # b) add some missing functionality (disable_sound) | |
6 | |
111
18ffaaaa27e7
Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents:
107
diff
changeset
|
7 import os |
18ffaaaa27e7
Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents:
107
diff
changeset
|
8 |
123 | 9 import pygame |
107 | 10 from albow.resource import _resource_path, dummy_sound |
11 | |
12 sound_cache = {} | |
13 | |
14 def get_sound(*names): | |
15 if sound_cache is None: | |
16 return dummy_sound | |
17 path = _resource_path("sounds", names) | |
18 sound = sound_cache.get(path) | |
19 if not sound: | |
111
18ffaaaa27e7
Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents:
107
diff
changeset
|
20 if not os.path.isfile(path): |
18ffaaaa27e7
Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents:
107
diff
changeset
|
21 missing_sound("File does not exist", path) |
18ffaaaa27e7
Add explicit check for sound file existence (my pygame 1.9 returns a valid sound object even when the path does not exist).
Simon Cross <simon@simonx>
parents:
107
diff
changeset
|
22 return dummy_sound |
107 | 23 try: |
24 from pygame.mixer import Sound | |
25 except ImportError, e: | |
26 no_sound(e) | |
27 return dummy_sound | |
28 try: | |
29 sound = Sound(path) | |
30 except pygame.error, e: | |
31 missing_sound(e, path) | |
32 return dummy_sound | |
33 sound_cache[path] = sound | |
34 return sound | |
35 | |
36 | |
37 def no_sound(e): | |
38 global sound_cache | |
39 print "get_sound: %s" % e | |
40 print "get_sound: Sound not available, continuing without it" | |
41 sound_cache = None | |
42 | |
43 def disable_sound(): | |
44 global sound_cache | |
45 sound_cache = None | |
46 | |
47 def missing_sound(e, name): | |
48 print "albow.resource.get_sound: %s: %s" % (name, e) | |
49 |