0

I have an interesting problem with sprits in pygame.

Essentially, I am trying to create an archer sprite which will follow a target. To do so, I need it to rotate. Naturally, I don't want the whole archer to rotate - just his shoulders/head etc. To do so I have create two separate spritesheets (for the top and bottom) I can then create the sprite image using:

class Archer(pygame.sprite.Sprite):
    def __init__(self, group, pos, size):
        super().__init__(group)
        
        self.pos = pos
        self.assets_legs = GetAssets('../graphics/archer_fixed.png', size)
        self.frames_legs = self.assets_legs.sprite_images
        self.assets_body = GetAssets('../graphics/archer_rotate.png', size)
        self.frames_body = self.assets_body.sprite_images

        self.frame_index = 0
        self.angle = 0
        
        self.archer = [self.frames_legs[round(self.frame_index)],     self.frames_body[round(self.frame_index)]]
        self.image = pygame.Surface((size), pygame.SRCALPHA)
        for image in self.archer:
            self.image.blit(image, (0,0))

This is fine and works well. However, when I want to rotate the top half and blit this to the self.image, as in:

def rotate(self):
        self.rot_img = self.archer[1]
        
        self.rot_img = pygame.transform.rotate(self.rot_img, 1)
        
        self.image = pygame.Surface((75,75), pygame.SRCALPHA)
        self.archer = [self.frames_legs[round(self.frame_index)], self.rot_img]
        for image in self.archer:
            self.image.blit(image, (0,0))
        
        self.rect = self.image.get_rect(center = self.pos)

I get strange artifacts. I suspect because I am trying to blit a surface with a rotated dimension to a self.image. The only work around I can think of is to create two separate sprites and then have them track each other, but the must be a way to do this in one class.

Any ideas?

Cheers

MWallace
  • 59
  • 6
  • You need to keep the original image and create a new rotated image. The reason for the artifacts that occur are well explained in the answer to [How do I rotate an image around its center using PyGame?](https://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame/54714144#54714144) – Rabbid76 Jan 29 '23 at 17:16
  • Sorry, didn't clarify, I do always use the original image, I am aware of the of quality when rotating. What happens is the rotating head 'bounces' of the edge of the image rectangle. As though the rotated rectangle for one image is squeezing badly into the drawn self.image – MWallace Jan 29 '23 at 17:25
  • `self.archer = [self.frames_legs[round(self.frame_index)], self.rot_img]` manipulates the original image. So you don't always use the original image. Read the answers to the duplicate questions. – Rabbid76 Jan 29 '23 at 17:43
  • What you have to do is `self.angle += 1` `self.rot_img = pygame.transform.rotate(self.original_img, self.angle)` instead of `self.rot_img = pygame.transform.rotate(self.rot_img, 1)` – Rabbid76 Jan 29 '23 at 17:47
  • Never mind. As I said I know about using the org image. The code I put is old. The issue was about bliting a rotated ans non rotated image to another blank one. I will just create two sprites separately – MWallace Jan 29 '23 at 18:40
  • You have inserted an old code into the question? How can we help you after the information in the question is wrong? – Rabbid76 Jan 29 '23 at 19:36

0 Answers0