Question

I am working on a game in pyglet, this is the first time, although normally I use pygame

I am trying to subclass in pyglet.

class menu(pyglet.sprite.Sprite):

    def __init(self, *args, **kwargs):
        super(menu, self).__init__(self, *args, **kwargs)
        self.labels = {}


class pause_menu(menu):

    def __init__(self, *args, **kwargs):
        super(pause_menu, self).__init__(self, *args, **kwargs)

        self.labels['resume'] = pyglet.text.Label('Resume Game', font_name='Times New Roman', font_size=36, x=window.main.width//2, y=window.main.height//2)

I am getting this error

File "C:\Python33\lib\site-packages\pyglet\sprite.py", line 225, in __init__
self._texture = img.get_texture()
AttributeError: 'pause_menu' object has no attribute 'get_texture' 'get_texture'.

What is going on? Am I using super wrong?

BTW: The class is being called from another module

pause_menu = menu.pause_menu(x=0, y=0, batch=menu_batch, img=None)
Was it helpful?

Solution

You are invoking the parent __init__ wrong; use super(menu, self).__init__(*args, **kwargs). The method is bound for you by super().

You also misnamed menu.__init__, missing a double underscore at the end.

class menu(pyglet.sprite.Sprite):
    def __init__(self, *args, **kwargs):
        super(menu, self).__init__(*args, **kwargs)
        self.labels = {}

class pause_menu(menu):
    def __init__(self, *args, **kwargs):
        super(pause_menu, self).__init__(*args, **kwargs)
        self.labels['resume'] = pyglet.text.Label('Resume Game', font_name='Times New Roman', font_size=36, x=window.main.width//2, y=window.main.height//2)

Because you passed in self explicitly, the pyglet.sprite.Sprite.__init__() is called with two positional arguments, both references to self.

As a result, that second self argument is seen as the img argument and pyglet tries to call the AbstractImage.get_texture() method on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top