title: How to implement sound in pygame?
body: <p>I have a problem with programming the sound effects in Space Invaders wrote in python. Entire game is divided into modules like the main loop, game functions, settings etc. This is a part of the code that create new bullet and then adds it into the group. Function contains the sound effect:</p>
  
  <pre><code>def sound_effect(sound_file):
      pygame.mixer.init()
      pygame.mixer.Sound(sound_file)
      pygame.mixer.Sound(sound_file).play().set_volume(0.2)
  
  def fire_bullet(si_settings, screen, ship, bullets):
  """Fire a bullet, if limit not reached yet."""
      if len(bullets) &lt; si_settings.bullets_allowed:
          new_bullet = Bullet(si_settings, screen, ship)
          bullets.add(new_bullet)
          sound_effect('sounds/shoot.wav')`
  </code></pre>
  
  <p>It have some issues, the main problem is the optimization: every time the game use an sound effect, it have to open and load a file - this issue create time gap between event generating the sound and the effect. How can I optimize this and for example write a code that load all sound effects at the start of the game?</p>