Example #1
0
 def _renderNoise(self):
     """ Generate a Random square of noise. """
     noise = Surface((RANDOM_SIZE + 1, RANDOM_SIZE + 1))
     for i in range(0, RANDOM_SIZE):
         for j in range(RANDOM_SIZE):
             red = random.randint(0, 255)
             green = random.randint(0, 255)
             blue = random.randint(0, 255)
             noise.set_at((i, j), (red, green, blue))
     # Set the last row and column to match the first row and column
     for i in range(0, RANDOM_SIZE):
         noise.set_at((RANDOM_SIZE, i), noise.get_at((0, i)))
         noise.set_at((i, RANDOM_SIZE), noise.get_at((i, 0)))
     # Set the last corner to complete the wrap
     noise.set_at((RANDOM_SIZE, RANDOM_SIZE), noise.get_at((0, 0)))
     return noise
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 __init__(self, left: int, top: int, game_player, image_height: int=50):
        super().__init__()

        self.game_player = game_player

        surface = game_player.character.face_image
        name = game_player.player.name
        stock = game_player.stock

        # キャラ画像
        image_width = image_height
        rect = Rect(left, top, image_width, image_height)
        image = surface_fit_to_rect(surface, rect)
        self.character_sprite = SimpleSprite(rect, image)
        self.add(self.character_sprite)

        # ストックの丸
        self.stock = stock
        self.stock_sprites = [Group() for i in range(stock)]
        x, y = self.character_sprite.rect.bottomright
        stock_radius = int(0.05 * image_height)
        left, top = x, y - stock_radius * 2
        for i in range(stock):
            surface = Surface((stock_radius * 2, stock_radius * 2)).convert_alpha()
            surface.fill((125, 125, 125))
            surface.set_colorkey(surface.get_at((0, 0)))
            pygame.gfxdraw.filled_circle(surface, stock_radius, stock_radius, stock_radius-1, (255, 255, 255))
            stock_sprite = SimpleSprite(Rect(left, top, stock_radius * 2, stock_radius * 2), surface)
            self.stock_sprites[i].add(stock_sprite)
            print(self.stock_sprites[i])
            left += stock_radius * 2

        # プレイヤーネーム
        font_size = int(0.2 * image_height)
        font = pygame.font.Font(None, font_size)
        left, bottom = self.character_sprite.rect.bottomright
        bottom -= stock_radius * 2
        self.player_name_sprite = TextSprite(
            x=left,
            y=bottom,
            align="left",
            vertical_align="bottom",
            text=name,
            font=font,
            color=(255, 255, 255)
        )
        self.add(self.player_name_sprite)

        # プレイヤーネームのbg
        width = self.character_sprite.rect.w + max(stock_radius * 2 * stock, self.player_name_sprite.rect.w) + 10
        height = self.character_sprite.rect.h
        self.base_rect = Rect(*self.character_sprite.rect.topleft, width, height)
        rect = self.base_rect
        bg_image = Surface(rect.size).convert_alpha()
        bg_image.fill((0, 0, 0))
        bg_image.set_alpha(225)
        bg_sprite = SimpleSprite(rect, bg_image)
        self.bg_group = Group()
        self.bg_group.add(bg_sprite)
Example #4
0
    def make_color_image(size, color):
        # PYGaME CHOKE POINT

        s = Surface(size).convert()
        if color:
            s.fill(color)
        else:
            s.set_colorkey(s.get_at((0, 0)))

        return s
Example #5
0
def fill_color(surface: Surface, color: tuple, max_alpha: int = 255) -> None:
    """
    Заливает картинку новым цветом, сохраняя прозрачность исходного изображения.
    :param surface: Исходное изображение.
    :param color: Цвет в виде tuple из 3-х int-ов.
    :param max_alpha: Максимальная непрозрачность получившегося изображения.
    """
    w, h = surface.get_size()
    r, g, b = color
    for x in range(w):
        for y in range(h):
            a = min(surface.get_at((x, y))[3], max_alpha)
            surface.set_at((x, y), (r, g, b, a))
def make_surface(size: Tuple[int, int], alpha: bool=True, used_colors: List[Color]=[], bg_color: Optional[Color]=None):
    if bg_color is not None:
        fill_color = bg_color
    else:
        used_colors = set(used_colors)
        for i in range(256):
            fill_color = (i, i, i)
            if fill_color not in used_colors:
                break
    surface = Surface(size)
    surface.fill(fill_color)
    if pygame.display.get_init():
        if alpha and bg_color is None:
            surface.convert_alpha()
            surface.set_colorkey(surface.get_at((0, 0)))
        else:
            surface.convert()

    return surface
Example #7
0
    def set_colorkey(self, image: Surface, color_key: ColorKey) -> Surface:

        if color_key is -1:
            return image.set_colorkey(image.get_at((0, 0)), RLEACCEL)
        return image.set_colorkey(color_key, RLEACCEL)