class EstadoSom(Estado): def __init__(self): super().__init__() self.__texto_som = Texto("Som", "Materials/Early GameBoy.ttf", 50, self.BLACK, [130, 40]) self.__texto_musica = Texto("Volume música:", "Materials/Retro Gaming.ttf", 25, self.WHITE, [20, 150]) self.__texto_efeitos = Texto("Volume efeitos:", "Materials/Retro Gaming.ttf", 25, self.WHITE, [20, 300]) self.__slider_musica = Slider(self.tela.display, 20, 230, 300, 20, min=0, max=1, step=0.1, handleColour=self.GREEN) #self.__slider_musica.setValue(self.som_controller.volume_atual_music) self.__slider_sons = Slider(self.tela.display, 20, 380, 300, 20, min=0, max=1, step=0.1, handleColour=self.GREEN) #self.__slider_sons.setValue(self.som_controller.volume_atual_sound) self.__sliders = [self.__slider_musica, self.__slider_sons] self.__events_som = EventsSom(self.__sliders) self.__numero_musica = Texto("", "Materials/Retro Gaming.ttf", 25, self.WHITE, [335, 225]) self.__numero_sons = Texto("", "Materials/Retro Gaming.ttf", 25, self.WHITE, [335, 375]) self.__imagem_botao_voltar = pygame.image.load("Materials/voltar.png").convert_alpha(self.tela.display) self.__fundo_voltar = Fundo([20, 510, 70, 70], self.WHITE) self.__botao_voltar = BotaoImagem(self.__imagem_botao_voltar, (25, 515), self.__fundo_voltar, self.GREEN, self.DARK_GREEN, self.__events_som) self.__texto_vol_padrao = Texto("Volume Padrão", "Materials/Retro Gaming.ttf", 27, self.BLACK, [125, 525]) self.__fundo_vol_padrao = Fundo([110, 510, 270, 70], self.WHITE) self.__botao_vol_padrao = Botao(self.__texto_vol_padrao, self.__fundo_vol_padrao, self.GREEN, self.DARK_GREEN, self.__events_som) def start(self): #Updates self.__events_som.check_events() self.som_controller.set_volume_music(self.__slider_musica.getValue()) self.som_controller.set_volume_sounds(self.__slider_sons.getValue()) voltar = self.__botao_voltar.update() padrao = self.__botao_vol_padrao.update() if padrao: self.som_controller.set_volume_padrao() self.__slider_musica.setValue(self.som_controller.volume_atual_music) self.__slider_sons.setValue(self.som_controller.volume_atual_sound) porcentagem_musica = self.__slider_musica.getValue() * 100 porcentagem_sons = self.__slider_sons.getValue() * 100 self.__numero_musica.texto = str(int(porcentagem_musica)) + "%" self.__numero_sons.texto = str(int(porcentagem_sons)) + "%" #Draws self.tela.fill(self.GREY) self.__texto_som.draw() self.__texto_musica.draw() self.__texto_efeitos.draw() self.__slider_musica.draw() self.__slider_sons.draw() self.__numero_musica.draw() self.__numero_sons.draw() self.__botao_voltar.draw() self.__botao_vol_padrao.draw() if voltar: return "inicial" else: return "som"
class LabeledSlider: def __init__(self, screen, pos_x, pos_y, label_text, width=200, label_size=150, min=0.0, max=1.0, initial=0.5, margin=20): self.width = width self.label_size = label_size self.label = TextBox(screen, pos_x, pos_y, label_size, 30, fontSize=16) self.label.setText(label_text) self.slider = Slider(screen, pos_x + self.label_size + margin, pos_y, self.width, 20, min=min, max=max, step=0.01, initial=initial) self.output = TextBox(screen, pos_x + self.label_size + self.width + margin * 2, pos_y, 30, 20, fontSize=10) def draw(self): self.label.draw() self.slider.draw() self.output.draw() def update(self, events): self.slider.listen(events) self.output.setText("%.2f" % self.slider.getValue()) def get_value(self): return self.slider.getValue() def set_value(self, value): self.slider.setValue(value)
class Settings(): def __init__(self, sounds): self.frame_h = int(str_dict.get("h")) self.frame_w = int(str_dict.get("w")) self.sc = pygame.display.set_mode((self.frame_w, self.frame_h)) self.mus_curr_vol = 0 self.snd_curr_vol = 0 self.sounds = sounds self.diff = 'medium' # Инициализация слайдеров, координаты наверное можно лучше сделать self.sound_vol_slider = Slider(self.sc, self.frame_w * 2 // 5, self.frame_h * 1 // 10, 100, 20, colour=(100, 100, 100), handleColour=(40, 40, 40)) self.music_vol_slider = Slider(self.sc, self.frame_w * 2 // 5, self.frame_h * 2 // 10, 100, 20, colour=(100, 100, 100), handleColour=(40, 40, 40)) update_settings() self.sound_vol_slider.setValue( int(float(settings_dict.get('sound_volume')) * 100)) self.music_vol_slider.setValue( int(float(settings_dict.get('music_volume')) * 100)) self.BACKGROUND_offset = 0 self.BACKGROUND_speed = 3 self.FPS = int(str_dict.get("FPS")) self.clock = pygame.time.Clock() # Запуск def run(self): global event while True: events = pygame.event.get() # Обновление слайдеров self.music_vol_slider.listen(events) self.sound_vol_slider.listen(events) self.clock.tick(self.FPS) for event in events: if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() rect = pygame.Rect( 0, self.sc.get_height() - int(str_dict.get('button_y')), int(str_dict.get('button_x')), int(str_dict.get('button_y'))) # Взаимодействие с кнопками сложности if self.easy_b_rect.collidepoint(pos): self.diff = 'easy' elif self.medium_b_rect.collidepoint(pos): self.diff = 'medium' elif self.hard_b_rect.collidepoint(pos): self.diff = 'hard' # Выход из настроек. Сохранение настроек. if rect.collidepoint(pos): with open('Res/CSV/settings.csv', encoding="utf8") as csvfile: change, write = dict(), list() reader = csv.reader(csvfile, delimiter=',', quotechar='"') for row in reader: change[row[0]] = row[1] # В change записываются изменённые настройки change[ 'music_volume'] = self.music_vol_slider.getValue( ) / 100 change[ 'sound_volume'] = self.sound_vol_slider.getValue( ) / 100 change['difficulty'] = self.diff write = list( map(lambda x: [x, change[x]], change.keys())) writer = csv.writer(open('Res/CSV/settings.csv', 'w', encoding="utf8", newline=''), delimiter=',', quoting=csv.QUOTE_ALL) writer.writerows(write) return self.redraw_window() # Перерисовка окна def redraw_window(self): # Текст около слайдеров sound_text = MAIN_FONT.render("Sound:", True, (170, 170, 170)) music_text = MAIN_FONT.render("Music:", True, (170, 170, 170)) # Движение фона self.sc.blit(BACKGROUND, (0, self.BACKGROUND_offset - BACKGROUND.get_height())) self.sc.blit(BACKGROUND, (0, self.BACKGROUND_offset)) self.BACKGROUND_offset += self.BACKGROUND_speed if self.BACKGROUND_offset > BACKGROUND.get_height(): self.BACKGROUND_offset = 0 self.sc.blit(BACK_BUTTON, (0, self.sc.get_height() - int(str_dict.get('button_y')))) # Кнопки сложности self.easy_b_rect = self.sc.blit(easy_b, (40, 180)) self.medium_b_rect = self.sc.blit(medium_b, (150, 180)) self.hard_b_rect = self.sc.blit(hard_b, (260, 180)) # Текст около слайдеров self.sc.blit(sound_text, (self.frame_w * 1 // 10 - 10, self.frame_h * 1 // 10 - 5)) self.sc.blit(music_text, (self.frame_w * 1 // 10 - 1, self.frame_h * 2 // 10 - 5)) self.sc.blit(settings_txt, ((self.frame_w - settings_txt.get_width()) // 2, settings_txt.get_height())) self.music_vol_slider.draw() self.sound_vol_slider.draw() # Изменение громкости если значение было изменено if self.mus_curr_vol != self.music_vol_slider.getValue(): self.change_volume(music=True, volume=self.music_vol_slider.getValue()) self.mus_curr_vol = self.music_vol_slider.getValue() if self.snd_curr_vol != self.sound_vol_slider.getValue(): self.change_volume(music=False, volume=self.sound_vol_slider.getValue()) self.snd_curr_vol = self.sound_vol_slider.getValue() pygame.display.update() # Изменение громкости, music - отвечает за тип звуков (True - изменение громкости музыки, False - остальных звуков) def change_volume(self, music=False, volume=99): volume /= 100 for i in self.sounds.keys(): if not (music ^ ('music' in i)): for j in self.sounds[i]: pygame.mixer.Channel(j).set_volume(volume)