Boom – simple sound effects
Adding sound effects in Kivy is very simple. Here is the code for boom.py
, which is going to produce a sound every time a Shot or Missile is fired:
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 sound
every time a new Boom
instance is created.