0

It says that the list index is out of range, and I don't know why. Does anyone know how to fix the problem?

Code for thing I'm importing

from os import walk
import pygame

def import_folder(path):
    surface_list = []

    for _, __, img_files in walk(path):
        for image in img_files:
            full_path = path + '/' + image
            image_surf = pygame.image.load(full_path).convert_alpha()
            surface_list.append(image_surf)

    return surface_list

Code for thing I'm using it in:

import pygame
from support import import_folder

class Player(pygame.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()
        self.import_character()
        self.frame_index = 0
        self.animation_speed = 0.15
        self.image = self.animations['run'][self.frame_index]
        
        # Player Movement

        self.direction = pygame.math.Vector2(0, 0)
        self.speed = 8
        self.gravity = 0.8
        self.jump_speed = -16

    def import_character(self):
        character_path = '../graphics/character/'
        self.animations = {'idle':['idle'], 'run':[], 'jump':[], 'fall':[]}

        for animation in self.animations.keys():
            full_path = character_path + animation
            self.animations[animation] = import_folder(full_path)

Full Error Code (starting from Traceback):

Traceback (most recent call last):
File "C:\Users\Daniel\Desktop\PlatformerGame.py", line 
13, in <module>
level = level(level_map, window)
File "C:\Users\Daniel\Desktop\level.py", line 11, in 
__init__
self.setup_level(level_data)
File "C:\Users\Daniel\Desktop\level.py", line 27, in 
setup_level
player_sprite = Player((x, y))
File "C:\Users\Daniel\Desktop\player.py", line 10, in 
__init__
self.image = self.animations['run'][self.frame_index]
IndexError: list index out of range
Park
  • 2,446
  • 1
  • 16
  • 25
Daniel
  • 15
  • 1
  • 7

3 Answers3

0

The frame_index becomes too large compared to the number of images for the "run" animation.

Try something like:

self.image = self.animations['run'][self.frame_index % len(self.animations['run'])]
Michael Butscher
  • 10,028
  • 4
  • 24
  • 25
0
if len(self.animations['run'])>0:
   self.animations['run'][self.frame_index]
else:
   condition
            
  • 2
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Feb 27 '22 at 02:07
0

Your image files are not loaded properly. If the file path is not correct wrong, walk(path) returns nothing.
It is not enough to put the files in the same directory or sub directory of the project. You also need to set the working directory. The image file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script.
The name and path of the file can be retrieved with __file__ and the current working directory can be changed with os.chdir(path).

Add the following at the beginning of the main code to set the working directory to the same as the main script's directory:

import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174