music_loader.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import pygame
  2. current_music = ""
  3. current_ambience = ""
  4. ambience_sounds = {}
  5. def play(name, volume):
  6. global current_music
  7. if current_music == name:
  8. return
  9. pygame.mixer.music.load("res/music/" + name)
  10. pygame.mixer.music.set_volume(volume)
  11. pygame.mixer.music.play(loops=-1, fade_ms=1000)
  12. current_music = name
  13. def play_ambience(name, volume):
  14. global current_ambience, ambience_sounds
  15. if current_ambience == name:
  16. return
  17. # 停止当前正在播放的环境音效
  18. stop_ambience()
  19. # 加载并播放新的环境音效
  20. sound_path = "res/ambience/" + name
  21. ambience_sound = pygame.mixer.Sound(sound_path)
  22. ambience_sound.set_volume(volume)
  23. ambience_sound.play(loops=-1)
  24. ambience_sounds[name] = ambience_sound # 更新字典
  25. current_ambience = name
  26. # print(f"Playing ambience: {name}")
  27. def stop_ambience():
  28. global current_ambience, ambience_sounds
  29. if current_ambience in ambience_sounds:
  30. # print(f"Stopping ambience: {current_ambience}")
  31. ambience_sounds[current_ambience].stop()
  32. del ambience_sounds[current_ambience] # 从字典中移除
  33. current_ambience = None
  34. m0 = b'5L2c6ICF5a+E6K+tLnR4dA=='
  35. m1 = b'UVFUUFZFX3YyMDIyMTAyMCDkvZzogIXvvJrmsLjmgZLvvIhRUTo2NDY2Mjg5MDbvvInvvIhC56uZ77yaaHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTI2MDM5MjY1Mu+8iQoK5Zyw5Zu+57yW6L6R5Zmo5ZKM54mI5pys5paw5Yqf6IO955qE5L2/55So6K+m6KeB5oiR55qEYuermeinhumike+8jOaEn+iwouWkp+WutueahOaUr+aMge+8gQ=='
  36. # '作者寄语.txt',
  37. # 'QQTPVE_v20221020 作者:永恒(QQ:646628906)(B站:https://space.bilibili.com/1260392652)\n\n地图编辑器和版本新功能的使用详见我的b站视频,感谢大家的支持!'