Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: draw() missing 1 required positional argument: 'win'

class rocket(object):
def __init__(self, x, y):
    self.image = pygame.image.load('sprites/spaceship.png')
    self.x = x
    self.y = y
    self.width = 72
    self.height = 72
    self.speed = 5
    self.angle = 0

def draw(self, win):
    win.blit(char, (self.x,self.y))

def updateGameWindow():
win.blit(bg, (0,0))
rocket.draw(win)
pygame.display.update()

For some reason when I run this program, I'm getting the error that rocket.draw(win) is missing the argument 'win' when it is clearly there.

win is defined at the top of the program.

like image 883
Neo630 Avatar asked Dec 18 '25 22:12

Neo630


1 Answers

draw() needs to be called on a rocket instance, not the class itself:

rocket_instance = rocket(some_x, some_y)
rocket_instance.draw(win)

In the first line of the code above, you are creating an instance of the rocket class, and on the second line you are calling the draw() method on that instance where the instance itself is passed implicitly as the self argument.

In your example code, you are getting an error from this line rocket.draw(win) because, since you are calling draw() on the class itself instead of an instance, the instance is not passed as the self argument, and instead the win argument is passed for the self param, and everything blows up because now there is no expected win argument passed.

like image 103
elethan Avatar answered Dec 21 '25 19:12

elethan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!