Python Pygame动画-索引器错误:列表索引超出范围

Python Pygame动画-索引器错误:列表索引超出范围,python,pygame,sprite,Python,Pygame,Sprite,我正在使用pygame为我的游戏创建一个敌人类。我为我的敌人制作了9张图片,我对其进行了动画处理,使其看起来像是敌人在移动。我写下了我的代码,但当我运行它时,它说: 索引器:列表索引超出范围 有人能帮我弄清楚我必须在代码中修改什么吗?先谢谢你。错误位于行self.image=self.imagesright[self.frame//self.ani]处 这是我的敌人课: 类敌人(pygame.sprite.sprite): ''' 滋生敌人 ''' 定义初始(自身、敌人列表): pygame.s

我正在使用pygame为我的游戏创建一个敌人类。我为我的敌人制作了9张图片,我对其进行了动画处理,使其看起来像是敌人在移动。我写下了我的代码,但当我运行它时,它说:

索引器:列表索引超出范围

有人能帮我弄清楚我必须在代码中修改什么吗?先谢谢你。错误位于行
self.image=self.imagesright[self.frame//self.ani]

这是我的敌人课:

类敌人(pygame.sprite.sprite):
'''
滋生敌人
'''
定义初始(自身、敌人列表):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
自我健康=50
self.frame=0
self.alpha=(0,0,0)
self.ani=2个动画周期
self.敌军名单=敌军名单
自我添加(自我敌人列表)
self.counter=0#计数器变量
self.imagesleft=[]
self.imagesright=[]
对于范围(1,10)内的i:
img=pygame.image.load(os.path.join('images','Bot'+str(i)+'.png')).convert()
img.convert_alpha()
图像设置颜色键(self.alpha)
self.imagesleft.append(img)
self.image=self.imagesleft[0]
self.rect=self.image.get_rect()
对于范围(1,10)内的i:
img=pygame.image.load(os.path.join('images','Bot'+str(i)+'.png')).convert()
img=pygame.transform.flip(img,True,False)
img.convert_alpha()
图像设置颜色键(self.alpha)
self.imagesright.append(img)
self.image=self.imagesright[0]
self.rect=self.image.get_rect()
def移动(自我):
'''
敌军运动
'''
距离=30
速度=10
如果self.counter>=0且self.counter 9*self.ani:
self.frame=0
self.image=self.imagesright[self.frame//self.ani]
elif self.counter>=距离和self.counter 9*self.ani:
self.frame=0
self.image=self.imagesleft[self.frame//self.ani]
其他:
self.counter=0
self.counter+=1
def更新(自我、dt、所有精灵):
bullet\u list=pygame.sprite.spritecollide(self,all\u sprite,True)
对于项目符号列表中的项目符号:
自我健康-=10
打印(自我健康)

如果self.health
self.frame
具有值[0,18]

self.ani
的值为2

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

因此:

self.frame//self.ani
0
(0//2)到
9
(18//2)获取值


9
超出了此列表的范围。

您有一行代码,上面写着
self.image=self.imagesright[self.frame//self.ani]
;也就是说,它尝试使用值
self.frame//self.ani
索引到
self.imagesright
(这是一个列表)。这导致了索引器错误,也就是说,与索引有关的错误。它特别指出列表索引超出范围。所以当错误发生时,是否尝试查看
self.frame//self.ani
的值?您希望此处的有效范围是多少(即,您认为应该使用的最低值和最高值是多少)?非常感谢您的时间。这真的很有帮助!