I'm currently trying to set up hitboxes for my characters but I can't seem to get rid of this error.
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
self.x = x
self.y = y
pygame.sprite.Sprite.__init__(self)
self.image = IronMan
self.rect = self.image.get_rect()
self.rect.y = 475
self.direction = 1
self.hitbox = (self.x + 20, self.y + 11, 28, 60)
def draw(self, win):
self.hitbox = (self.x + 20, self.y + 11, 28, 60)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
When the code is ran Im faced with
TypeError: __init__() missing 1 required positional argument: 'y'
Looks like you didn't provide sufficient parameters when you created an instance of the class. There are 2 options to fix it.
Example:
Mark = Player() # Error
Cindy = Player(1) # Error
James = Player(1, 2) # Good!
Since you specified _ init _ method to take 2 arguments, you must provide 2 arguments when calling it.
Example:
def __init__(self, x=0, y=0):
#blabla
Now, _ init _ method will automatically initialize the arguments to 0, if it's not provided manually.
Mark = Player() # Mark.x=0, Mark.y=0
Cindy = Player(1) # Cindy.x=1, Cindy.y=0
James = Player(1, 2) # James.x=1, James.y=2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.