How do I rotate an image around its center using Pygame?

后端 未结 6 1522
[愿得一人]
[愿得一人] 2020-11-22 00:13

I had been trying to rotate an image around its center in using pygame.transform.rotate() but it\'s not working. Specifically the part that hangs is rot_i

6条回答
  •  猫巷女王i
    2020-11-22 01:06

    You are deleting the rect that rotate creates. You need to preserve rect, since it changes size when rotated.

    If you want to preserve the objects location, do:

    def rot_center(image, angle):
        """rotate a Surface, maintaining position."""
    
        loc = image.get_rect().center  #rot_image is not defined 
        rot_sprite = pygame.transform.rotate(image, angle)
        rot_sprite.get_rect().center = loc
        return rot_sprite
    
        # or return tuple: (Surface, Rect)
        # return rot_sprite, rot_sprite.get_rect()
    

提交回复
热议问题