Python Forum
[PyGame] Rotation Problem in PyGame - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [PyGame] Rotation Problem in PyGame (/thread-22465.html)



Rotation Problem in PyGame - thunderbird028 - Nov-14-2019

I tried to recreate Flappy Bird using pygame and everything works properly except for the rotation of the Flappy Bird.

The rotated image increases the size of the rect of sprite which it makes detecting the collision of the bird with the pipes difficult.
Here is my rotation function of the bird sprite:

def rotation(self, dt):
        """
            Rotates the bird as per its position
        :param dt: for counting frames
        :return: None
        """
        if self.isJump and not self.isUp:
            old_center = self.rect.center
            for i in range(3):
                self.group_images[i] = self.rotated_group_images[i]
            self.image = self.group_images[self.image_index]
            self.rect = self.image.get_rect()
            self.rect.center = old_center
            self.isUp, self.isDown = True, False

        if self.isUp and self.isJump:
            self.jump_height = self.rect.y

        if not self.isJump and not self.isDown:
            if self.rect.y > self.jump_height:
                if self.rotated >= self.max_rotation:
                    self.rotation_time += dt
                    if self.rotation_time >= self.rotation_animation_time:
                        self.isUp = False
                        old_center = self.rect.center
                        for i in range(3):
                            self.group_images[i] = pygame.transform.rotate(self.group_images[i], self.rotate)
                        self.image = self.group_images[self.image_index]
                        self.rect = self.image.get_rect()
                        self.rect.center = old_center
                        self.rotated += self.rotate
                        if self.rotated == self.max_rotation:
                            self.isDown = True
                            self.rotated = 0
Here is what the game looks like. I have drawn the rect of the sprite to show its size:

[Image: ETrlk4p]
[Image: Hsh1oqY]
[Image: oVLTK5q]

I have changed the new rect with the old rect's center but still its size is getting bigger.

Here is a link to my project:
https://github.com/pranav-28/FlappyBird


RE: Rotation Problem in PyGame - Windspar - Nov-14-2019

You must rotate from original image. Otherwise there will be side effects. Like image will degrade the more you rotate it.
You can use pygame mask. Otherwise keep original rect for collision.