Flipping a sprite in pygame

Lee Jones :

I am new to pygame and trying to learn the ropes, so forgive me if this is a noob question. I have a set of sprites I downloaded off the Internet to practice and I am using a basic walk code to animate the character walking left and right. The images I am using are all facing right, so I am using the pygame.transform.flip function to rotate the image when the character is moving left. Here is a sample of the code:

if self.left and not self.isJump:
        win.blit(pygame.transform.flip(self.walk[self.walkCount],True,False), (self.x,self.y))
        self.walkCount += 1
    elif self.right and not self.isJump:
        win.blit(self.walk[self.walkCount], (self.x,self.y))
        self.walkCount += 1

The problem is, the image has a bunch of blank space around it. The image below is an example (I drew the border just so you can see, the real ones are transparent png files). When the image flips, it flips along the right edge, so the character flips to the other side of the image. So instead of turning in place, the character jumps back and forth on the screen. Is there a way to handle this besides going through and cropping all the blank space in all my images?

enter image description here

Kitso :

Unfortunately cropping blank space is the best course of action as this will likely cause collision issues later into the game, so it is best to just try to remove as much of the blank space as possible. Trust me I know from experience.

If however you do wish to keep the blank space one possible solution would be to make the actual sprite itself move a set distance corresponding to the amount of blank space to remove the feel of it jumping about suddenly but as said before it will cause collision problems.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=13873&siteId=1