[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] How to rotate a rect

Leonardo Santagada writes:
 > Can a rect be rotated? Can I make the rect to rotate with some image? I mean
 > to have a rect like this ((0,2),(5,0),(3,5),(7,4))? I haven't tried this
 > already but it is the only thing that can stop me from start coding. If
 > someone want to show how to make this i will be quite happy.

The rect object itself can't be rotated - it's edges have to be
parallel with the x and y axis of the display surface.

That said, an image itself can be rotated, using the
pygame.transform.rotate() and pygame.transform.rotozoom()
functions. Create an image surface with pygame.image.load(), then
rotate it with either of the above functions - for example, to rotate
image_surf by 45 degrees counterclockwise, I might call...

new_surface = pygame.transform.rotate(image_surf, 45)

Now new_surface will contain the original image, rotated 45
degrees. As a result of this rotation, new_surface will be much larger
than the original image_surf was, to accomodate the corners of the
rotated image. You can blit away with new_surface now.

Hope this helps.

David Clark
futility at #pygame on irc.openprojects.net

pygame mailing list