简体   繁体   English

在 pygame 中翻转精灵

[英]Flipping a sprite in pygame

I am new to pygame and trying to learn the ropes, so forgive me if this is a noob question.我是 pygame 的新手并试图学习绳索,所以如果这是一个菜鸟问题,请原谅我。 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.我使用的图像都朝右,所以我使用 pygame.transform.flip 函数在角色向左移动时旋转图像。 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).下图是一个示例(我画了边框只是为了让您看到,真实的是透明的 png 文件)。 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?除了遍历和裁剪我所有图像中的所有空白空间之外,还有其他方法可以解决这个问题吗?

在此处输入图片说明

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.但是,如果您确实希望保留空白空间,一个可能的解决方案是使实际精灵本身移动与空白空间量相对应的设定距离,以消除它突然跳跃的感觉,但如前所述,它会导致碰撞问题.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM