繁体   English   中英

如何旋转图像 pygame 的蒙版

[英]How to rotate mask of image pygame

嗨,我在旋转遮罩的 object 时遇到问题,旋转遮罩仍在与原始图像相同的 position 中。 关键是在赛道上移动掩码形式的碰撞。

    def __init__(self, x, y , height , width):
        self.x = x - width / 2
        self.y = y - height / 2
        self.height = height
        self.width = width
        self.car_img = pygame.image.load('img/auticko.png').convert_alpha()
        self.car_rect = self.car_img.get_rect()
        self.car_mask = pygame.mask.from_surface(self.car_img)
        self.surface = pygame.Surface((height, width),pygame.SRCALPHA)

        self.surface.blit(self.car_img, (0, 0))
        self.angle = 0
        self.speed = 2
    def draw(self,screen):  # 3
        self.car_rect.topleft = (int(self.x), int(self.y))
        rotated = pygame.transform.rotate(self.surface, self.angle)
        #rotated.set_colorkey((0, 0, 0))
        surface_rect = self.surface.get_rect(topleft=self.car_rect.topleft)
        new_rect = rotated.get_rect(center=surface_rect.center)
        screen.blit(rotated, new_rect.topleft)

我试图从表面制作新的面具,但它不起作用,正如你在图像上看到的那样,当汽车不应该在此处输入图像描述时,它们会在转弯时堆积角落

您不能旋转蒙版,但可以在旋转图像后创建一个新蒙版:

rotated = pygame.transform.rotate(self.image, self.angle)
self.car_mask = pygame.mask.from_surface(rotated)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM