0

I'm making RPG game on pygame and one of the skills animation requires rotation on its center, the thing is when it collide with other sprites the collision area changes because of the rotation.

Here is my rotation function:

def rotate(self):
    self.image = pygame.transform.rotozoom(self.image, self.angle, 1)
    self.rect = self.image.get_rect(center=self.rect.center)
    self.angle += 1

And this is the projectile class, which has the rotation function inside it basically if the projectile attribute equals to "big_star" it rotates it.

class Projectile(pygame.sprite.Sprite):
    def __init__(self, x, y, direction, range, isRotate, projectile, damage):
        pygame.sprite.Sprite.__init__(self)
        self.range = range
        self.projectile = projectile
        self.speed = 12
        self.hit_count = 0
        self.damage = damage
        self.isRotate = isRotate
        if direction == 1:
            self.flip = True
        else:
            self.flip = False
        self.image = pygame.image.load(f'sprites/projectiles/{projectile}/0.png').convert_alpha()
        self.image = pygame.transform.flip(self.image, self.flip, False)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.direction = direction
        self.angle = 0

    def update(self, mobs, player):
        #move projectile
        self.rect.x += (self.direction * self.speed)
        if self.isRotate:
            self.rotate()
        for mob in mobs:
            if pygame.sprite.spritecollide(mob, player.projectiles_group, False):
                if mob.alive and self.hit_count != 1:
                    mob.play_sound("mob","hit")
                    mob.health -= 25
                    mob.update_action(3)
                    if self.projectile != 'big_star':
                        self.kill()
                    self.hit_count += 1
        #check if projectile has gone off range
        if self.rect.x > (player.rect.x + self.range) or self.rect.x < (player.rect.x - self.range):
            self.kill()

    def rotate(self):
        self.image = pygame.transform.rotozoom(self.image, self.angle, 1)
        self.rect = self.image.get_rect(center=self.rect.center)
        self.angle += 1

I think the reason for that is this line:

self.rect = self.image.get_rect(center=self.rect.center)

But I don't know how to fix it with out changing the rect, any ideas?

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Hawanity
  • 13
  • 7
  • I'm not sure what you want to do. If you rotate an image, it'll get bigger because the hypotenuse is greater than the width or height. Look at [this](https://gamedev.stackexchange.com/a/127870/89526). So what is the behavior you expect? Also, **don't rotate your original image**. Every rotation will make the image lose quality. Look at the provided link. – Ted Klein Bergman Jan 20 '22 at 10:42
  • Can't you remain using the original rect, why do you have to change it? – The_spider Jan 20 '22 at 20:16

0 Answers0