0
votes

I have a pygame sprite animation where the position and shape of the sprite changes throughout a certain animation sequence (eg. - attack). How do I adjust the sprite's rect for collisions?

My Image: in one part of the animation, my sprite is at the center of a png image file (96x64 pixels). In another part of the animation, my sprite is near the right side of the png image file (96x64 pixels). In another part of the animation, the sprite changes shape (caused by sword movement). Etc.

Do I have to do some math and calculation to adjust the collision rect. of the sprite each frame of the animation or is there an easier method? I am new to pygame so I'm not sure of all the different features and am not sure where to begin with creating the different rects. for each frame.

PS. I am using a ready-made sprite I found online - each frame is 96x64 pixels in dimension

Frame 1, 11, 22:

enter image description here enter image description here enter image description here

1

1 Answers

0
votes

It's been a while since I've worked with Pygame but I believe you can use a pygame.mask object. A mask is basically an object with the precise outline of the foreground of an image. It takes considerably more computation power but if you only create a mask for the one main character then it should be okay. You can create a mask from the image:

myMask = pygame.mask(myImage)

Then you can either use its rect or create a new surface to use:

myRect = myMask.get_rect()  # Returns the pygame.Rect object that conatains mask, or:
mySurface - myMask.to_surface()  # Returns a pygame.Surface object of the mask

Hope this helps, my info is from:

https://www.pygame.org/docs/ref/mask.html