def _set_back_btn(self): back_btn_image = self.font.render("Back", True, (0, 0, 0)) back_btn = RichSprite(*self.display_rect.bottomleft, image=back_btn_image, align="left", vertical_align="bottom") back_btn.rect.move_ip(5, -5) outline = make_outline_sprites(back_btn.rect, self.outline_image) back_btn.change_enter_fnc(self.middle_sprites.add, outline) back_btn.change_exit_fnc(self.middle_sprites.remove, outline) back_btn.change_press_fnc(self._goto_title) self.front_sprites.add(back_btn)
def _set_start_btn(self): # start_btn_surfaces = self.components["start"] start_btn_surface = self.font.render("start", True, (0, 0, 0)) rect = self.display.get_rect() x = rect.w // 2 y = rect.h * 2 // 3 # start_btn_sprite = RichSprite(x=x, y=y, images=start_btn_surfaces, interval=5, press_fnc=self._go_to_character_select_screen) start_btn_sprite = RichSprite( x=x, y=y, image=start_btn_surface, press_fnc=self._go_to_character_select_screen) self.middle_sprites.add(start_btn_sprite) outline_sprites = make_outline_splites(start_btn_sprite.rect, self.components["outline"], border_width=1) start_btn_sprite.change_enter_fnc( self._visible_sprites, (self.front_sprites, outline_sprites)) start_btn_sprite.change_exit_fnc(self._invisible_sprites, (self.front_sprites, outline_sprites))
class CharacterSelectScreen(BaseScreen): def __init__(self, game_config, gameplayer1, gameplayer2): super().__init__() self.game_config = game_config self.players = list(self.game_config.players.values()) self.characters = list(self.game_config.characters.values()) self.gameplayer1 = gameplayer1 self.gameplayer2 = gameplayer2 self.margin_lr = 30 self.display_rect = self.display.get_rect() self.character_select_rect = None self.player_select_rect = None self.outline_image = self.game_config.components["outline"] self.outline_image = pygame.transform.scale2x(self.outline_image) self.font_size = 40 self.font = pygame.font.SysFont(None, self.font_size) self.character_select_area = CharacterSelectArea( self.display_rect, self.characters, self.outline_image, self.gameplayer1, self.gameplayer2) def _goto_stage_select(self): for i, gameplayer in enumerate([self.gameplayer1, self.gameplayer2]): gameplayer.player = self.player_select_btn[i].get_value() self.next_screen = Screen.STAGE_SELECT self.run = False def _goto_title(self): self.next_screen = Screen.START self.run = False def _set_next_btn(self): next_btn_image = self.font.render("Next", True, (0, 0, 0)) self.next_btn = RichSprite(*self.display_rect.bottomright, image=next_btn_image, align="right", vertical_align="bottom") self.next_btn.rect.move_ip(-5, -5) outline = make_outline_sprites(self.next_btn.rect, self.outline_image) self.next_btn.change_enter_fnc(self.middle_sprites.add, outline) self.next_btn.change_exit_fnc(self.middle_sprites.remove, outline) self.next_btn.change_press_fnc(self._goto_stage_select) self.front_sprites.add(self.next_btn) def _set_back_btn(self): back_btn_image = self.font.render("Back", True, (0, 0, 0)) back_btn = RichSprite(*self.display_rect.bottomleft, image=back_btn_image, align="left", vertical_align="bottom") back_btn.rect.move_ip(5, -5) outline = make_outline_sprites(back_btn.rect, self.outline_image) back_btn.change_enter_fnc(self.middle_sprites.add, outline) back_btn.change_exit_fnc(self.middle_sprites.remove, outline) back_btn.change_press_fnc(self._goto_title) self.front_sprites.add(back_btn) def _set_characters_area(self): self.middle_sprites.add(self.character_select_area) def _set_player_select_area(self): self.player_select_rect = pygame.rect.Rect( self.margin_lr, self.character_select_area.rect.bottomleft[1] + (self.display_rect.height - self.character_select_area.rect.bottomleft[1]) // 2, self.character_select_area.rect.width, 30) self.players_rects = layout_rects(self.player_select_rect, 2, 1, margin_horizontal=10) self.players_name = [player.name for player in self.players] self.colors = [(200, 200, 200), (200, 200, 200)] self.player_select_btn = [ ValuesGroup(base_rect=self.players_rects[i], values=self.players, labels=self.players_name, color=(0, 0, 0), bg_color=self.colors[i], defalut_i=i) for i in range(len(self.players_rects)) ] self.front_sprites.add(self.player_select_btn) def _set_bgm(self): self.bgm: pygame.mixer.Sound = self.game_config.sounds["menu"] self.bgm.set_volume(0.1) self.bgm.play() def _adapt_display(self): pygame.display.set_caption("Character Select") bg_image = self.game_config.components["background"] bg_image = pygame.transform.scale(bg_image, self.display_rect.size) bg_sprite = SimpleSprite(rect=self.display_rect, image=bg_image) self.background_sprites.add(bg_sprite) self._set_characters_area() self._set_player_select_area() self._set_next_btn() self._set_back_btn() def update(self): super().update() if self.front_sprites.has( self.next_btn) and self.player_select_btn[0].get_value( ) == self.player_select_btn[1].get_value(): self.front_sprites.remove(self.next_btn) elif not self.front_sprites.has( self.next_btn) and self.player_select_btn[0].get_value( ) != self.player_select_btn[1].get_value(): self.front_sprites.add(self.next_btn) def main(self): self._adapt_display() super().main()