music_loader.py 2.0 KB

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