1
votes

Hi Im just starting to learn OOP and I use python to do so. Recently I ve been trying to code a game. I ve declared class Character that should be general and from that class my other classes will inherit. Now Im trying to create a class Player, I want to inherit everything but 1 variable. Here is a code:

class Character:
def __init__(self, x, y, width, height):
    self.width = width
    self.height = width
    self.x = x
    self.y = y
    self.vel = 3
    self.right = False
    self.left = False
    self.walk_count = 0
    self.is_jump = False
    self.jump_count = 10
    self.standing = True

class Player(Character):
def __init__(self, x, y, width, height, right, left, walk_count, is_jump, jump_count, standing):
    super().__init__(x, y, width, height, right, left, walk_count, is_jump, jump_count, standing)
    self.vel = 5

in super() I keep getting pylint error:

Too many positional arguments for method call.

But I think it lets me run it even with it.

Also I want to put in only self.x, self.y, self.width and self.height when initializing, should I declare these values in class Player or let it be in class Character?

Then here I try to create an instance of class Player:

man = Player(200,410,64,64)

And I got errors missing value for argument right, left, walk_count, is_jump, jump_count, standing

I tought that if i set values for them in class Character that I dont need to put values in when creating instance, because I want to set them to default values at the creation and then I will change them if I need it.

Later I will add class Enemy that will inherit most of the parameters from class Character also.

1
If you don't want to pass values to the __init__ function, why do you declare the function to take these arguments in the first place? Also Character's init only takes 4 arguments, yet you are trying to pass a lot more (way too many to make sense IMO) - UnholySheep
You are right Ive had false understeanding of the whole class declaration. I tought that I need to put all values that I want to initialize in Player class to init and then call super() to inherit them from class Character. Thanks! :) - MadGrip

1 Answers

2
votes

Then, you must call the super method with the signature (number of parameters) desired.

Change

def __init__(self, x, y, width, height, right, left, walk_count, is_jump, jump_count, standing):
    super().__init__(x, y, width, height, right, left, walk_count, is_jump, jump_count, standing)
    self.vel = 5

to

def __init__(self, x, y, width, height):
    super().__init__(x, y, width, height)
    self.vel = 5

Or, if you prefer, change Character's __init__ to match the Player's super call, and change:

class Character:
def __init__(self, x, y, width, height):

to

class Character:
def __init__(self, x, y, width, height, right, left, walk_count, is_jump, jump_count, standing):

and leave the super call as is.

The best approach is to only pass what is needed. If the only needed values to parameterize your Character object are x,y,height,weight, then you should only pass them - the remaining will be set automatically (you don't need to give them as input, since they will be discarded).

So, the correct approach is the first I mentioned.