0

So I made an animation for my player and I was wondering how to make it play once, I have tried this code

           if self.anim_index == 5:
               self.direction = "idle"

but it is not working, I'm trying make it so that the player animation plays once then goes back to its idle position depending on the way it is facing.

This is were I wrote the code

class Player:
    def __init__(self,x,y,width,height,color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.speed = 5
        self.isJump = False
        self.JumpCount = 10
        self.idle =[pygame.image.load("player_idel_1.png"),
                            pygame.image.load("player_idel_2.png"),
                            pygame.image.load("player_idel_3.png"),
                            pygame.image.load("player_idel_4.png"),
                            pygame.image.load("player_idel_5.png"),
                            pygame.image.load("player_idel_6.png"),
                            pygame.image.load("player_idel_7.png"),
                            pygame.image.load("player_idel_8.png")
                            ]

        self.idlel = [pygame.image.load("Player_idel_left1.png"),
                         pygame.image.load("Player_idel_left2.png"),
                         pygame.image.load("Player_idel_left3.png"),
                         pygame.image.load("Player_idel_left4.png"),
                         pygame.image.load("Player_idel_left5.png"),
                         pygame.image.load("Player_idel_left6.png"),
                         pygame.image.load("Player_idel_left7.png"),
                         pygame.image.load("Player_idel_left8.png")
                         ]
        self.right = [pygame.image.load("Player_walk_right1.png"),
                      pygame.image.load("Player_walk_right2.png"),
                      pygame.image.load("Player_walk_right3.png")]
        
        self.left = [pygame.image.load("Player_walk_left1.png"),
                     pygame.image.load("Player_walk_left2.png"),
                     pygame.image.load("Player_walk_left3.png")]

        self.jab = [pygame.image.load("Player_stab1.png"),
                    pygame.image.load("Player_stab2.png"),
                    pygame.image.load("Player_stab3.png"),
                    pygame.image.load("Player_stab4.png"),
                    pygame.image.load("Player_stab5.png")]

        self.ljab = [pygame.image.load("Player_lstab1.png"),
                     pygame.image.load("Player_lstab2.png"),
                     pygame.image.load("Player_lstab3.png"),
                     pygame.image.load("Player_lstab4.png"),
                     pygame.image.load("Player_lstab5.png")]

        self.jump1 = [pygame.image.load("Player_jump5.png")]

        
        self.jump2 = [
                     pygame.image.load("Player_rjump1.png")]
        

        self.fall = 0
        self.rect = pygame.Rect(x,y,height,width)
        self.idle = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.idle]
        self.idlel = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.idlel]
        self.right = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.right]
        self.left = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.left]
        self.jump1 = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.jump1]
        self.jump2 = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.jump2]
        self.jab = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.jab]
        self.ljab = [pygame.transform.scale(image,(image.get_width()*2,image.get_height()*2)) for image in self.ljab]
        self.fps = 10
        self.clock = pygame.time.Clock()
        self.anim_index = 0
        self.direction = "idle"
        self.direction = "right"
        self.direction = "left"
        self.dierection = "idleleft"
        self.dierection = "jump1"
        self.dierection = "jump2"
        self.direction = "jab"
        self.direction = "ljab"
        self.next_frame_time = 0
    def get_rect(self):
        self.rect.topleft = (self.x,self.y)
        return self.rect
        pygame.draw.rect(self.color,self.rect)
    
    def draw(self):
        if self.direction == "idle":
            image_list = self.idle
        if self.direction == "right":
            image_list = self.right
        if self.direction == "left":
            image_list = self.left
        if self.direction == "idlel":
            image_list = self.idlel
        if self.direction == "jump1":
            image_list = self.jump1
        if self.direction == "jump2":
            image_list = self.jump2
        if self.direction == "jab":
            image_list = self.jab
        if self.direction == "ljab":
            image_list = self.ljab

        for image in self.jab:
            if self.anim_index == 5:
                self.direction = "idle"

My full code https://pastebin.com/vUdvGMS5

Window man
  • 125
  • 8
  • Might not make a difference, but it seems you have some mispellings in your code: `self.dierection` – Halmon Nov 01 '20 at 00:46
  • 1
    So you want to, for example, have the animation go through all the "stab" images, and then return to idle? What should happen if, say the player starts moving half-way through a "stab", etc.? it might be worthwhile sitting down with a pencil and paper, and working out all the different combinations and paths through the animation code. Maybe make a flow chart. – Kingsley Nov 01 '20 at 04:17
  • 1
    Why didn't you take my advice that I gave in answer to your previous question? [Enemy not staying at original spot](https://stackoverflow.com/questions/64540452/enemy-not-staying-at-original-spot). Your code is still too complicated. – Rabbid76 Nov 01 '20 at 06:46
  • I did, but then I had a last minute change of plans of what kind of game I wanted to make. – Window man Nov 03 '20 at 18:19

0 Answers0