Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何让动画一次播放一次_Python_Python 3.x_Pygame - Fatal编程技术网

Python 如何让动画一次播放一次

Python 如何让动画一次播放一次,python,python-3.x,pygame,Python,Python 3.x,Pygame,所以我为我的播放器制作了一个动画,我想知道如何让它播放一次,我已经尝试了这个代码 if self.anim_index == 5: self.direction = "idle" 但它不起作用,我正在尝试使它使播放器动画播放一次,然后根据它面对的方式返回其空闲位置 这是我写的代码 class Player: def __init__(self,x,y,width,height,color): sel

所以我为我的播放器制作了一个动画,我想知道如何让它播放一次,我已经尝试了这个代码

           if self.anim_index == 5:
               self.direction = "idle"
但它不起作用,我正在尝试使它使播放器动画播放一次,然后根据它面对的方式返回其空闲位置

这是我写的代码

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"
我的完整代码

可能不会有什么不同,但您的代码中似乎有一些拼写错误:
self.dierection
因此,您希望,例如,让动画遍历所有“stab”图像,然后返回空闲状态?如果,比如说,玩家开始在“刺杀”的中途移动,会发生什么。?拿着铅笔和纸坐下来,通过动画代码计算出所有不同的组合和路径可能是值得的。也许做一个流程图。你为什么不采纳我在回答你之前的问题时给出的建议呢。你的代码还是太复杂了。我做了,但我在最后一刻改变了我想做什么样的游戏的计划。