I am trying to instanciate a object i created witch is a subclass of pygame.sprite.Sprite. This is the code i use to instanciate it:
import pygame as pg
from GameObject import GameObject
player = GameObject("Sprites/Player", 6)
And this is the code for the object itself:
import pygame as pg
class GameObject(pg.sprite.Sprite):
def __ini__(self, image_path, number_of_images):
for i in range(number_of_images):
self.images.append(pg.image.load("{}/sprite{}.png".format(image_path, i)))
When i try to run it i get this:
Traceback (most recent call last):
File "main.py", line 11, in <module>
player = GameObject("Sprites/Player", 6)
File "/usr/lib/python3.6/site-packages/pygame/sprite.py", line 124, in __init__
self.add(*groups)
File "/usr/lib/python3.6/site-packages/pygame/sprite.py", line 142, in add
self.add(*group)
File "/usr/lib/python3.6/site-packages/pygame/sprite.py", line 142, in add
self.add(*group)
File "/usr/lib/python3.6/site-packages/pygame/sprite.py", line 142, in add
self.add(*group)
[Previous line repeated 327 more times]
RecursionError: maximum recursion depth exceeded
I have looked at sever example codes for creating a subclass of this class but i do not see the difference between my code and others. What am i missing out on?
I had the same problem, it's caused by the typo in the __init__
(you wrote __ini__
) function (it happened to me when i wrote _init_
instead of __init__
). Fixing the typo makes it work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.