2012-09-22 62 views
2

所以我有一个箭头作为图像。从外观看,它似乎围绕着中心旋转。但是,当它旋转时,无论哪种方式,当他的角度大约为75度时,每边都有一个错误: ValueError:地下矩形外表面区域在pygame中旋转图像

我不确定问题出在哪里,旋转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

该页面中的第一功能,仅适用于正方形图像。

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

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