TypeError: 'NoneType' object is not subscriptable when importing image

Viewed 26

So i am coding a game using pygame. I have a class called Player with a function called import_assets() where i import the names of my file and folder to import the player graphics to my game. Then at the beggining of this file i call this function and then give the value of an image to a variable called self.status and a value for the index called self.index. I then setup those informations to create an image for my player. this class goes like :

import pygame
from settings import *
from support import *

'class Player(pygame.sprite.Sprite):
       def __init__(self, pos, group):
           super().__init__(group)

       self.import_assets()
       self.status = 'left_water'
       self.frame_index = 0

       # general setup
       self.image = self.animations[self.status][self.frame_index]
       self.rect = self.image.get_rect(center = pos)

       # movement attributes
       self.direction = pygame.math.Vector2()
       self.pos = pygame.math.Vector2(self.rect.center)
       self.speed = 200

   def import_assets(self):
       self.animations = {'up': [],'down': [],'left': [],'right': [],
                       'right_idle':[],'left_idle':[],'up_idle':[],'down_idle':[],
                       'right_hoe':[],'left_hoe':[],'up_hoe':[],'down_hoe':[],
                       'right_axe':[],'left_axe':[],'up_axe':[],'down_axe':[],
                       'right_water':[],'left_water':[],'up_water':[],'down_water':[]}

       for animation in self.animations.keys():
           full_path = '../graphics/character/' + animation
           self.animations[animation] = import_folder(full_path)

   def input(self):
       keys = pygame.key.get_pressed()

       if keys[pygame.K_UP]:
           self.direction.y = -1
       elif keys[pygame.K_DOWN]:
           self.direction.y = 1
       else:
           self.direction.y = 0

       if keys[pygame.K_RIGHT]:
           self.direction.x = 1
       elif keys[pygame.K_LEFT]:
           self.direction.x = -1
       else:
           self.direction.x = 0

   def move(self,dt):

       # normalizing a vector 
       if self.direction.magnitude() > 0:
           self.direction = self.direction.normalize()

       # horizontal movement
       self.pos.x += self.direction.x * self.speed * dt
       self.rect.centerx = self.pos.x

       # vertical movement
       self.pos.y += self.direction.y * self.speed * dt
       self.rect.centery = self.pos.y

   def update(self, dt):
       self.input()
       self.move(dt)

I am also using a class called support to help me loop trought my image folder and import the good path for the image. it goes like this :

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

My problemseems to be coming from self.image = self.animations[self.status][self.frame_index] that when i try to run my game im getting the error :

TypeError: 'NoneType' object is not subscriptable

Any idea how to solve this ?

2 Answers

I think your import_assets function is in the wrong indentation.

Fix it by adding 1 indent to the whole function code (with the def line) to make the function belong to class Player

The problem is probably with your import_folder return value. If walk(path) returns an empty iterable, then your import_folder function will have a return value of None instead of a list.

I'm not entirely sure with your foldering system, but perhaps any of the below codes could solve your issue. They have different functions depending on what you really intend to do, so you may want to try both of them.

Solution 1:

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
    return [] # default return

Solution 2:

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
Related