【发布时间】:2017-08-27 07:05:19
【问题描述】:
我最近在设计一款游戏,因为我要在学校开设一门课程 并认为这会有所帮助。
我遇到了错误
Traceback (most recent call last):
File "C:\Users\Jake\Documents\Dungeon Crawler 2.1\Main.py", line 100, in <module>
town()
File "C:\Users\Jake\Documents\Dungeon Crawler 2.1\Main.py", line 85, in town
houseSpr1(0,250)
TypeError: 'pygame.Surface' object is not callable
在执行此代码时。
def town ():
global outTown
while not outTown:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print (event)
mx, my = pygame.mouse.get_pos()
mx = mx + my
if 375 <= mx <= 448:
outTown = True
print ("OK!")
if event.type == pygame.QUIT:
pygame.quit()
quit()
houseSpr1 = pygame.image.load ('houseD.png') # default house img
houseSpr1(0,250)
gameDisplay.fill(green) # background
pygame.draw.rect(gameDisplay,grey,(0,400,1280,50)) # main path
pygame.draw.rect(gameDisplay,grey,(200,125,50,280)) # branch path
player(x,y)
pygame.display.update()
clock.tick(60)
def houseSpr1(a,b):
gameDisplay.blit(houseSpr1, (0,250))
def house1():
global outHouse
while not outHouse:
gameDisplay.fill(black)
town()
houseSpr1()
gameIntro()
house1()
pygame.quit()
quit()
我了解此代码在某些方面可能效率低下,如果您希望就此提供见解,我很乐意知道如何改进它。
任何有关此错误的帮助将不胜感激。是的,我已经阅读了我之前的其余问题,我发现这主要是由于印刷错误,但我在这里看不到任何问题。
【问题讨论】:
-
请包含异常的完整回溯,而不仅仅是异常行本身。
-
为什么要分配本地名称
houseSpr1?你屏蔽了你定义的全局函数houseSpr1。函数不存在于单独的命名空间中,您不能为多个事物使用相同的名称。 -
我不知道蒙面是什么意思。我已更新条目以显示完整错误。 @MartijnPieters
-
你重用了同名作为局部变量;函数
town()中对houseSpr1的所有引用均指该本地名称。全局名称houseSpr1,即您定义的函数,在town()函数内不再可访问。 -
因此,当您编写
houseSpr1(..)时,您将(..)调用表达式添加到您在其前一行创建的houseSpr1对象中。为您加载的函数或图像使用不同的名称。
标签: python python-3.x pygame