python - Pygame 动画 - IndexError : list index out of range

标签 python pygame sprite

我正在使用 pygame 为我的游戏创建一个敌人类。我为敌人准备了 9 个图像,并对其进行了动画处理,以便敌人看起来实际上在移动。我写下了我的代码,但是当我运行它时,它说:

IndexError: list index out of range

任何人都可以帮我弄清楚我必须在代码中更改什么吗?预先感谢。错误出现在 self.image = self.imagesright[self.frame//self.ani] 行。

这是我的敌人类别:

class Enemy(pygame.sprite.Sprite):
    '''
    Spawn an enemy
    '''
    def __init__(self, enemy_list):
        pygame.sprite.Sprite.__init__(self)
        self.health = 50
        self.frame = 0
        self.alpha = (0,0,0)
        self.ani = 2 # animation cycles
        self.enemy_list = enemy_list
        self.add(self.enemy_list)
        self.counter = 0 # counter variable
        self.imagesleft = []
        self.imagesright = []
        for i in range(1,10):
            img = pygame.image.load(os.path.join('images','Bot' + str(i) + '.png')).convert()
            img.convert_alpha()
            img.set_colorkey(self.alpha)
            self.imagesleft.append(img)
            self.image = self.imagesleft[0]
            self.rect  = self.image.get_rect()
        for i in range(1,10):
            img = pygame.image.load(os.path.join('images','Bot' + str(i) + '.png')).convert()
            img = pygame.transform.flip(img, True, False)
            img.convert_alpha()
            img.set_colorkey(self.alpha)
            self.imagesright.append(img)
            self.image = self.imagesright[0]
            self.rect  = self.image.get_rect()

    def move(self):
        '''
        enemy movement
        '''
        distance = 30
        speed = 10

        if self.counter >= 0 and self.counter <= distance:
            self.rect.x += speed
            self.frame += 1
            if self.frame > 9*self.ani:
                self.frame = 0
            self.image = self.imagesright[self.frame//self.ani]

        elif self.counter >= distance and self.counter <= distance*2:
            self.rect.x -= speed
            self.frame += 1
            if self.frame > 9*self.ani:
                self.frame = 0
            self.image = self.imagesleft[self.frame//self.ani]
        else:
            self.counter = 0

        self.counter += 1

    def update(self, dt, all_sprites):
        bullet_list = pygame.sprite.spritecollide(self, all_sprites, True)
        for bullets in bullet_list:
            self.health -= 10
            print(self.health)
        if self.health <= 0:
            self.kill()

最佳答案

self.frame 的值为 [0, 18]

self.ani 的值为 2

self.imagesright 有 9 个元素(值从 0-8)

因此:

self.frame//self.ani 的值从 0 (0//2) 到 9 (18//2) .

9 超出此列表的范围。

关于python - Pygame 动画 - IndexError : list index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62938242/

相关文章:

c# - 构建 2D XNA 游戏引擎

python - 如何用numpy找到最小非零元素的索引?

python - pygame音频错误无法识别的音频格式

python - PyGame 中的无效颜色参数

python - 有没有办法将html嵌入到pygame中

java - 将多个 Sprite 作为一个旋转(围绕同一原点)

c++ - getLocalBounds 替代文本对象? (SFML)

python - 当我运行 pygame 时,它​​不会加载我的游戏?

python - 将时间戳转换为月份名称和年份作为数据框列名称

python - 生产中未调用错误处理程序