【问题标题】:Pygame Error: TypeError: projectile() takes 1 positional argument but 5 were given in PygamePygame 错误:TypeError:projectile() 需要 1 个位置参数,但在 Pygame 中给出了 5 个
【发布时间】:2020-03-03 22:31:36
【问题描述】:

我还是 Python 的新手,我正在关注 Youtube 上关于 Pygame 的教程。我在运行 Pygame 时遇到了一个错误。我尝试在这里查找解决方案,但似乎我的错误有点不同。希望大家对如何解决这个问题提供一些见解。

# 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))

我收到的错误是这样的:

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

【问题讨论】:

    标签: pygame


    【解决方案1】:

    代码定义了一个函数projectile(),带有一个本地函数__init__() - 不是一个类。

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

    它只需要一个小的语法更改:

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

    这可能应该是评论,而不是答案,但我自己花了几分钟才弄清楚,所以我认为也许值得一个完整的答案。

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 2020-05-02
      • 1970-01-01
      • 2023-03-04
      • 2019-06-09
      • 2020-02-25
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多