-2

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'
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Bonzo 998
  • 3
  • 2
  • 1
    You need to create an instance of the `ball` class. Also, name classes with capital first letter (`Ball`) instead, as PEP8 recommends. – Ted Klein Bergman Mar 04 '22 at 14:44
  • You must instantiate an object of the class before calling a class method; otherwise, self will have no value. We can only call a method using the class object and not the class name. Therefore we also need to use the correct syntax of parentheses after the class name when instantiating an object. The common mistakes that can cause this error are: Not instatiating an object of a class Not correctly instantiating a class – LSeu Mar 04 '22 at 14:45
  • @ŁukaszKwieciński They're trying to use attributes, so this can't be a static method. – Ted Klein Bergman Mar 04 '22 at 14:45
  • Also, you need to take `y` as a parameter into the initializer. – Ted Klein Bergman Mar 04 '22 at 14:46
  • @TedKleinBergman you're right, I didn't notice it at first glance – Łukasz Kwieciński Mar 04 '22 at 14:52

1 Answers1

0

try:

ball().draw_shape()

or just

player = ball()
player.draw_shape()
Zahon
  • 20
  • 4