我正在尝试使用 pygame 在 python 中获取我的步行动画,但我收到以下错误:
Traceback (most recent call last):
File "C:\Users\name\Desktop\game\game.py", line 37, in <module>
player1 = player()
File "C:\Users\name\Desktop\game\game.py", line 21, in __init__
self.img = pygame.image.load(self.ani[1])
IndexError: list index out of range
这是我的代码:
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
height = 400
width = 800
screen = pygame.display.set_mode((width, height), 0, 32)
class player:
def __init__(self):
self.x = 200
self.y = 300
self.ani_speed_init = 10
self.ani_speed = self.ani_speed_init
self.ani = glob.glob("game\redplayer*.png")
self.ani.sort()
self.ani_pos = 0
self.ani_max = len(self.ani) - 1
self.img = pygame.image.load(self.ani[1])
self.update(0)
def update(self, pos):
if pos != 0:
self.ani_speed -= 1
self.x += pos
if self.ani_speed == 0:
self.img = pygame.image.load(self.ani[self.ani_pos])
self.ani_speed = self.ani_speed_init
if self.ani_pos == self.ani_max:
self.ani_pos = 0
else:
self.ani_pos += 1
screen.blit(self.img,(self.x, self.y))
player1 = player()
pos = 0
我检查了文件名,我不认为是这样,但它可能是