Greg Peckory
Greg Peckory

Reputation: 8068

Rotating image in pygame

So I have an arrow as an image. By the looks of it, it seems to be rotating right around the centre. But when it rotates, either way, when it his an angle of around 75 degrees on each side there is an error: ValueError: subsurface rectangle outside surface area

I'm not sure what the problem is, I got the rotating function off of pygames website. If anyone knows what the problem is, I'd really appreciate your help. Here's the code:

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)

Upvotes: 2

Views: 3270

Answers (1)

ninMonkey
ninMonkey

Reputation: 7511

The first function on that page, is for square images only.

For arbitrary dimensions, use the second one:

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

Upvotes: 4

Related Questions