Why is my Pygame sprite rotating like a collision box?

Viewed 60

Right, so this is out of all the questions and bugs I have had in Python/Pygame, the strangest one to me. So I just learned how to rotate something in Pygame and it was working fine with everything else except this one sprite.

This is what rotates it: rotated_roads = pygame.transform.rotate(roads, angle)

This is what blits it: screen.blit(rotated_roads, (240 + x - 465, 0))

I don't know what the problem is, but it's not rotating the way I was expecting it to; to me, it kind of looked like it was rotating like a collision box. [The sprite's shape is a rectangle] I don't know what the problem is, so can you help me??

Thanks.

1 Answers

See How do I rotate an image around its center using PyGame?.

Get the rectangle of the original image and set the position. Get the rectangle of the rotated image and set the center position through the center of the original rectangle:

rotated_roads = pygame.transform.rotate(roads, angle)
rect = roads.get_rect(topleft = (240 + x - 465, 0))
rotated_rect = rotated_roads.get_rect(center = rect.center)
screen.blit(rotated_roads, rotated_rect)
Related