ihave an index error list index out of range can somone help me

Started by
3 comments, last by Alberth 2 years, 4 months ago

i programing at the moment a rpg but the animation is not working becous i get this error

here is the code:

class Attack(pygame.sprite.Sprite):

def __init__(self, game, x, y,):


self.game = game

self._layer = PLAYER_LAYER

self.groups = self.game.all_sprites, self.game.attacks

pygame.sprite.Sprite.__init__(self, self.groups)


self.x = x

self.y = y

self.width = TILESIZE

self.height = TILESIZE


self.animation_loop2 = 0


self.image = self.game.attack_spritesheet.get_sprite(0, 0, self.width, self.height)


self.rect = self.image.get_rect()

self.rect.x = self.x

self.rect.y = self.y


def colide(self):

hits = pygame.sprite.spritecollide(self, self.game.enemies, True)


def update(self):

self.animate()

self.colide()

def animate(self):

diretion = self.game.player.facing


right_animations = [self.game.attack_spritesheet.get_sprite(0, 64, self.width, self.height),

self.game.attack_spritesheet.get_sprite(32, 64, self.width, self.height),

self.game.attack_spritesheet.get_sprite(64, 64, self.width, self.height),

self.game.attack_spritesheet.get_sprite(96, 64, self.width, self.height),

self.game.attack_spritesheet.get_sprite(128, 64, self.width, self.height)]


down_animations = [self.game.attack_spritesheet.get_sprite(0, 32, self.width, self.height),

self.game.attack_spritesheet.get_sprite(32, 32, self.width, self.height),

self.game.attack_spritesheet.get_sprite(64, 32, self.width, self.height),

self.game.attack_spritesheet.get_sprite(96, 32, self.width, self.height),

self.game.attack_spritesheet.get_sprite(128, 32, self.width, self.height)]


left_animations = [self.game.attack_spritesheet.get_sprite(0, 96, self.width, self.height),

self.game.attack_spritesheet.get_sprite(32, 96, self.width, self.height),

self.game.attack_spritesheet.get_sprite(64, 96, self.width, self.height),

self.game.attack_spritesheet.get_sprite(96, 96, self.width, self.height),

self.game.attack_spritesheet.get_sprite(128, 96, self.width, self.height)]


up_animations = [self.game.attack_spritesheet.get_sprite(0, 0, self.width, self.height),

self.game.attack_spritesheet.get_sprite(32, 0, self.width, self.height),

self.game.attack_spritesheet.get_sprite(64, 0, self.width, self.height),

self.game.attack_spritesheet.get_sprite(96, 0, self.width, self.height),

self.game.attack_spritesheet.get_sprite(128, 0, self.width, self.height)]


if diretion =="up":

self.image = up_animations[math.floor(self.animation_loop2)]

self.animation_loop2 += 0.5

if self.animation_loop2 >= 5:

self.kill


if diretion =="down":

self.image = down_animations[math.floor(self.animation_loop2)]

self.animation_loop2 += 0.5

if self.animation_loop2 >= 5:

self.kill


if diretion =="left":

self.image = left_animations[math.floor(self.animation_loop2)]

self.animation_loop2 += 0.5

if self.animation_loop2 >= 5:

self.kill


if diretion =="right":

self.image = right_animations[math.floor(self.animation_loop2)]

self.animation_loop2 += 0.5

if self.animation_loop2 >= 5:

self.kill

if you have any questions just ask

Advertisement

In short, I believe that the error indicates an attempt to access a list via an index larger (or more negative) than the list is long. For example, an attempt to access item three in a list that only has two items.

Which line does the error indicate produces the error? (Perhaps it would be helpful for us to see the full error-text.)

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Without proper indentation it's difficult to see what you're trying to do.
Use code blocks like this:

def sayHello():
  # Hello there
  print("Hello!")
if self.animation_loop2 >= 5:
  self.kill

I am guessing you expect this to kill the character, but you only say the name of the function, it never gets executed (you need to append a pair of parenthesis to it for that, ie “self.kill()”), so the next loop you are trying to get the 6th sprite from the animation which doesn't exist.

You can verify such things by adding a statement like ‘print(”Inside kill")’ in self.kill so you get feedback that the computer is performing the code.

EDIT: Your direction handling can be much shorter:

if diretion =="down":
   self.image = down_animations[math.floor(self.animation_loop2)]
elif diretion =="up":
   self.image = up_animations[math.floor(self.animation_loop2)]
elif diretion =="left":
   self.image = left_animations[math.floor(self.animation_loop2)]
elif diretion =="right":
   self.image = right_animations[math.floor(self.animation_loop2)]
   
self.animation_loop2 += 0.5
if self.animation_loop2 >= 5:
   self.kill

This topic is closed to new replies.

Advertisement