Home > OS >  Enemy sprite will not flip left
Enemy sprite will not flip left

Time:12-23

I have a sprite that traverses left and right using the following function below. When it flips left, it flips the img for 1 frame it seems and then stays pointing right while moving left. Same 1 frame flip happens at the other side too. With a single image this works fine, but I have added this animation loop in and now I am stumped.

class Enemy(pygame.sprite.Sprite):#==================================================
    def __init__(self,x,y):   
        pygame.sprite.Sprite.__init__(self)
        self.images_right = []
        self.frame_index = 0
        self.counter = 0
        for num in range (1,8):
            img_right = pygame.image.load(f'img/enemy/{num}.png')   
            self.images_right.append(img_right)
        self.image = self.images_right[self.frame_index]
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.move_direction = 1
        self.move_counter = 0

    def update(self):
        self.counter  = 1
        walk_cooldown = 2
        if self.counter > walk_cooldown:
            self.counter = 0
            self.frame_index  = 1
            if self.frame_index >= len(self.images_right):
                self.frame_index = 0
            self.image = self.images_right[self.frame_index]
        self.rect.x  = self.move_direction
        self.move_counter  = 1
        if abs(self.move_counter) > 32:
            self.move_direction *= -1
            self.image = pygame.transform.flip(self.image, True, False)  
            self.move_counter *= -1

CodePudding user response:

You have a list of images. You're only flipping the last image in the list.

In the constructor, create a list of right and left images and select the image to be displayed in update:

class Enemy(pygame.sprite.Sprite):
    def __init__(self,x,y):   
        pygame.sprite.Sprite.__init__(self)
        
        self.images_right = [pygame.image.load(f'img/enemy/{num}.png') for num in range (1,8)]
        self.images_left = [pygame.transform.flip(img, True, False) for img in self.images_right]
        
        self.frame_index = 0
        self.image = self.images_right[self.frame_index]
        self.rect = self.image.get_rect(topleft = (x, y))   
        self.counter = 0
        self.move_direction = 1
        self.move_counter = 0

    def update(self):
        self.counter  = 1
        walk_cooldown = 2
        if self.counter > walk_cooldown:
            self.counter = 0
            self.frame_index  = 1
            if self.frame_index >= len(self.images_right):
                self.frame_index = 0
        self.rect.x  = self.move_direction
        self.move_counter  = 1
        if abs(self.move_counter) > 32:
            self.move_direction *= -1
            self.move_counter *= -1

        image_list = self.images_right if self.move_direction > 0 else self.images_left    
        self.image = image_list[self.frame_index]
  • Related