如何正确地相对于图像中心旋转蒙版?

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

我使用以下方法围绕中心旋转图像:

    def rotate(self, obj):
        image_rect = obj['object'].get_rect()
        rotated_rect = image_rect.copy()
        rotated_rect.center = (obj['x'] + obj['scale'][0] // 2, 
                               obj['y'] + obj['scale'][1] // 2)
        rotated_image = pygame.transform.rotate(obj['object'], 
                                                obj['angle'])
        rotated_rect = rotated_image.get_rect(center=rotated_rect.center)
        
        obj['mask'] = pygame.mask.from_surface(rotated_image)
        obj['mask_image'] = obj['mask'].to_surface()
        self.game.screen.blit(obj['mask_image'],(obj['x'],obj['y']))
        self.game.screen.blit(rotated_image, rotated_rect.topleft)

        return rotated_image, rotated_rect

然后我用旋转后的图像更新蒙版,但是蒙版不会相对于中心旋转,它不断增大和缩小

遮罩不能给定中心或坐标,据我了解,它是自动设置的,只能指定显示。

绘制“blit”时,我们使用“rotated_rect.topleft”设置图像的坐标 但掩模继续旋转而不是相对于中心 问题是如何相对于图像中心旋转蒙版?

enter image description here

python pygame mask
1个回答
0
投票

旋转图像时,图像的尺寸可能会变大,请参阅如何使用 Pygame 围绕其中心旋转图像?。您可以使用

rotated_rect = rotated_image.get_rect(center=rotated_rect.center)
更新图像的位置。 '您从旋转图像生成蒙版,但您想更新蒙版的位置:

obj['mask'] = pygame.mask.from_surface(rotated_image)
obj['x'] = rotated_rect.x
obj['y'] = rotated_rect.y
© www.soinside.com 2019 - 2024. All rights reserved.