2

所以我有一个箭头作为图像。从外观上看,它似乎围绕着中心旋转。但是当它旋转时,无论哪种方式,当它在每边大约 75 度的角度时都会出现错误: ValueError: subsurface rectangle outside surface area

我不确定问题是什么,我从 pygames 网站获得了旋转功能。如果有人知道问题出在哪里,我将非常感谢您的帮助。这是代码:

screen = pygame.display.set_mode(ss)
arrow = pygame.image.load("obj.png").convert_alpha()

x = 400
y= 300
a = 0
turn = 2

def rot_center(image, angle):
    """rotate an image while keeping its center and size"""
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image

while True:
    screen.fill((255, 255, 255))
    if turn == 1:
        a += 10
    if turn == 2:
        a -=10
    if a == 90:
        turn = 2
    if a == -90:
        turn = 1

    rotarrow = rot_center(arrow, a)
    screen.blit(rotarrow, (x, y))
    pygame.display.update()
    sleep(0.1)
4

1 回答 1

4

该页面上的第一个功能仅适用于方形图像。

对于任意尺寸,使用第二个:

def rot_center(image, rect, angle):
        """rotate an image while keeping its center"""
        rot_image = pygame.transform.rotate(image, angle)
        rot_rect = rot_image.get_rect(center=rect.center)
        return rot_image,rot_rect
于 2012-09-23T00:31:05.053 回答