How do you inherit a pygame class?

Asked

Viewed 107 times

0

When I run this code (this is the current entire code, ie only 3 lines):

import pygame
class sp(pygame.sprite):
    pass

I get:

Typeerror: module() takes at Most 2 Arguments (3 Given)

I would like to inherit this class to create some additional objects in it as well as perform some of the existing functions.

For example, instead of ...    

mysprites = pygame.sprite.Group()

I want...    

mysprites = sp.Group()

How can I do that?

2 answers

1

You can’t create a child class from a module. The inheritance system only works from class to class, and modules are not classes. They are objects, instances of the class module.

What you said you want at the end of the question does not need inheritance, can be done like this:

from pygame import sprite as sp
mysprites = sp.Group()

If you want to, for some reason raise a daughter class of sprite.Group you can mainly because sprite.Group is a class:

class MyGroup(sp.Group):
    ...

However inherit from a module as I said at the beginning of the answer, it is not something that makes sense in the language.

  • Yes, but as I said, I would like to inherit this class to create some additional objects in it... how to do in this case?

  • @Rogériodec The answer is clear - it is not possible to "inherit this class" because sprite is a module and not a class - and "inherit" only works with classes.

  • Maybe you can think of another way to do what you want - you probably don’t have to inherit anything to do. Remember that python is different from languages like java where each file is a class and everything needs to be classes. In python this is simplified, you can have loose functions and use the modules at will.

  • -1 - both the author and what makes sense in Pygame is inheriting from the Sprite class - only a well-advanced use in large project will need to inherit Sprite. Groups to create something new. Already inherit from Sprite.Sprite is made for normal pygame use: the idea is that the objects to be displayed on the screen set in the user code run some boot code and keep some internal states to work well with the various types of pygame.sprite.Group predefined.

  • @jsbueno I am not suggesting that the OP inherits from Group for some reason. I’m just saying that you can only inherit from classes, and never from modules. I think you were hasty in giving -1 to the answer, which is correct. Read again

  • there is nothing hasty there - the wrong thing is to give 1 less for the correct answer for "revenge" - if you had familiarity with the library or looked at the documentation, you would see that the AP confused the module with the class. You could at least put the phrase "you’ve probably confused "pygame.sprite.Sprite" with "pygame.Sprite". - Instead, it suggests an inheritance that, correct in terms of OOP, in practical terms when doing a pygame project, is meaningless for beginners. I don’t consider that answer to be correct.

  • 1

    @good jsbueno, I was not the one who gave -1 for your answer, which in my opinion although correct does not answer the question asked by the OP, so I also did not give +1, but I do not think she deserves -1.

  • @jsbueno But that’s the way it is, so there’s the voting system - not everyone will agree here, so everyone gives the vote they think best.

Show 3 more comments

0

The class you should inherit in Pygame for noral use of the library is pygame.sprite.Sprite - the problem you had was just to confuse pygame.sprite which is the module, with pygame.sprite.Sprite which is the class Sprite inside the module.

Inheriting from that class and calling the method __init__ of the superclasses, their objects will work with the groups and all methods in them, as is in the documentation.

import pygame
class sp(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
         ...

As for your second question, it is natural to want to shorten what you type, and Python allows this, including with three different forms of the command import.

Instead of

import pygame
a = = pygame.sprite.Group()

you can do:

from pygame.Sprite import Group a = Group()

or

import pygame.sprite as sp
a = sp.Group()

Browser other questions tagged

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