title: How to put different music for different maps in pygame?
body: <p>i'm coding a game in python with pygame and i would like some help to be able to implement music (with my sound_manager function which comes from my sound class) in my game. the principle would be to put one music per map , and therefore to save the music as an element of the map (like the walls or the objects)</p>
  <p>at the beginning I wanted to create a music variable in my map dataclass to be able to change it each time the map is created, but I'm a bit lost :/</p>
  <p>here is my map class ( which allows me to create the maps) :</p>
  <pre><code>from dataclasses import dataclass
  
  import pygame, pytmx, pyscroll
  from Son import Son
  
  @dataclass
  class Portal:
      from_world: str
      origin_point: str
      target_world: str
      teleport_point: str
  
  
  @dataclass
  class Map:
      name: str
      walls: list[pygame.Rect]
      group: pyscroll.PyscrollGroup
      tmx_data: pytmx.TiledMap
      portail: list[Portal]
  
  
  
  class MapManager:
      def __init__(self, screen, player):
  
          self.sound_manager = Son()
          self.maps = dict()  
          self.screen = screen 
          self.player = player  
          self.current_map = &quot;CrapouliWolrd&quot;
  
          self.register_map(&quot;CrapouliWolrd&quot;, portail=[
              Portal(from_world=&quot;CrapouliWolrd&quot;, origin_point=&quot;enter_house&quot;, target_world=&quot;Maison_perso&quot;, teleport_point=&quot;spawn_house&quot; )
          ])
          self.register_map(&quot;Maison_perso&quot;, portail=[
              Portal(from_world=&quot;Maison_perso&quot;, origin_point=&quot;exit_house&quot;, target_world=&quot;CrapouliWolrd&quot;, teleport_point=&quot;spawn_sortie&quot; )
          ])
  
          self.teleport_player(&quot;Player&quot;)
  
      def check_collisions(self):
         
          for Portal in self.get_map().portail:
              if Portal.from_world == self.current_map:
                  point = self.get_object(Portal.origin_point)
                  rect = pygame.Rect(point.x, point.y, point.width, point.height)
  
                  if self.player.feet.colliderect(rect):
                      copy_portal = Portal
                      self.current_map = Portal.target_world
                      self.teleport_player(copy_portal.teleport_point)
  
  
         
          for sprite in self.get_group().sprites():
              if sprite.feet.collidelist(self.get_walls()) &gt; -1:
                  sprite.move_back()
      def teleport_player(self, name):
          point = self.get_object(name)
          self.player.position[0] = point.x
          self.player.position[1] = point.y
          self.player.save_location()
  
      def register_map(self, name, portail=[]):
  
          tmx_data = pytmx.util_pygame.load_pygame(
              f&quot;../Map/{name}.tmx&quot;)  # PERMET DOUVRIR LA MAP SUIVANT SON NOM ! grace a f&quot;&quot;
          map_data = pyscroll.data.TiledMapData(tmx_data)
          map_layer = pyscroll.orthographic.BufferedRenderer(map_data, self.screen.get_size())
          map_layer.zoom = 3
  
          self.sound_manager.play('MusiqueMaison')
  
  
  
         
          walls = []
  
          for obj in tmx_data.objects:
              if obj.type == &quot;colision&quot;:
                  walls.append(pygame.Rect(obj.x, obj.y, obj.width, obj.height))
  
         
          group = pyscroll.PyscrollGroup(map_layer=map_layer, default_layer=6)
          group.add(self.player)  # ici on instancie le joueur dans les calques
  
         
  
          self.maps[name] = Map(name, walls, group, tmx_data, portail,)
  
      def get_map(self): return self.maps[self.current_map]      
  
      def get_group(self): return self.get_map().group         
      def get_walls(self): return self.get_map().walls        
  
      def get_object(self, name): return self.get_map().tmx_data.get_object_by_name(name)
  
                                                                                  # permet de rentrer simplement get_object pour faire allusion a un objet du decors
  
      def draw(self):
          self.get_group().draw(self.screen)
          self.get_group().center(self.player.rect.center)
  
  
  
  
  
      def update(self):
          self.get_group().update()
          self.check_collisions()
  </code></pre>
  <p>and here is my sound class with music methode</p>
  <pre><code>import pygame
  
  
  class Son:
      def __init__(self):
          self.sounds = {
              'MusiqueMaison': pygame.mixer.Sound(&quot;../music/mercantile-the-legend-of-zelda-the-wind-waker-hd-ost.mp3&quot;),
              'MusiquePlaine': pygame.mixer.Sound(&quot;../music/scandinavianz-nature-7504.mp3&quot;)
  
          }
  
      def play(self, name):
          self.sounds[name].play()
  </code></pre>