def make_counter_btn(x: int, y: int, font: pygame.font.Font, align: str="center", vertical_align: str="middle", min_: int=1, max_: int=5, color: Color=(0, 0, 0)): group = pygame.sprite.Group() counter_sprite = CounterSprite(x, y, font, align, vertical_align, min_, max_, color) group.add(counter_sprite) rect = counter_sprite.base_rect w = h = rect.h w = w // 3 points = [ (0, h//2), (w-1, h*0.8-1), (w-1, h*0.2) ] x, y = rect.topleft left_image = Surface((w, h)).convert_alpha() left_image.fill((255, 255, 255)) left_image.set_colorkey(left_image.get_at((0, 0))) pygame.gfxdraw.filled_polygon(left_image, points, color) # left_image = font.render("<", True, color) btn = RichSprite(x-5, y, align="right", vertical_align="top", image=left_image, press_fnc=counter_sprite._count_down) group.add(btn) x, y = rect.topright right_image = pygame.transform.flip(left_image, True, False) right_image.set_colorkey(right_image.get_at((0, 0))) btn = RichSprite(x+5, y, align="left", vertical_align="top", image=right_image, press_fnc=counter_sprite._count_up) group.add(btn) return group, counter_sprite.get_count
def _set_frame(self, clear = True, image = None): if image == None: btn_image = pygame.Surface((self.width, self.height)) btn_image.fill((200, 200, 200)) if clear: btn_image.set_colorkey(btn_image.get_at((0, 0))) self.frame_sprite = RichSprite(0, 0, image = btn_image, align = "left", vertical_align="top") else: btn_image = pygame.transform.smoothscale(image, (self.width, self.height)) self.frame_sprite = RichSprite(0, 0, image = btn_image, align = "left", vertical_align="top") self.frame_sprite.change_press_fnc(self.func, self.func_args)
def _set_title(self): title_surfaces = self.components["title"] rect = self.display.get_rect() x = rect.w // 2 y = rect.h // 3 # title_sprite = make_animation_sprites(x, y, images=title_surfaces, interval=3, multiple=1.0) title_sprite = RichSprite(x=x, y=y, images=title_surfaces, interval=4) self.middle_sprites.add(title_sprite)
def __init__(self, base_rect: Rect, values: List[Any], labels: Optional[List[str]]=None, font_name: Optional[str]=None, color: Color=(255, 255, 255), bg_color: Color=(0, 0, 0), bg_alpha: float=1.0, defalut_i: int=0): super().__init__() self.values = values self.i = defalut_i self.base_rect = base_rect if defalut_i < 0 or len(values) <= defalut_i: raise(ValueError("default_i が values のリストの範囲を超えています.")) # ラベルのSpriteのリストを作成 self.labels = labels if labels else [str(v) for v in values] font = pygame.font.Font(font_name, base_rect.h) label_surfaces = [font.render(label, True, color) for label in self.labels] self.label_sprites = [] for surface in label_surfaces: rect = surface.get_rect() rect.center = base_rect.center self.label_sprites.append(SimpleSprite(rect, surface)) # 左右のボタンの作成 size = (int(base_rect.h * 0.5), int(base_rect.h * 0.8)) left_btn_surface_inactive = make_left_triangle(size, color=(192, 192, 192)) left_btn_surface_active = make_left_triangle(size, color=(255, 140, 0)) rect = base_rect self.left_btn_active = RichSprite(*rect.midleft, align="left", vertical_align="middle", image=left_btn_surface_active, press_fnc=self._left) self.left_btn_inactive = RichSprite(*rect.midleft, align="left", vertical_align="middle", image=left_btn_surface_inactive, press_fnc=self._left) self.right_btn_active = RichSprite(*rect.midright, align="right", vertical_align="middle", image=pygame.transform.flip(left_btn_surface_active, True, False), press_fnc=self._right) self.right_btn_inactive = RichSprite(*rect.midright, align="right", vertical_align="middle", image=pygame.transform.flip(left_btn_surface_inactive, True, False), press_fnc=self._right) # 背景作成 size = (base_rect.w, base_rect.h) bg_surface = make_surface(size, alpha=False, bg_color=bg_color) bg_sprite = SimpleSprite(base_rect, bg_surface) self.add(bg_sprite) # 表示のためのグループの作成と追加 self.left_btn = GroupSingle() self.right_btn = GroupSingle() self.single_group = GroupSingle() self.add(self.left_btn) self.add(self.right_btn) self.add(self.single_group) # 初期化 self._update_images()
def _set_background(self): rect = self.display.get_rect() bg_surface = pygame.transform.scale(self.components["background"], (rect.w, rect.h)) bg_sprite = RichSprite(0, 0, align="left", vertical_align="top", image=bg_surface) self.background_sprites.add(bg_sprite)
def _set_random_btn(self): """(デバッグ)ランダム勝敗ボタンの設置 """ rect = self.display.get_rect() surface = self.font.render("random result", True, (255, 255, 255), (0, 0, 0)) random_result_btn = RichSprite(rect.w // 2, rect.h // 3, image=surface, press_fnc=self._random_result) self.middle_sprites.add(random_result_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_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_return_btn(self): #SimpleButton(width=rect.width, height=rect.height, text="Return", outline=self.game_config.components["outline"], func = self._go_to_title) font = pygame.font.Font(None, 60) textsurface = font.render("Return", True, (0, 0, 0)) return_btn = RichSprite(*self.display.get_rect().bottomleft, align="left", vertical_align="bottom", image=textsurface) return_btn.rect.move_ip(10, -10) self.hoverable(return_btn, self.game_config.components["outline"], border_width=5) return_btn.change_press_fnc(self._go_to_title) self.middle_sprites.add(return_btn)
def _set_option_btn(self): option_surface = self.font.render("option", True, (0, 0, 0)) rect = self.display.get_rect() x = rect.w // 2 y = rect.h * 3 // 4 rect = adjust_rect(option_surface.get_rect().copy(), x, y) outline_sprites = make_outline_splites(rect, self.components["outline"], border_width=1) option_btn_sprite = RichSprite( x, y, image=option_surface, enter_fnc=self._visible_sprites, enter_fnc_args=(self.front_sprites, outline_sprites), exit_fnc=self._invisible_sprites, exit_fnc_args=(self.front_sprites, outline_sprites), press_fnc=self._go_to_option_screen, ) self.middle_sprites.add(option_btn_sprite)
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))
def __init__(self, display_rect, characters, outline, gameplayer1, gameplayer2): super().__init__() self.characters = characters self.outline_image = outline self.gameplayer1 = gameplayer1 self.gameplayer2 = gameplayer2 self.margin_lr = 30 self.margin_top = 30 self.space = 20 self.rect = pygame.rect.Rect(self.margin_lr, self.margin_top, display_rect.width - self.margin_lr * 2, max(100, display_rect.height // 2)) width = (self.rect.width - self.space * (len(self.characters) - 1)) // len(self.characters) height = self.rect.height self.character_rects = [ pygame.rect.Rect(self.rect.left + i * (width + self.space), self.rect.top, width, height) for i in range(len(self.characters)) ] self.character_to_rect = {} for character, rect in zip(self.characters, self.character_rects): face_image = surface_fit_to_rect(rect=rect, surface=character.face_image) sprite = RichSprite(rect.centerx, rect.centery, image=face_image) to_hoverable(sprite, self.outline_image, self.background_sprites) sprite.change_press_fnc(self._press_character, (character, rect)) self.middle_sprites.add(sprite) self.badges = { gameplayer1: BadgeSpriteGroup(25, (200, 5, 5), "1"), gameplayer2: BadgeSpriteGroup(25, (5, 200, 5), "2") } self.front_sprites.add(self.badges.values()) self.gameplayers = [ GamePlayerSetter(gameplayer1, self._set_character), GamePlayerSetter(gameplayer2, self._set_character) ] self.add(self.gameplayers)