列表索引超出范围错误 Pygame 如何修复?


window.blit(self.esright[self.Walking//3], (self.x,self.y))
IndexError: list index out of range

我不确定如何解决这个问题,我试图修复它,但我不断收到同样的错误

# this makes the enemy move right and left
def draw(self,window):
self.move()
if self.Walking + 1 >= 33:
self.Walking = 0
if self.vel > 0:
window.blit(self.esright[self.Walking//3], (self.x,self.y))
self.Walking += 1
else:
window.blit(self.esleft[self.Walking//3], (self.x,self.y))
self.Walking += 1

敌人的阶级

# ---------------------------------------------- # this for bird my g LOl

class bird:
def __init__(self,x,y,height,width,end):
self.x = x
self.y =y
self.esright = [pygame.image.load("bird1.png"),
pygame.image.load("bird2.png"),
pygame.image.load("bird3.png"),
pygame.image.load("bird4.png")
]
self.esleft = [pygame.image.load("b1.png"),
pygame.image.load("b2.png"),
pygame.image.load("b3.png"),
pygame.image.load("b4.png")
]
self.esright = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.esright]
self.esleft = [pygame.transform.scale(image,(image.get_width()//5,image.get_height()//5)) for image in self.esleft]
self.height = height
self.width = width
self.distance = 80
self.speed = 8
self.vel = 3
self.path = [x,end]
self.Walking = 0
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.rect = pygame.Rect(x,y,height,width)
# enemys health
self.health = 10
self.soko = 0
self.visible = True
# this makes the enemy move right and left
def draw(self,window):

self.move()
if self.Walking + 1 >= 33:
self.Walking = 0
elif self.vel > 0:
window.blit(self.esright[self.Walking//3], (self.x,self.y))
self.Walking += 1
else:
window.blit(self.esleft[self.Walking//3], (self.x,self.y))
self.Walking += 1
# this moves the enemy left and right
def move(self):
if self.visible:
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.Walking = 0
else:
if self.x - self.vel >  self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.Walking = 0
# the hit box for the enemy the health
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 70, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 20, 70 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 47, self.y + 31, 50, 72)

# THIS PART MAKES  the enemy not scroll with the player
def scroll(self,sx, sy):
self.x += sx
self.y += sy
self.path[0] += sx
self.path[1] += sx

# define the enemy class
black = (0,0,0)
bird1 = bird(300,199,104,64,500)
birds = [bird1]

fefesfe

如果您希望在每次迭代中计算self.Walking//3,请将它更改为self.Walking % 4
,例如:
0 1 2 3 0 1 2 3 0 1...etc

如果您希望在每次迭代时计算self.Walking//3,请将更改为int(self.Walking / 4) % 4
,例如:0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 0 0 0 0 1 1 1 1...etc

尝试使你的第二个if语句成为elif的一部分,我想像这样

def draw(self,window):
self.move()
if self.Walking + 1 >= 33:
self.Walking = 0
elif self.vel > 0:
window.blit(self.esright[self.Walking//3], (self.x,self.y))
self.Walking += 1
else:
window.blit(self.esleft[self.Walking//3], (self.x,self.y))
self.Walking += 1

或者尝试将 self.move(( 缩进到 if、elif、else 语句

最新更新