typeError: 'module' Object is not callable

Asked

Viewed 1,991 times

0

I’m trying to add layers to my game window, so I tried using Sprite groups, but there’s this mistake that I don’t understand why 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 to rect.

  • There is no way to clone in the code a comment indicating the line where the interpreter points the error. And in case the error message is more verbose attach to the message.

1 answer

1

It’s on the pygame.Surf line - that’s the name of the module - you should use pygame.Surface (with uppercase "S" - to create an instance of the Surface class.

This is common in some medium to large projects in Python: the names of the modules and subcases of the project - which are the files ". py" 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.

This 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 same Surface class 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 - I called contexts.py - then users will see the 3 names.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.