import pygame import time from client_local.const import game as G from collections import defaultdict from datetime import timedelta try: pygame.mixer.init() except Exception as e: print(e) PATH = "res/sound" # 用于记录每种音效的上一次播放时间 last_play_time = defaultdict(lambda: -1) # 时间间隔阈值(毫秒) TIME_THRESHOLD = 100 def play(name): global last_play_time current_time = pygame.time.get_ticks() try: # 检查是否超过时间间隔阈值 if current_time - last_play_time[name] > TIME_THRESHOLD or name in {"bomb", "flame"}: sound = pygame.mixer.Sound(f"{PATH}/{name}.wav") sound.set_volume(G.SOUND_VOLUME) sound.play() last_play_time[name] = current_time except Exception as e: print(e) def playWin(name): try: sound = pygame.mixer.Sound(f"res/music/{name}.ogg") sound.set_volume(G.SOUND_VOLUME) sound.play() except Exception as e: print(e)