簡體   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