如何使用pytmx正确显示旋转对象?

时间:2019-06-29 11:49:42

标签: python pygame pytmx

经过大量研究后,我没有找到答案,当我尝试使用pytmx在pygame中显示对象时,结果被完全破坏了,因为x,y随旋转而变化。我尝试使用矩阵旋转,但是为此,我需要知道原始中心。我不知道如何找到它,因为Tiled旋转后会向我发送x,y ...

所以我的目标只是使用pytmx在pygame中显示对象图块。

if isinstance(layer, pytmx.TiledObjectGroup):
        for object in layer:
            if (object.image):
                assets_surface = pygame.Surface((object.width, object.height), pygame.SRCALPHA)
                assets_surface.blit(object.image, (0, 0))
                assets_surface_rotate = pygame.transform.rotate(assets_surface, -object.rotation)
                rdc.blit(assets_surface_rotate, (object.x, object.y))

如果我仅使用pygame.transform.rotate:

{{1}}

对于图块对象,我在x,y位置上的位置错误:

image of the error

2 个答案:

答案 0 :(得分:0)

我认为旋转后通过对象的x和y位置时您在做一些错误。我从未使用过贴图,所以我不知道具体细节,但是在pygame中,当您将位置传递到blit时,应该传递左上角的坐标。 Surface的左上角将在这些坐标处。

rdc.blit(assets_surface_rotate, (object.x, object.y))

在这里,我不知道object.xobject.y的确切坐标,但是我敢打赌它们不是左上角,否则您的代码应该可以工作。

通常,要执行这些工作,您可以使用Sprite类或子类,这会有所帮助。

class TMSprite(pygame.sprite.Sprite):
    # Constructor. Create a Surface from a TileMap and set its position
    def __init__(self, tmo, x, y, width, height):
        # Call the parent class (Sprite) constructor
        super(TMSprite, self).__init__()

        # Create the image of the block
        self.image = pygame.Surface((width, height), pygame.SRCALPHA)
        self.image.blit(tmo.image, (0, 0))

        # Fetch the rectangle object that has the dimensions of the image
        # Set its position with the move method
        self.rect = self.image.get_rect().move(x, y)

    def rotate(self, angle):
        # TMSprite rotation on its center of a given angle
        rot_center = self.rect.center
        self.image = pygame.transform.rotate(self.image, angle)
        self.rect = self.image.get_rect()
        self.rect.center = rot_center

这就是使用TMSprite类重写代码段的方式。

if isinstance(layer, pytmx.TiledObjectGroup):
    for tmob in layer:
        if (tmob.image):
            x = tmob.x #x should be that of the top-left corner. Adjust the formula if tmob.x is not the top-left
            y = tmob.y #y should be that of the top-left corner. Adjust the formula if tmob.y is not the top-left
            assets_sprite = TMSprite(tmob, x, y, tmob.width, tmob.height)
            assets_sprite.rotate(-object.rotation)
            rdc.blit(assets_sprite.image, assets_sprite.rect)

在这里,我没有传递左上角的坐标,而是将精灵的Rect传递给blitblit方法将从矩形中提取坐标。

请注意,旋转是在Surface的中心上执行的。旋转后,如果该角度不是90°的倍数,则由于必须将曲面的正方形与屏幕对齐,因此会放大曲面。如果存在Alpha通道没有问题,则多余的像素是透明的,但是左上角会改变。

答案 1 :(得分:0)

好吧,如果有人需要,我已经找到了解决方案:

elif isinstance(layer, pytmx.TiledObjectGroup):
        for Object in layer:
            if (Object.image):
                if Object.rotation != 0:
                    angle = math.radians(-Object.rotation)
                    center_x = Object.centerX
                    center_y = Object.centerY
                    Object_y = Object.y + Object.height

                    id_rotation = [ [math.cos(angle), -math.sin(angle)],
                                    [math.sin(angle), math.cos(angle)] ]
                    R = numpy.matrix(id_rotation)

                    id_position = [ [Object.x - center_x],
                                    [Object_y - center_y] ]
                    P = numpy.matrix(id_position)

                    id_center = [ [center_x],
                                [center_y] ]
                    C = numpy.matrix(id_center)

                    position_without_rotation = numpy.dot(R, P) + C

                    no_rotation_x = position_without_rotation[0]
                    no_rotation_y = position_without_rotation[1] - Object.height #Repere Tiled pas le meme que Pygame

                    Object_surface = pygame.Surface((Object.image.get_rect()[2], Object.image.get_rect()[3]), pygame.SRCALPHA)
                    Object_surface.blit(Object.image, (0, 0))
                    Object_surface_scale = pygame.transform.scale(Object_surface, (round(Object.width), round(Object.height)))
                    Object_surface_rotate = pygame.transform.rotate(Object_surface_scale, -Object.rotation) #Pygame va en anti horaire

                    extra_x = (Object_surface_rotate.get_rect()[2] - Object.width) / 2
                    extra_y = (Object_surface_rotate.get_rect()[3] - Object.height) / 2

                    rdc.blit(Object_surface_rotate, (no_rotation_x - extra_x, no_rotation_y - extra_y))
                else:
                    Object_surface = pygame.Surface((Object.image.get_rect()[2], Object.image.get_rect()[3]), pygame.SRCALPHA)
                    Object_surface.blit(Object.image, (0, 0))
                    Object_surface_scale = pygame.transform.scale(Object_surface, (round(Object.width), round(Object.height)))

                    rdc.blit(Object_surface_scale, (Object.x, Object.y))