Boom – simple sound effects
Adding sound effects in Kivy is very simple. A Boom
instance will produce a sound when it is created, and this will happen every time a shot or missile is fired. Here is the code for boom.py
:
52. # File name: boom.py 53. from kivy.uix.image import Image 54. from kivy.core.audio import SoundLoader 55. 56. class Boom(Image): 57. sound = SoundLoader.load('boom.wav') 58. def boom(self, **kwargs): 59. self.__class__.sound.play() 60. super(Boom, self).__init__(**kwargs)
Reproducing a sound involves the use of two classes, Sound
and SoundLoader
(line 54). SoundLoader
loads an audio file (.wav
) and returns a Sound
instance (line 57) that we keep in the sound
reference (a static attribute of the Boom
class). We play a sound every time a new Boom
instance is created.