Skip to content
Advertisement

TypeError: draw_shape() missing 1 required positional argument: ‘self’

I’m trying to code a simple game of pong as a way to learn pygame as i’m not that experienced in coding. I’ve only recently started to use classes and i guess i don’t quite understand how to use init properly As whenever i run the code:

class ball:

    def __init__(self):
        self.y = y
        
    def draw_shape(self):
        
        pygame.draw.circle(screen, WHITE,(30 , self.y),15)
        pygame.display.flip()


while running:
    clock.tick(fps)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == KEYDOWN and (event.key == K_UP):
            player.y = player.y + vel
    screen.fill(BLACK)
    visual.border_draw()
    visual.mid_lines()
    visual.score()
    ball.draw_shape()

I got the following error message:

TypeError: draw_shape() missing 1 required positional argument: 'self'

Advertisement

Answer

try:

ball().draw_shape()

or just

player = ball()
player.draw_shape()
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement