sound_player.py 974 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import pygame
  2. import time
  3. from game.const import game as G
  4. try:
  5. pygame.mixer.init()
  6. except Exception as e:
  7. print(e)
  8. PATH = "res/sound"
  9. sounds_dic = {}
  10. def play(name):
  11. global sounds_dic
  12. last_time = sounds_dic.get(name)
  13. try:
  14. if not last_time:
  15. sound = pygame.mixer.Sound(G.GAME_PATH + "\\" + PATH + '/' + name + ".wav")
  16. sound.set_volume(G.SOUND_VOLUME)
  17. sound.play()
  18. else:
  19. if time.time() - last_time >= 0.1:
  20. sound = pygame.mixer.Sound(G.GAME_PATH + "\\" + PATH + '/' + name + ".wav")
  21. sound.set_volume(G.SOUND_VOLUME)
  22. sound.play()
  23. sounds_dic[name] = time.time()
  24. except Exception as e:
  25. print(e)
  26. def playWin(name):
  27. try:
  28. sound = pygame.mixer.Sound(G.GAME_PATH + "\\" + "res/music" + '/' + name + ".ogg")
  29. sound.set_volume(G.SOUND_VOLUME)
  30. sound.play()
  31. except Exception as e:
  32. print(e)