0
class combattant(pygame.sprite.Sprite):
    def __init__(self,img,posit):
        pygame.sprite.Sprite.__init__(self)
        self.image=marche[0]
        self.image_pos=posit
        self.face=0
    def mov(self,direction):
        if direction[K_LEFT]:
            self.face=(self.face+1)%2
            self.image_pos.x -= 1
            self.image=marche[0+self.face]
            print ('gauche')
        if direction[K_RIGHT]:
            print ("droit")
            self.face=(self.face+1)%2
            self.image_pos.x += 1
            self.image=marche[2+self.face]

combattant.mov (tkey)

Here is my problem , when I run the programme containing this , I get this:

 Traceback (most recent call last):
File "F:\ISN\essai 2.py", line 63, in <module>
combattant.mov (tkey)
TypeError: mov() takes exactly 2 arguments (1 given)

Python seems to consider 'self' as argument that I need to give in order for it to work. I have tried using alpha functions or puting an empty space where the self argument is but of course I get an error saying 'Invalid Syntax' and the alpha function doesn't change anything ... Maybe I'm using this the wrong way because I'm a beginner ... It would be very helpfull if someone could help me ! Thank's in advance !

7
  • 1
    self is bound when you create an instance and call methods on that. If you are calling methods directly on the class then there is no instance to bind to. Commented Apr 9, 2014 at 18:15
  • You need to create an instance of combattant first, not directly access the methods on the class. Commented Apr 9, 2014 at 18:16
  • could help correct that error ? Commented Apr 9, 2014 at 18:17
  • should I run combattant () first ? Commented Apr 9, 2014 at 18:17
  • 2
    Yes, but you need to pass in arguments; what values should img and posit be set to? Commented Apr 9, 2014 at 18:18

1 Answer 1

1

In your specific case, when you call combatant.move(), you are calling move on the class, not on the instance of the class. The proper way to use that method is to first create an instance.

Typically, people name their class with an uppercase letter, and their instances with a lowercase, to make problems like this easy to spot.

For example:

class Combattant(...):
    ...
combattant = Combattant(...)
combattant.move(tkey)

The reason self is required, is so that the methods know which instance they are applied to. This makes it possible to have more than once instance of the class. When you call some_instance.some_method(...), python will automatically add the self parameter when calling the method.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok so one big problem is that this programme was originally made to be used with Vector2 wich I tried to work without ... So I am now going to tryand redefine posit using the get_rect() method ... But not tonight !!! Thank's for your help Martijn Pieters and Bryan Oakley

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.