Why am I getting a missing required positional argument when It's being provided?

Viewed 48

I am trying to write a boid program using python, I have a program called boid.py which defines the boid class and another program called flocking practice.py which is supposed to use the create the boids and draw them on the screen. the code in boid.py is:

import numpy
import random

from pygame.locals import*

pg.init()

class Boid(object):
    def __init__(self,pos):
        object.__init__(self)
        self.pos = [pos[0]/2,pos[1]/2]
        self.vel = []
        self.accel = []

    def draw(self,screen):
        pg.draw.circle(screen,[255,255,255],self.pos,5)

and the code in flocking practice.py is:

import pygame as pg
import sys

clock = pg.time.Clock()
screen_dimensions = [1000,800]
screen = pg.display.set_mode(screen_dimensions)

boid_list = []
flock_size = 1
white = [255,255,255]


for i in range(flock_size):
    new_boid = boid.Boid(screen_dimensions)
    boid_list.append(boid)

while True:
    for event in pg.event.get():
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    sys.exit()
            if event.type == pg.QUIT:
                sys.exit()
    screen.fill(0)
    for i in range(len(boid_list)):
        boid.Boid.draw(screen)
    clock.tick(60)
    pg.display.update()

when I run flocking practice is gives me an error message saying

Traceback (most recent call last):
  File "C:\Users\Chris\Documents\python programs\flocking practice.py", line 27, in <module>
    boid.Boid.draw(screen)
TypeError: draw() missing 1 required positional argument: 'screen'
[Finished in 1.219s]

why is it saying that it's missing the positional argument when I feed it into the method?

2 Answers

You are trying to invoke the draw method with the class Boid, not the instances of Boid stored in boid_list.

for boid in boid_list:
    bond.draw(screen)

You're calling draw on the class, therefore it requires a self argument. I think you meant to do boid_list[i].draw(screen).

Related