D
D
Denis Kulikov2021-09-28 20:37:22
Python
Denis Kulikov, 2021-09-28 20:37:22

When playing the animation, it gives a "list index out of range" error. How to fix it?

Creating a school project for assessment in the certificate (a new development of the Ministry of Education) came across the following problem:
The first part of the problematic code

class Explosion(pygame.sprite.Sprite):
  def __init__(self, center, size):
    pygame.sprite.Sprite.__init__(self)
    self.size = size 
    self.image = explosion_anim[self.size][0]
    self.rect = self.image.get_rect()
    self.rect.center = center 
    self.frame = 0
    self.last_update = pygame.time.get_ticks()
    self.frame_rate = 50
  def update(self):
    now = pygame.time.get_ticks()
    if now - self.last_update > self.frame_rate:
      self.last_update = now
      self.frame += 1
      if self.frame == len(explosion_anim[self.size]):
        self.kill
      else:
        center = self.rect.center
        self.image = explosion_anim[self.size][self.frame]
        self.rect = self.image.get_rect()
        self.rect.center = center

The second part of the problematic code
explosion_anim = {}
explosion_anim['lg'] = []
explosion_anim['sm'] = []
for i in range(9):
  filename = 'regularExplosion0{}.png'.format(i)
  img = pygame.image.load(path.join(expl_dir, filename)).convert()
  img.set_colorkey(BLACK)
  img_lg = pygame.transform.scale(img, (75, 75))
  explosion_anim['lg'].append(img_lg)
  img_sm = pygame.transform.scale(img, (32, 32))
  explosion_anim['sm'].append(img_sm)

The third part of the problematic code
hitsMob = pygame.sprite.groupcollide(mobs, bullets, True, True)
  for hit in hitsMob:
    score += 50 - hit.radius
    random.choice(expl_sounds).play()
    expl = Explosion(hit.rect.center, 'lg')
    all_sprites.add(expl)
    newmob()
  hits = pygame.sprite.spritecollide(player, mobs, True, pygame.sprite.collide_circle)
  for hit in hits:
    player.shield -= hit.radius * 2
    expl = Explosion(hit.rect.center, 'sm')
    all_sprites.add(expl)
    newmob()
    if player.shield < 0:
      if score > maxScore:
        saveMaxScore(score)
      running = False

According to the lines of the interpreter, the error is in the line self.image = explosion_anim[self.size][self.frame].
Error type: IndexError.
Reason: list index out of range (not strong in English, but it seems to be written that the index is out of range).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-09-28
@Artin24

Reason: list index out of
range

Well, yes, you are out of range. In python, indexing from scratch is the reason for this.
In general, output to the console (or debug it) and look at the sizes of explosion_anim and size with frame.
Also, as a quick guess - it's not clear what's in kill (is it a method? There are definitely no brackets needed there? It doesn't look like a method call yet), but maybe update continues to spin after it? And the check for going out of bounds will not be performed - there is a strict equal

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question