Tiki
Tiki

Reputation: 11

Pygame Error: TypeError: projectile() takes 1 positional argument but 5 were given in Pygame

I am still a newbie to Python and I am following a tutorial on Youtube about Pygame. I came across an error while running Pygame. I tried looking up the solution here but it seems that my error is a bit different. Hoping for some insights from you guys on how to fix this.

# Character Class
class player(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.velocity = 5
        self.isJump = False
        self.jumpCount = 10
        self.left = False
        self.right = False
        self.walkCount = 0
        self.standing = True

    def draw(self, window):
        # Draw Image
        if self.walkCount + 1 >= 27:
            self.walkCount = 0

        if not (self.standing):
            if self.left:
                window.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
            elif self.right:
                window.blit(walkRight[self.walkCount // 3], (self.x, self.y))
                self.walkCount += 1
        else:
            if self.right:
                window.blit(walkRight[0], (self.x, self.y))
            else:
                window.blit(walkLeft[0], (self.x, self.y))

# Projectile Class
def projectile(object):
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.velocity = 8 * facing


    def draw(self, window):
        pygame.draw.circle(window, self.color, (self.x, self.y), self.radius)


# Game Function
def redrawGameWindow():
    window.blit(background, (0, 0))
    hero.draw(window)
    for bullet in bullets:
        bullet.draw(window)
    pygame.display.update()

# Main Loop
hero = player(300, 410, 64, 64)
bullets = []
run = True
while run:
    clock.tick(27)

    # Check for events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # Bullets
    for bullet in bullets:
        if bullet.x < 500 and bullet.x > 0:
            bullet.x += bullet.velocity
        else:
            bullets.pop(bullets.index(bullet))

    # Movements
    keys = pygame.key.get_pressed()

    # Shoot Bullets
    if keys[pygame.K_SPACE]:
        if hero.left:
            facing = -1
        else:
            facing = 1

        if len(bullets) < 5:
            bullets.append(projectile(round(hero.x + hero.width //2), round(hero.y + hero.height //2), 6, (0,0,0), facing))

The error that I receive was this:

bullets.append(projectile(round(hero.x + hero.width //2), round(hero.y + hero.height //2), 6, (0,0,0), facing))
TypeError: projectile() takes 1 positional argument but 5 were given

Upvotes: 1

Views: 220

Answers (1)

Kingsley
Kingsley

Reputation: 14906

The code is defining a function projectile(), with a local-function __init__() - not a class.

# Projectile Class
def projectile(object):                                 # <-- HERE
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        ...

It just needs a minor syntax change:

# Projectile Class
class projectile(object):                               # <-- HERE
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        ...

This probably should be a comment, not an answer, but it took me a few minutes to work it out myself, so I figured maybe it's worth a full answer.

Upvotes: 1

Related Questions