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 ?