[Python] Animation not working pygame, pygame.sprite.Sprite

Discussão em 'Python' iniciado por Stack, Janeiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    i want to run a separate animation for an ability then the main one because i have a separate character that i use both for enemies and hero, but i can't figure why the separate ability animation won't start

    see the code @@@@@@@@@

    class Slash(pygame.sprite.Sprite):
    def __init__(self, x, y, direction):
    pygame.sprite.Sprite.__init__(self)
    self.speed = 7
    self.image = slash_img
    self.rect = self.image.get_rect()
    self.rect.center = (x, y)
    self.direction = direction

    def update(self):
    #move slash
    self.rect.x += (self.direction * self.speed)
    #check if slash has gone off screen
    if self.rect.right < 0 or self.rect.left > SCREEN_WIDTH:
    self.kill()

    #check collision with characters
    if pygame.sprite.spritecollide(enemy, slash_group, False):
    if enemy.alive:
    enemy.health -= 25
    self.kill()

    class Slash_attack(pygame.sprite.Sprite):
    def __init__(self, x, y, direction, scale):
    pygame.sprite.Sprite.__init__(self)
    self.update_time = pygame.time.get_ticks()
    self.images = []
    for num in range(1, 5):
    img = pygame. image.load(f'img/slash/slash{num}.png').convert_alpha()
    img = pygame.transform.scale(img, (int(img.get_width() * scale), int(img.get_height() * scale)))
    self.images.append(img)
    self.frame_index = 0
    self.image = self.images[self.frame_index]
    self.rect = self.image.get_rect()
    self.rect.center = (x, y)
    self.counter = 0


    def update(self):
    #update animation
    SLASH_COOLDOWN = 100
    #update image depending on the current frame
    self.image = self.images[self.frame_index]
    # If the animation has run out, reset to the start or keep at the last frame
    self.counter += 1
    if self.counter >= SLASH_COOLDOWN:
    self.counter = 0
    self.frame_index += 1

    # If the animation has finished, kill the slash
    if self.frame_index >= len(self.images):
    self.kill() # remove the slash attack sprite
    else:
    self.frame_index = 0

    Continue reading...

Compartilhe esta Página