music_loader.py 2.1 KB

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