Mercurial > rinkhals
comparison gamelib/sound.py @ 99:f5d56688943b
Added background music support, and daytime and night time music (really animal sounds, not music)
author | David Fraser <davidf@sjsoft.com> |
---|---|
date | Wed, 02 Sep 2009 12:50:28 +0000 |
parents | 529a4d41c67a |
children | 41db9f3ba29e |
comparison
equal
deleted
inserted
replaced
98:725b292ca07b | 99:f5d56688943b |
---|---|
27 if not os.path.exists(file_path): | 27 if not os.path.exists(file_path): |
28 return | 28 return |
29 SOUND_CACHE[file_path] = sound = pygame.mixer.Sound(file_path) | 29 SOUND_CACHE[file_path] = sound = pygame.mixer.Sound(file_path) |
30 sound.play() | 30 sound.play() |
31 | 31 |
32 CURRENT_MUSIC_FILE = None | |
32 | 33 |
34 def stop_background_music(): | |
35 """stops any playing background music""" | |
36 global CURRENT_MUSIC_FILE | |
37 CURRENT_MUSIC_FILE = None | |
38 # TODO: fadeout in a background thread | |
39 pygame.mixer.music.stop() | |
40 | |
41 def background_music(filename): | |
42 """plays the background music with the given filename from the data sounds directory""" | |
43 global CURRENT_MUSIC_FILE | |
44 if not SOUND_INITIALIZED: | |
45 return | |
46 file_path = data.filepath("sounds", filename) | |
47 if CURRENT_MUSIC_FILE == file_path: | |
48 return | |
49 stop_background_music() | |
50 if not os.path.exists(file_path): | |
51 return | |
52 CURRENT_MUSIC_FILE = file_path | |
53 pygame.mixer.music.load(file_path) | |
54 pygame.mixer.music.set_volume(0.3) | |
55 pygame.mixer.music.play(-1) | |
56 |