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

时间:2015-07-17 20:49:37

标签: python-3.x pygame

我只想说这个问题,我确实已经查到了这个问题,但似乎有类似问题的每个人都找到了一个对我不起作用的解决方案。我正在使用python 3.4.3 64位和pygame 1.9.2a并获得标题中声明的错误。我的代码目前看起来像这样:

import pygame

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
bright_red = (255,0,0)
green = (0,200,0)
bright_green = (0,255,0)
blue = (0,0,200)
bright_blue = (0,0,255)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Le jeux')
clock = pygame.time.Clock()

thatonething = pygame.image.load('thatonething.png')

def thatonething(x,y):
    gameDisplay.blit(thatonething,(x,y))

x = (display_width * 0.45)
y = (display_height * 0.8)

crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        print(event) 

    gameDisplay.fill(white)
    thatonething(x,y)
    pygame.display.update() 
    clock.tick(30) 

pygame.quit() 
quit()

运行它给了我这个:

Traceback (most recent call last):
  File "C:/Users/Windows 8/Desktop/Scripts/New folder/Main.py", line 39, in <module>
    thatonething(x,y)
  File "C:/Users/Windows 8/Desktop/Scripts/New folder/Main.py", line 24, in thatonething
    gameDisplay.blit(thatonething,(x,y))
TypeError: argument 1 must be pygame.Surface, not function

提前致谢,如果这听起来像是一个愚蠢的问题,我很抱歉。

编辑:感谢Brian,这已经解决了。对于有类似问题的人,可能会在以后看到此问题,更正的版本如下所示:

import pygame

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
bright_red = (255,0,0)
green = (0,200,0)
bright_green = (0,255,0)
blue = (0,0,200)
bright_blue = (0,0,255)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Le jeux')
clock = pygame.time.Clock()

thatonething = pygame.image.load('thatonething.png')

def somefunction(x,y):
    gameDisplay.blit(thatonething,(x,y))

x = (display_width * 0.45)
y = (display_height * 0.8)

crashed = False

while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
        print(event) #This creates a log of the events that pygame has been handling.

    gameDisplay.fill(white)
    somefunction(x,y)
    pygame.display.update() #Updates everything "pygame.display.flip()" updates just one thing
    clock.tick(30) #This defines the refresh rate i.e. 30 in brackets gives 30 fps

pygame.quit() #Closes the game (with the next line)
quit()

关键的区别是'thatonething'不再是函数的名称。

1 个答案:

答案 0 :(得分:1)

你有一个功能:

def thatonething(x,y):
    gameDisplay.blit(thatonething,(x,y))

当您放置gameDisplay.blit(thatonething,(x,y))时,它会将参数解释为1:thatonething,2:(x,y)

你可能想把其他东西作为gameDisplay.blit的输入,因为即使你删除了逗号,你也会有无限递归。

我不确定这个功能是做什么的,但你可能想考虑重新设计它。