typeError: 'module' object is not callable
I'm trying to add layers to my game window, so I tried to use sprite groups, but there is this error that I don't understand because it persists:
TypeError: 'module' object is not callable
I searched and it seems to happen when a function and a variable have the same name, but in my code there is no function or variable with the same name.
sprite = pygame.sprite.Group()
green = (0, 255, 0)
class Block (pygame.sprite.Sprite):
def __init__ (self, color, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x/2
self.rect.y = y/2
quadrado = Block(green, 50, 50)
sprite.add(quadrado)
I was trying to import an image, but gave this problem and summarized it to rect.
1 answers
Is on the pygame line.surface-this is the name of the module-you should use pygame.Surface (with capital "S") - to create an instance of the Surface class.
This is common in some medium to large Python projects: the names of the modules and subpackages of the project-which are the ".py" files that the project uses internally-appear to those who will use the project-even if it is a more or less simple use. At the same time, the project needs to make available the classes and functions created in these modules.
Is the case of the Class Surface
- which can be used directly as pygame.Surface
- but pygame also has a file surface.py
where (most likely) the Surface class itself is defined, with the command class Surface...
.
In a project of mine, I have an attribute context
, A Class Context
and I need to define everything in a file - which I called contexts.py
- so users will see the 3 names.