sound effect

This commit is contained in:
Frank DeMarco 2018-05-21 19:46:28 -04:00
parent c6e2f85edd
commit e98e6aeffc
3 changed files with 26 additions and 2 deletions

2
README
View File

@ -32,7 +32,7 @@ class SampleGame(Game):
if __name__ == '__main__':
SampleGame().play()
SampleGame().run()
License

View File

@ -48,3 +48,27 @@ class Audio(GameChild):
if sound not in self.original_volumes.keys():
self.original_volumes[sound] = sound.get_volume()
sound.set_volume(self.original_volumes[sound] * self.volume)
class SoundEffect(GameChild, Sound):
def __init__(self, parent, path, volume=1.0):
GameChild.__init__(self, parent)
Sound.__init__(self, path)
self.display_surface = self.get_display_surface()
self.initial_volume = volume
self.set_volume(volume)
def play(self, loops=0, maxtime=0, fade_ms=0, position=None, x=None):
self.set_volume(self.initial_volume *
self.get_configuration("audio", "sfx-volume"))
channel = Sound.play(self, loops, maxtime, fade_ms)
if x is not None:
position = float(x) / self.display_surface.get_width()
if position is not None and channel is not None:
channel.set_volume(*self.get_panning(position))
return channel
def get_panning(self, position):
return 1 - max(0, ((position - .5) * 2)), \
1 + min(0, ((position - .5) * 2))

View File

@ -21,4 +21,4 @@ class SampleGame(Game):
if __name__ == '__main__':
# the play method begins the project's animation
SampleGame().play()
SampleGame().run()