如何旋转图像pygame的掩码



嗨,我在旋转蒙版时遇到了问题,因为旋转蒙版的对象仍在原始图像的同一位置。重点是在赛道上移动面罩以避免碰撞。

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.surface, self.angle)
self.car_mask = pygame.mask.from_surface(rotated)

最新更新