如何旋转图像pygame的口罩

发布于 01-17 18:53 字数 1136 浏览 1 评论 0原文

嗨,我在旋转旋转掩码的对象面罩上遇到麻烦,仍然与原始图像相同。关键是要在赛道上移动面膜形式碰撞。

    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)

我试图从表面制作新面具,但它不起作用 正如您在图像上看到的那样,转动汽车不应该是 在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信2025-01-24 18:53:33

您无法旋转蒙版,但可以在旋转图像后创建新蒙版:

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

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

rotated = pygame.transform.rotate(self.surface, self.angle)
self.car_mask = pygame.mask.from_surface(rotated)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文