2

When I run this code (this is the current entire code, ie, just 3 line):

import pygame
class sp(pygame.sprite):
    pass

I get:

TypeError: module() takes at most 2 arguments (3 given)

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

For example, rather than...

mysprites = pygame.sprite.Group()

I want...

mysprites = sp.Group()

How can I do this?

martineau
  • 119,623
  • 25
  • 170
  • 301
Rogério Dec
  • 801
  • 8
  • 31
  • When you subclass `sprite` are you overriding the `__init__` method? If so can you show that code? – 101 Sep 17 '18 at 00:44
  • @101, I have now put the entire code, ie, just 3 lines... – Rogério Dec Sep 17 '18 at 00:50
  • 1
    Hang on, `sprite` is a module, not a class, so you can't subclass it. You could just import everything from `sprite` into a new module though. – 101 Sep 17 '18 at 01:43

1 Answers1

3

As @101 mentioned in a comment, sprite is a pygame [sub] module, but is not itself a Python class. To do what you want, you'll need to derive your subclass from the Sprite class that module defines. This means using something along the lines below. (There's also an example in the pygame documentation of a slightly different way of creating a Sprite subclass which you should probably take a look at.)

Note also that class names should have their initial letter Capitalized according to Naming Conventions section of the PEP 8 - Style Guide for Python Code, so I've fixed that as well.

from pygame.sprite import Sprite

class Sp(Sprite):
    pass

To answer the other part of your question where you're trying to use sp.Group(). The problem is what you're trying to do is just plain incorrect. Group is a separate "container" class that's also defined in the pygame.sprite module. It's primary purpose to group a bunch of Sprite class instances. It should be able to handle your Sprite subclass just fine. Below is more code showing how that can be done:

from pygame.sprite import Group, Sprite

class Sp(Sprite):
    pass

# Create a Group container instance and put some Sp class instances in it.
mygroup = Group()
sp1 = Sp()  # Create first instance of subclass.
mygroup.add(sp1)  # Put it in the Group (NOT via sp1.Group())

sp2 = Sp()  # Create another instance of subclass.
mygroup.add(sp2)  # Put it into the Group, too.
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Ok, but how to execute a current object "Group"? `mysprites = Sp.Group()` – Rogério Dec Sep 17 '18 at 02:33
  • The latest update to my answer also addresses that aspect of your problem. Also see [How to use sprite groups in pygame](https://stackoverflow.com/questions/13851051/how-to-use-sprite-groups-in-pygame). – martineau Sep 17 '18 at 03:12