changeset 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 725b292ca07b
children e90068d1f374
files data/sounds/daytime.ogg data/sounds/nighttime.ogg data/sounds/sources.txt gamelib/engine.py gamelib/equipment.py gamelib/sound.py
diffstat 6 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
Binary file data/sounds/daytime.ogg has changed
Binary file data/sounds/nighttime.ogg has changed
--- 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
--- 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):
--- 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)
 
--- 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)
+