TypeError:参数1必须是pygame.Surface,而不是builtin_function_or_method

时间:2018-06-06 21:58:21

标签: python pygame

此处的完整代码: https://pastebin.com/Ne3dCpzp

Dice0 = pygame.image.load("dice_0.png").convert
Dice1 = pygame.image.load("dice_1.png").convert
Dice2 = pygame.image.load("dice_2.png").convert
Dice3 = pygame.image.load("dice_3.png").convert
Dice4 = pygame.image.load("dice_4.png").convert

dice0img = Dice0
dice1img = Dice1
dice2img = Dice2
dice3img = Dice3
dice4img = Dice4

...

    if dice == 0 :
        screen.blit(dice0img, (80, 320))
    if dice == 1 :
        screen.blit(dice1img, (80, 320))
    if dice == 2 :
        screen.blit(dice2img, (80, 320))
    if dice == 3 :
        screen.blit(dice3img, (80, 320))
    if dice == 4 :
        screen.blit(dice4img, (80, 320))

我目前收到错误:

Traceback (most recent call last):
  File "C:\Users\Ed\Documents\thonk\game.py", line 90, in <module>
    screen.blit(dice4img, (80, 320))
TypeError: argument 1 must be pygame.Surface, not builtin_function_or_method

为什么会这样?

1 个答案:

答案 0 :(得分:0)

正如错误消息所示,您必须将Surface传递给blit函数。

如果您查看代码的这些行:

Dice4 = pygame.image.load("dice_4.png").convert
...
dice4img = Dice4

您可以看到Dice4 / dice4img是对convert函数的引用。同样,这也是错误消息告诉您的内容( not builtin_function_or_method )。

您必须实际调用convert函数,以便返回Surface并将其分配给Dice4变量。

<强> TL; DR:

看起来像

的线条
Dice4 = pygame.image.load("dice_4.png").convert

应该是这样的

Dice4 = pygame.image.load("dice_4.png").convert()