0

I've read several other posts about rotating an image around the center point, but I've yet to figure it out. I even copy pasted one of the solutions posted on another SO question and it didn't work.

This is my code

  def rotate(self, angle):
    self.old_center = self.surface.get_rect().center
    self.surface = pygame.transform.rotate(self.surface, angle)
    self.surface.get_rect(center = self.old_center)

it's inside a class which contains the surface.

When I call this method the image rotates but also translates, and gets distorted.

niebula
  • 351
  • 1
  • 6
  • 13

1 Answers1

2

You are not assigning the new rect, you should do something like:

self.rect = self.surface.get_rect(center = self.old_center)

And you should always keep the original surface and rotate from the original, that way distortion doesn't accumulate when rotating multiple times.

Update

If you don't want to keep track of the rect object, you can do it every time you blit. If you keep the center coordinates.

Example:

rect = object.surface.get_rect()
rect.center = object.center
display.blit(object.surface, rect.topleft)
pmoleri
  • 4,238
  • 1
  • 15
  • 25
  • I was under the impression that "self.surface = pygame.transform.rotate(self.surface, angle)" implicitly assigned a new rotated rect since rect is a property of the surface. My class doesn't contain a rect property, I use surface.get_rect() everytime. – niebula Sep 17 '13 at 20:50