1

I'm making a game using the pygame module and I have a player class:

class Player(pygame.sprite.Sprite):
def __init__(self, name, position, axsis, movment, idle, walk = None, jump = None):
    pygame.sprite.Sprite.__init__(self)
    self.name = name
    self.idle = idle
    self.walk = walk
    self.jump = jump
    self.image = self.idle[0]
    self.movment = movment 
    self.left, self.right  = axsis
    self.pos = vec(position[0],position[1])

I am adding my characters using json data type and trying to add animations after calling the class but i can't do it

Sample code

class Game():
    def __init__(self,json_file):
        self.player_attribute = json_file

    def character(self):
          self.npc = []
          for i in self.player_attribute:
              self.npc.append(Player(i['name'],
                                     i['position'],
                                     i['axsis'],
                                     i['movment']))
          self.animation()
          return self.npc

    def add_animation(self):
        for i in self.npc:
            i.idle = "images\ghost.png" 
    def main_loop()
        self.character

when i try this i get an error

self.image = self.idle[0] TypeError: init() missing 1 required positional argument: 'idle'

how can i add the variables of the class after calling the class

3
  • idle is a mandatory parameter for the Player class. If you change this to idle=None in the __init__ parameter list you will be able to define player and then define the images later. Commented Nov 5, 2021 at 11:55
  • first try it right but i am getting an error like this below.> self.image = self.idle[0] TypeError: 'NoneType' object is not subscriptable is there a way to add the values after calling the class? Commented Nov 5, 2021 at 11:58
  • I'd move your self.image = idle[0] to a different method rather than __init__. This seems like something you would do in an update method just before you redraw the character Commented Nov 5, 2021 at 12:00

1 Answer 1

1

It is not clear what the 'idle' parameter is supposed to represent in your code. However, the reason for the exception is that you are not passing any argument for 'idle' when constructing the Player object. You need something like:

          self.npc.append(Player(i['name'],
                                 i['position'],
                                 i['axsis'],
                                 i['movment'],
                                 i['idle']))

You can either do that or alternatively you can pass a default argument to the Player constructor so that, when initializing you do not need to explicitly pass idle:

class Player(pygame.sprite.Sprite):
def __init__(self, name, position, axsis, movment, idle=[1], walk = None, jump = None):

You can modify its content at a later time, however I suggest you are careful what you instantiate that object attribute as, because it might come bite you back later (type error or value error).

If it were me, I would move this out of init, or not build the object until all values and their types are known (see Builder Design Pattern).

Hope this helped :) Cheers!

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

2 Comments

I think I'm facing a logic error, I'll reconsider my class,thank you for your attention.
No problem. Please don't hesitate to upvote the answer if it helped and choose it as the correct answer. It would be much appreciated :)

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.