0

I'm trying to make a pygame zelda_like, which can set random rooms and enemies. First, I called the same Enemy_Class for each room, but figured out it modified the overall enemies. So I called the function copy.deepcopy(). Since I had sprites declared in the Enemy_Class, it wouldn't work, so I tried this :

class Enemy(pygame.sprite.Sprite):

def __init__(self, img_list):

    self.animation = 0
    self.img_ref = 'img_list'

    self.animation_list =  []
    self.img = []
    self.ig = []
    self.rect = []
    self.mask = []

Which is copied like this :

room_number[i].enemy1 = copy.deepcopy(enemy_list[n])
lazy_loading(room_number[i].enemy1)

Enemy_list[x] returns a child from the Enemy_Class. And then the lazy_loading function is supposed to fill all :

def lazy_loading(sprite):
    sprite.animation_list = get_lazy_loading(sprite)
    sprite.img = sprite.animation_list[sprite.animation]
    sprite.ig = pygame.transform.scale(sprite.img, (x, y))
    sprite.rect = sprite.ig.get_rect()
    sprite.mask = pygame.mask.from_surface(sprite.ig, 127)

get_lazy_loading returns a list of surfaces. Problem is self.img is considered as a NoneType and python doesn't really want it to be a surface...

So the question is, before I get further in vain : is there a way to make it accept a surface, or should I find a better way to load it in the lazy way ? Thank you very much.

edit* : Error I get is the following one :

sprite.img = sprite.animation_list[sprite.animation]
TypeError: 'NoneType' object is not subscriptable
Dindoleon
  • 1
  • 1
  • How can Python "not want the attribute to be a Surface"? Are you getting an error? Is it just blank? What is happening? If `get_lazy_loading(sprite)` really returns a list of sprites, then `sprite.img` should be a sprite object. – Ted Klein Bergman Sep 04 '16 at 16:02
  • Sorry for this, error is `sprite.img = sprite.animation_list[sprite.animation] TypeError: 'NoneType' object is not subscriptable` – Dindoleon Sep 04 '16 at 16:39
  • Your `get_lazy_loading(sprite)` returns None. You might need to post it as well. – Ted Klein Bergman Sep 04 '16 at 17:00
  • Maybe you should run `get_lazy_loading(sprite)` without assigning result to `sprite.animation_list = `. – furas Sep 05 '16 at 11:54
  • Thanks for your answers, which pointed me to the right problem. I finally figured out what was wrong : - first I called a wrong variable name into get_lazy_loading, tho it would raise an error only if this particular value was called. - more important, I typed `self.img_ref = 'img_list'` instead of `self.img_ref = img_list`, and that's why the function returned None. I corrected the code and everything is fine, so, to answer the topic's name, `var = []` can be used to store a surface or a rect later on. – Dindoleon Sep 05 '16 at 12:13

0 Answers0