无法在pygame中的透明表面上取消绘制

问题描述 投票:0回答:1

我正在尝试创建一种方法来基本上擦除表面上的绘图。我的功能适用于包含 png 的表面,但是当我使用相同的方法删除 pygame.draw.lines() 绘制的表面时,它不会被擦除。

这两个表面之间的主要区别是一个是透明的,另一个是不透明的。我想我可能不完全理解 pygames 透明表面是如何工作的。

无法使用 undraw():

class MovementComponent(AbstactComponent):
    def __init__(self) -> None:
        super().__init__()
        game_surfaces = GameSurfaces() #singleton class that rerenders all of the surfaces in order
        self.movement_surface = game_surfaces.movement_surface
        self.path_surface = pg.Surface(self.movement_surface.get_size(), pg.SRCALPHA)
        self.path_surface.set_alpha(150)

    def draw_movement(self):
        if len(self.queue()) >= 2:
            tile_centers = []
            for tile in self.queue():
                tile_centers.append(tile.center_pixel)

            pg.draw.lines(
                self.path_surface, 
                self.character.color, 
                False, tile_centers, 3
            )
            
            self.movement_surface.blit(self.path_surface, (0,0))

    def undraw_movement(self):
        empty = pg.Color(0,0,0,0)
        self.path_surface.fill(empty)
        self.movement_surface.blit(self.path_surface, (0,0))

另一个组件中的工作取消绘制:

class SpriteComponent(AbstactComponent):
    NORMAL_SIZE = (55,55)

    def __init__(self, image: pg.image):
        surfaces = GameSurfaces()
        self.char_surface = surfaces.character_surface
        self.pixel_pos: (int,int) = None

        self.standard_image = pg.transform.scale(image, self.NORMAL_SIZE)
        self.empty = pg.Color(0,0,0,0)

    def draw(self, pixel_pos: (int, int)):
        self.pixel_pos = pixel_pos
        top_left_pixel = self.get_topleft_pos(pixel_pos)
        self.char_surface.blit(self.standard_image, top_left_pixel)

    def undraw(self, pixel_pos: (int, int)=None):
        pixel_pos = pixel_pos if pixel_pos else self.pixel_pos
        top_left_pixel = self.get_topleft_pos(self.pixel_pos)
        rect_to_clear = pg.Rect(top_left_pixel, self.NORMAL_SIZE)
        self.char_surface.fill(self.empty, rect_to_clear)
python pygame pygame-surface
1个回答
0
投票

透明的

Surface
与背景混合。然而,删除时,您希望完全覆盖背景。因此,您必须再次使
Surface
暂时不透明才能删除:

class MovementComponent(AbstactComponent):
    # [...]

    def undraw_movement(self):
        empty = pg.Color(0,0,0,0)
        self.path_surface.fill(empty)
        self.path_surface.set_alpha(None)
        self.movement_surface.blit(self.path_surface, (0,0))
        self.path_surface.set_alpha(150)
© www.soinside.com 2019 - 2024. All rights reserved.