1

I'm currently trying to code a game which includes a wee robot landing on a pad. In trying to work out the physics of the way it would fall, I have come across an issue with the rotation. It rotates on a left or right key press, in the respective direction.

I've tried using blit with the rect.center but it still doesn't seem to work. Any help would be much appreciated!!

 def rotate_right(self):
    self.new_angle = self.angle + 30
    self.rotate_lander()   

 def rotate_lander(self):
    self.image = pygame.transform.rotozoom(self.image, self.new_angle, 1)
    self.rect = self.image.get_rect(center=self.image.get_rect().center)
    screen.blit(self.image, self.rect.center)

I've managed to get it to rotate, but it moves with every rotation, and I need it to stay in the same position. I think the centre is off, but I'm not sure where it could have gone wrong.

ellie
  • 19
  • 1
  • Hi @ellie, great first question! Welcome to Stack Overflow :) – André Laszlo Mar 29 '19 at 16:21
  • 2
    Possible duplicate of [How do I rotate an image around its center using Pygame?](https://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame) – Rabbid76 Mar 29 '19 at 16:21

1 Answers1

0

First, in rotate_lander(), you should always rotate the original picture, otherwise it will get distorted (And in your case, run away). So, create another copy of self.image, which you wont change.

original = pygame.Surface(...)
...
def rotate_lander(self):
   self.image = pygame.transform.rotozoom(self.original, self.new_angle, 1)

But now, the image still won't rotate on exact same position. The problem is, that the bounding box of image is changing. And with that, the position of every point of image changes. You shouldn't set the position of self.rect to the center, because it is moving. Instead, you must update the position according to the change of bounding box. You should compare the bounding box before and after the rotation. You have a full tutorial on this topic already answered here:

Rotate an image around its center

*If you just wish to keep an image on place (not rotating it around its center), you can just get rid of center.

 def rotate_lander(self):
   self.image = pygame.transform.rotozoom(self.image, self.new_angle, 1)
   self.rect = self.image.get_rect()
   screen.blit(self.image, self.rect)
Urh
  • 187
  • 1
  • 1
  • 12