# HG changeset patch # User Simon Cross # Date 1302387078 -7200 # Node ID 0ffb493a6fa4e1acefe180c60eca3c55aad21e74 # Parent 52b38b803782b02098bb3c8e9bb8b3907db8f863 Add support for sounds for breaking objects. diff -r 52b38b803782 -r 0ffb493a6fa4 skaapsteker/sound.py --- a/skaapsteker/sound.py Sun Apr 10 00:09:20 2011 +0200 +++ b/skaapsteker/sound.py Sun Apr 10 00:11:18 2011 +0200 @@ -5,9 +5,22 @@ from . import data from .constants import FREQ, BITSIZE, CHANNELS, BUFFER +_GLOBAL_SOUND = None + + +def load_sound(key, track_name, volume=None): + if _GLOBAL_SOUND is not None: + _GLOBAL_SOUND.load_sound(key, track_name, volume) + +def play_sound(key): + if _GLOBAL_SOUND is not None: + _GLOBAL_SOUND.play_sound(key) + + class SoundSystem(object): def __init__(self, want_sound): + global _GLOBAL_SOUND if want_sound: # See if we can actually enabled sound try: @@ -22,6 +35,7 @@ self.sound_enabled = False self._sounds = {} + _GLOBAL_SOUND = self def play_background_music(self, track_name, volume=1.0): @@ -37,7 +51,7 @@ if self.sound_enabled: mixer.music.stop() - def load_sound(self, key, track_name): + def load_sound(self, key, track_name, volume=None): if key in self._sounds: # First caller wins on duplicate keys return @@ -45,6 +59,8 @@ self._sounds[key] = None else: self._sounds[key] = pygame.mixer.Sound(data.filepath(track_name)) + if volume is not None: + self._sounds[key].set_volume(volume) def play_sound(self, key): sound = self._sounds.get(key, None) diff -r 52b38b803782 -r 0ffb493a6fa4 skaapsteker/sprites/items.py --- a/skaapsteker/sprites/items.py Sun Apr 10 00:09:20 2011 +0200 +++ b/skaapsteker/sprites/items.py Sun Apr 10 00:11:18 2011 +0200 @@ -1,4 +1,4 @@ -from .. import engine, data +from .. import engine, data, sound from .base import Item, PC_LAYER, PROJECTILE_LAYER import time @@ -12,13 +12,16 @@ class BreakableItem(Item): whole_image_file = None broken_image_file = None + breaking_sound = (None, None) collides_with = set([PC_LAYER, PROJECTILE_LAYER]) def setup(self, broken, **opts): super(BreakableItem, self).setup(**opts) self.broken = broken - + if self.breaking_sound[0] is not None: + track, volume = self.breaking_sound + sound.load_sound(track, track, volume) def setup_image_data(self, pos): self.image_file = self.broken_image_file if self._me.broken else self.whole_image_file @@ -29,6 +32,8 @@ self._me.broken = True self.broken = True self.setup_image_data(self._starting_tile_pos) + if self.breaking_sound[0] is not None: + sound.play_sound(self.breaking_sound[0]) def damage(self, damage): self.smash() @@ -215,6 +220,7 @@ class Vase(BreakableItem): whole_image_file = 'props/vase-whole.png' broken_image_file = 'props/vase-broken.png' + breaking_sound = ('sounds/vase breaking.ogg', 0.1) class Salmon(Item):