Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.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 3.x 在pygame中使用self.image和current_frame设置敌人精灵的动画_Python 3.x_Pygame - Fatal编程技术网

Python 3.x 在pygame中使用self.image和current_frame设置敌人精灵的动画

Python 3.x 在pygame中使用self.image和current_frame设置敌人精灵的动画,python-3.x,pygame,Python 3.x,Pygame,我正试图在pygame中为一款视频游戏制作一个特朗普敌人的动画。我有我所有的图像加载,我想让他在随机移动。我很难让敌人特朗普角色的图像动画化。我可以让他的初始帧显示出来,但之后我就迷路了。您将在animate2函数的代码中看到问题区域。 我对如何使用当前的_frame2或self.image更新他的精灵感到困惑。有人知道如何进行吗 class TRUMP(pg.sprite.Sprite): def __init__(self, game): pg.sprite.Spri

我正试图在pygame中为一款视频游戏制作一个特朗普敌人的动画。我有我所有的图像加载,我想让他在随机移动。我很难让敌人特朗普角色的图像动画化。我可以让他的初始帧显示出来,但之后我就迷路了。您将在animate2函数的代码中看到问题区域。
我对如何使用当前的_frame2或self.image更新他的精灵感到困惑。有人知道如何进行吗

class TRUMP(pg.sprite.Sprite):
    def __init__(self, game):
        pg.sprite.Sprite.__init__(self)
        self.game = game
        self.current_frame2 = 0
        self.last_update2 = 0
        self.load_images()
        self.animate2()

        self.image = self.TRUMP_fingers_l[0]
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH *3/4), (589)
        self.pos = vec((WIDTH/2), (HEIGHT/2))
        self.vel = vec(0, 0)
        self.acc = vec(0, 0)

        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        self.clock = pg.time.Clock()

    def load_images(self):

        self.TRUMP_fingers_l = [self.game.spritesheet.get_image(503, 1, 248, 559)]
        for frame in self.TRUMP_fingers_l:
            frame.set_colorkey(BLACK)
        self.TRUMP_walk1_l = [self.game.spritesheet.get_image(643, 562, 249, 448)]
        self.TRUMP_walk2_l = [self.game.spritesheet.get_image(894, 474, 249, 448)]
        self.TRUMP_hailing = [self.game.spritesheet.get_image(1, 571, 248, 439)]
        self.TRUMP_throwing = [self.game.spritesheet.get_image(753, 1, 248, 471)]
        self.TRUMP_smug = [self.game.spritesheet.get_image(252, 1, 249, 562)]
        self.TRUMP_flexing_l = [self.game.spritesheet.get_image(1, 1, 249, 568)]

    def animate2(self):

       now = pg.time.get_ticks()
       if now - self.last_update2 > 200:
            self.last_update2 = now
            self.current_frame2 = self.TRUMP_hailing[0]
            self.image = self.TRUMP_hailing[0]

在我看来,您只需要一个
索引
变量(如果计时器工作正常),您可以通过增加该变量来循环查看图像列表。使用模
%len(self.TRUMP\u hailling)
将索引保持在正确的范围内

    # In the __init__ method:
    self.index = 0

def animate2(self):
    now = pg.time.get_ticks()
    if now - self.last_update2 > 200:
        self.index += 1
        self.index %= len(self.TRUMP_hailing)
        self.last_update2 = now
        self.current_frame2 = self.TRUMP_hailing[self.index]
        self.image = self.TRUMP_hailing[self.index]