0
def rotate(self):
    #Save the original rect center
    self.saved_center=self.rect.center

    #Rotates a saved image every time to maintain quality
    self.image=pygame.transform.rotate(self.saved_image, self.angle)

    #Make new rect center the old one
    self.rect.center=self.saved_center

    self.angle+=10

When I rotate the image, there is a weird shifting of it despite the fact that I'm saving the old rect center and making the rotated rect center the old one. I want it to rotate right at the center of the square.

Here's what it looks like: https://i.stack.imgur.com/vARoF.gif

jdi
  • 90,542
  • 19
  • 167
  • 203
  • Can you post a working example? – jdi May 18 '12 at 01:43
  • Also, I had answered a similar pygame question before that also address doing rotation while keeping it centered. Maybe you can [review this question](http://stackoverflow.com/a/9848408/496445) to see if it helps? – jdi May 18 '12 at 01:44
  • main: http://dl.dropbox.com/u/11788669/main.py and module: http://dl.dropbox.com/u/11788669/sprite_module.py. And I'll look into the link you posted hopefully it helps :) – PhoneMicrowave May 18 '12 at 01:57

2 Answers2

2

You are just calculating the new rect wrong. Try this:

def rotate(self):
    self.image=pygame.transform.rotate(self.saved_image, self.angle)
    self.rect = self.image.get_rect(center=self.rect.center)
    self.angle+=10

It tells the new rect to center itself around the original center (the center never changes here. Just keeps getting passed along).

The issue was that the self.rect was never being properly updated. You were only changing the center value. The entire rect changes as the image rotates because it grows and shrinks in size. So what you needed to do was completely set the new rect each time.

self.image.get_rect(center=self.rect.center)

This calculates a brand new rect, while basing it around the given center. The center is set on the rect before it calculate the positions. Thus, you get a rect that is properly centered around your point.

jdi
  • 90,542
  • 19
  • 167
  • 203
  • It's so beautiful :o! Thanks so much. Can you explain what self.image.get_rect(center=self.rect.center) is and how it differs from self.rect.center. I did a test run and printed the variables. reg center: (325, 225) center rect: – PhoneMicrowave May 18 '12 at 02:47
  • I have to say this is the best one-line solution I have seen in a while. Thank you for this! – invert Sep 24 '13 at 09:45
0

I had this issue. My method has a bit of a different purpose, but I solved it quite nicely.

import pygame, math

def draw_sprite(self, sprite, x, y, rot):
    #'sprite' is the loaded image file.
    #'x' and 'y' are coordinates.
    #'rot' is rotation in radians.

    #Creates a new 'rotated_sprite' that is a rotated variant of 'sprite'
    #Also performs a radian-to-degrees conversion on 'rot'.
    rotated_sprite = pygame.transform.rotate(sprite, math.degrees(rot))

    #Creates a new 'rect' based on 'rotated_sprite'
    rect = rotated_sprite.get_rect()

    #Blits the rotated_sprite onto the screen with an offset from 'rect'
    self.screen.blit(rotated_sprite, (x-(rect.width/2), y-(rect.height/2)))
Ruben Bakker
  • 446
  • 1
  • 5
  • 11