sound_player.py 810 B

1234567891011121314151617181920212223242526272829
  1. import pygame
  2. from client_local.const import game as G
  3. from collections import defaultdict
  4. from datetime import timedelta
  5. pygame.mixer.init()
  6. PATH = "res/sound"
  7. # 用于记录每种音效的上一次播放时间
  8. last_play_time = defaultdict(lambda: -1)
  9. # 时间间隔阈值(毫秒)
  10. TIME_THRESHOLD = 100
  11. def play(name):
  12. global last_play_time
  13. current_time = pygame.time.get_ticks()
  14. # 检查是否超过时间间隔阈值
  15. if current_time - last_play_time[name] > TIME_THRESHOLD:
  16. sound = pygame.mixer.Sound(PATH + '/' + name + ".wav")
  17. sound.set_volume(G.SOUND_VOLUME)
  18. sound.play()
  19. last_play_time[name] = current_time
  20. def playWin(name):
  21. sound = pygame.mixer.Sound("res/music" + '/' + name + ".ogg")
  22. sound.set_volume(G.SOUND_VOLUME)
  23. sound.play()