TypeError:draw()缺少1个必需的位置参数:' surface'

时间:2015-08-30 08:01:13

标签: python-3.x pygame typeerror

使用此代码时出现错误。 虽然我可以告诉表面确实填充了所有的位置参数。

我从块类创建播放器,然后传入参数来创建播放器。 (颜色,宽度和高度) 创建播放器后,我将其添加到all_sprites列表中,并使用player.rect.x和player.rect.y将块放在屏幕上。

最后我只是将all_sprites_list绘制到屏幕上。

然而,当这样做时,我收到此错误:

TypeError: draw() missing 1 required positional argument: 'surface' 

有人可以告诉我这里的错误吗?

class Block(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.width = width

screen_width = 700
screen_height = 500
size = (screen_width, screen_height)
screen = pygame.display.set_mode(size)

# Sprites lists
all_sprites_list = pygame.sprite.Group

# Create the player
block_width = 30
block_height = 15
player = Block(BLUE, block_width, block_height)
# Set the initial position for the player in the center of the screen
player.rect.x = screen_width/2 - block_width/2
player.rect.y = screen_height/2 - block_height/2
# Add the player to all_sprites_list
all_sprites_list.add(player)

# -------- Main Program Loop -----------
while not done:
    # --- Main event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(WHITE)
    all_sprites_list.draw(screen)

    pygame.display.flip()

1 个答案:

答案 0 :(得分:2)

pygame.sprite.Group是一个类,根据位于here的文档。

Group(*sprites) -> Group

您需要做什么在调用pygame.sprite.Group的地方添加括号,并在括号中添加该组中的所有精灵,如下所示:

all_sprites_list = pygame.sprite.Group(player,enemy,etc...)

至少你需要在Group之后添加括号,以便解释器不将其视为对象,并正确调用函数。