# HG changeset patch # User David Fraser # Date 1251895828 0 # Node ID f5d56688943be7f987cd39773c836b8b7797c626 # Parent 725b292ca07ba5dcf5bcfba24dc7410a2bbddaef Added background music support, and daytime and night time music (really animal sounds, not music) diff -r 725b292ca07b -r f5d56688943b data/sounds/daytime.ogg Binary file data/sounds/daytime.ogg has changed diff -r 725b292ca07b -r f5d56688943b data/sounds/nighttime.ogg Binary file data/sounds/nighttime.ogg has changed diff -r 725b292ca07b -r f5d56688943b data/sounds/sources.txt --- a/data/sounds/sources.txt Wed Sep 02 12:14:24 2009 +0000 +++ b/data/sounds/sources.txt Wed Sep 02 12:50:28 2009 +0000 @@ -39,6 +39,14 @@ ArchiveExtension: zip ArchiveMember: ColetteBoulanger44/rooster.wav +[daytime.ogg] +URL: http://www.archive.org/download/Berklee44v6/Berklee44v6.zip +Source: http://www.archive.org/details/Berklee44v6 +License: http://creativecommons.org/licenses/by/3.0/ +Credit: Sound samples by Berklee recorded for Richard Boulanger for use in the One Laptop per Child music library. See http://wiki.laptop.org/go/Sound_samples for details. +ArchiveExtension: zip +ArchiveMember: Berklee44v6/animal_noises1.wav + [nightfall.ogg] URL: http://www.archive.org/download/Berklee44v6/Berklee44v6.zip Source: http://www.archive.org/details/Berklee44v6 @@ -48,6 +56,14 @@ ArchiveMember: Berklee44v6/dog1.wav OriginalFormat: mplayer +[nighttime.ogg] +URL: http://www.archive.org/download/Berklee44v6/Berklee44v6.zip +Source: http://www.archive.org/details/Berklee44v6 +License: http://creativecommons.org/licenses/by/3.0/ +Credit: Sound samples by Berklee recorded for Richard Boulanger for use in the One Laptop per Child music library. See http://wiki.laptop.org/go/Sound_samples for details. +ArchiveExtension: zip +ArchiveMember: Berklee44v6/fakejungle1.wav + [kill-chicken.ogg] URL: http://www.archive.org/download/Berklee44v6/Berklee44v6.zip Source: http://www.archive.org/details/Berklee44v6 diff -r 725b292ca07b -r f5d56688943b gamelib/engine.py --- a/gamelib/engine.py Wed Sep 02 12:14:24 2009 +0000 +++ b/gamelib/engine.py Wed Sep 02 12:50:28 2009 +0000 @@ -45,6 +45,7 @@ class DayState(State): def init(self): """Add some chickens to the farm""" + sound.stop_background_music() self.game.gameboard.tv.sun(True) sound.play_sound("daybreak.ogg") @@ -52,6 +53,7 @@ pygame.time.set_timer(MOVE_FOX_ID, 0) self.game.gameboard.clear_foxes() self.game.gameboard.update_chickens() + sound.background_music("daytime.ogg") def event(self, e): if events_equal(e, START_NIGHT): @@ -79,6 +81,7 @@ class NightState(State): def init(self): """Add some foxes to the farm""" + sound.stop_background_music() self.game.gameboard.tv.sun(False) sound.play_sound("nightfall.ogg") @@ -86,6 +89,7 @@ self.cycle_count = 0 pygame.time.set_timer(MOVE_FOX_ID, 200) self.game.gameboard.spawn_foxes() + sound.background_music("nighttime.ogg") def event(self, e): if events_equal(e, START_DAY): diff -r 725b292ca07b -r f5d56688943b gamelib/equipment.py --- a/gamelib/equipment.py Wed Sep 02 12:14:24 2009 +0000 +++ b/gamelib/equipment.py Wed Sep 02 12:50:28 2009 +0000 @@ -1,6 +1,7 @@ """Stuff for animals to use.""" import random +import sound class Equipment(object): is_weapon = False @@ -24,5 +25,6 @@ def hit(self, gameboard, wielder, target): """Closer is more accurate.""" + sound.play_sound("fire-rifle.ogg") return random.randint(1, 100) > 60 + 10*wielder.pos.dist(target.pos) diff -r 725b292ca07b -r f5d56688943b gamelib/sound.py --- a/gamelib/sound.py Wed Sep 02 12:14:24 2009 +0000 +++ b/gamelib/sound.py Wed Sep 02 12:50:28 2009 +0000 @@ -29,4 +29,28 @@ SOUND_CACHE[file_path] = sound = pygame.mixer.Sound(file_path) sound.play() +CURRENT_MUSIC_FILE = None +def stop_background_music(): + """stops any playing background music""" + global CURRENT_MUSIC_FILE + CURRENT_MUSIC_FILE = None + # TODO: fadeout in a background thread + pygame.mixer.music.stop() + +def background_music(filename): + """plays the background music with the given filename from the data sounds directory""" + global CURRENT_MUSIC_FILE + if not SOUND_INITIALIZED: + return + file_path = data.filepath("sounds", filename) + if CURRENT_MUSIC_FILE == file_path: + return + stop_background_music() + if not os.path.exists(file_path): + return + CURRENT_MUSIC_FILE = file_path + pygame.mixer.music.load(file_path) + pygame.mixer.music.set_volume(0.3) + pygame.mixer.music.play(-1) +