简体   繁体   中英

How to rotate mask of image pygame

Hi i have trouble with rotating mask of object that is rotating mask is still in the same position as original image. The point is to move mask form collision in race track.

    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)

i was trying to make new mask from surface but its not working as you can see on image while turning cars got stacked corner when they should not be enter image description here

You can't rotate the mask, but you can create a new mask after rotating the image:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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