class PlayMusic(): # 设置音乐播放标志:0表示自动播放,1表示手动介入 _MUSIC_PLAY_FLAG = 0 def __init__(self, setting): self.setting = setting self.logger = LoggerPrint(self.setting) self.log = self.logger.printLogToSystem() # 初始化混音器 self.mixer_init = pygame.mixer.init() self.log.info("初始化混音器成功!") # 播放背景音乐 def play_bg_music(self, music_file=None): # 如果music_file有值,则播放选中的音乐 if music_file: pygame.mixer.music.stop() self.log.info(f"切换的背景音乐为: {music_file}") # 没有值则随机播放 else: music_list = self.setting.music_list music_file = random.sample(music_list, 1) music_file = music_file[0] self.log.info(f"随机播放的音乐为: {music_file}") pygame.mixer.music.load(f'mids/{music_file}') pygame.mixer.music.play() PlayMusic._MUSIC_PLAY_FLAG = 0 self.is_not_busy() # 播放一首音乐完成之后,自动随机播放下一首 def is_not_busy(self): mixer_state = pygame.mixer.get_init() if mixer_state: # 如果播放器没有工作,则调用播放背景音乐功能方法 if pygame.mixer.music.get_busy( ) == False and PlayMusic._MUSIC_PLAY_FLAG == 0: self.play_bg_music() else: if PlayMusic._MUSIC_PLAY_FLAG == 0: # 定时器,没隔1s检查一次播放器是否有在播放 t = threading.Timer(1, self.is_not_busy) t.start() else: pass # 手动停止背景音乐播放 def stop_bg_music(self): mixer_state = pygame.mixer.get_init() if mixer_state: pygame.mixer.music.stop() self.log.info('背景音乐播放停止!') PlayMusic._MUSIC_PLAY_FLAG = 1 # 加载操作棋子时的音效 def load_play_sound(self, sound): soundobj = pygame.mixer.Sound(sound) soundobj.play() # 退出游戏的时候,需要先退出pygame和mixer播放器 def quit_music(self): pygame.mixer.music.fadeout(1000) pygame.mixer.quit() pygame.quit() self.log.info("混音器销毁成功!pygame退出!")
class Commmon(): def __init__(self, setting): self.setting = setting self.__logger = LoggerPrint(self.setting) self.log = self.__logger.printLogToSystem() self.times = 0 self.color1, self.color2 = 'red', 'black' # 获取当前系统的名称 def get_system_name(self): system_flag = 0 uname = platform.uname().system system_name = platform.platform().split('-')[0] if uname == 'Darwin' or system_name == 'Darwin': # mac系统加载mac配置,设置flag为1 system_flag = 1 elif uname == 'Windows' or system_name == 'Windows': # windows系统加载默认setting设置,设置flag为2 system_flag = 2 else: system_flag = 3 self.log.info(f"当前系统为:{uname}") return system_flag # 压缩图片,改变图片的大小 def change_img(self, img, width=100, height=100): if isinstance(img, str): piece1 = Image.open(img) piece2 = piece1.resize((width, height)) changed_img = ImageTk.PhotoImage(piece2) return changed_img elif isinstance(img, dict): img_dict = {} for key, value in img.items(): piece1 = Image.open(value) piece2 = piece1.resize((width, height)) changed_img = ImageTk.PhotoImage(piece2) img_dict[key] = changed_img return img_dict else: raise ImgNotFound('传入图片格式不正确或者图片不存在!') # 读取文件 def read_file(self, filename, flag=None): try: with open(filename, 'r', encoding='utf-8') as f: if flag is None: return f.read() elif flag == 'info': return f.readlines() except Exception: self.log.exception(f'读取{filename}文件异常!') # 写入info文件 def write_file(self, filename, write_value): try: if isinstance(write_value, str): with open(filename, 'ab+') as f: writestr = (write_value + os.linesep).encode('utf-8') f.write(writestr) elif isinstance(write_value, list): with open(filename, 'w+', encoding='utf-8') as f: f.writelines(write_value) except Exception: self.log.exception(f'写入{filename}文件异常!') # 获取当前系统时间,格式化后添加到文件名称中 def format_now_time(self): ntime = time.strftime('_%Y_%m_%d_%H_%M_%S') return ntime # 获取当前系统时间 def get_now_time(self): ntime = time.strftime('%Y-%m-%d %H:%M:%S') return ntime # 计算两个时间之间的时间差,返回str def how_long_time(self, begintime, endtime): howlongdays = (endtime - begintime).days howlongsecs = (endtime - begintime).seconds hours = int(howlongsecs / 3600) mins = int((howlongsecs % 3600) / 60) secs = (howlongsecs % 3600) % 60 how_long = '' if howlongdays != 0: howlongdays = '%s天' % (str(howlongdays)) how_long += howlongdays if hours != 0: hours = '%s小时' % (str(hours)) how_long += hours if mins != 0: mins = '%s分' % (str(mins)) how_long += mins if secs != 0: secs = '%s秒' % (str(secs)) how_long += secs return how_long # 改变字体颜色 def change_font_color(self): if self.times <= 5: self.color1, self.color2 = self.color2, self.color1 self.times += 1 t = threading.Timer(1, self.change_font_color) t.start() print( f"第 {self.times} 次:color1, color2 = {self.color1}, {self.color2}")