【问题标题】:How to rotate mask of image pygame如何旋转图像pygame的蒙版
【发布时间】:2022-08-07 16:41:19
【问题描述】:

嗨,我在旋转蒙版的对象时遇到问题,旋转蒙版仍与原始图像处于相同位置。关键是在赛道上移动掩码形式的碰撞。

    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)

我试图从表面制作新面具,但它不起作用 正如您在图像上看到的那样,当汽车转弯时,它们不应该堆积在角落里 enter image description here

    标签: python pygame


    【解决方案1】:

    您不能旋转蒙版,但可以在旋转图像后创建一个新蒙版:

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

    【讨论】:

    • 我已经尝试过了,但没有帮助,所以错误将在其他地方
    • offset = (int(cars.cars[counter].x), int(cars.cars[counter].y)) track.track_mask.overlap(cars.cars[counter].car_mask, offset)
    • 我将其用于碰撞贴图绘制在 0,0
    • @Kajstrl 你必须计算到new_rect 的偏移量
    • cars.cars[counter].y -= cars.cars[counter].speed * math.sin(math.radians(cars.cars[counter].angle)) # 8 cars.cars[counter].x += cars .cars[counter].speed * math.cos(math.radians(-cars.cars[counter].angle)) # 8 im or not bcs im 改变 x 和 y
    猜你喜欢
    • 1970-01-01
    • 2016-07-31
    • 2016-10-24
    • 2021-09-05
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多