首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame错误: TypeError: projectile()采用1个位置参数,但在Pygame中给出了5个

Pygame错误: TypeError: projectile()采用1个位置参数,但在Pygame中给出了5个
EN

Stack Overflow用户
提问于 2020-03-04 06:31:36
回答 1查看 70关注 0票数 1

我仍然是Python的新手,我正在关注Youtube上关于Pygame的教程。我在运行Pygame时遇到了一个错误。我试着在这里查找解决方案,但似乎我的错误有点不同。希望你们能给我一些关于如何解决这个问题的见解。

代码语言:javascript
复制
# 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))

我收到的错误是:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2020-03-04 06:53:43

代码使用本地函数__init__()定义了一个函数projectile(),而不是一个类。

代码语言:javascript
复制
# Projectile Class
def projectile(object):                                 # <-- HERE
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        ...

它只需要稍微修改一下语法:

代码语言:javascript
复制
# Projectile Class
class projectile(object):                               # <-- HERE
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        ...

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60516486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档