【问题标题】:Python pygame event loop type errorPython pygame 事件循环类型错误
【发布时间】:2013-04-27 16:35:13
【问题描述】:

我正在制作一个简单的 pygame 游戏。我的问题是,当我尝试检查用户是否单击退出按钮时,出现错误。这是代码:

for event in pygame.event.get():
   if event.type == pygame.QUIT():
       pygame.quit()
       sys.exit()

这是错误:

Traceback (most recent call last):
File "C:\Users\Rafi\Python Programs\Game.py", line 20, in <module>
if event.type == pygame.QUIT():
TypeError: 'int' object is not callable

另外,这可能不是,但我在 Windows 8 上。

【问题讨论】:

  • 你得到什么错误?从标签来看,我猜是TypeError
  • 是的。抱歉,我现在将在错误中进行编辑。
  • 您必须添加所有相关代码。第 20 行附近发生了什么,'i' 是什么?
  • 我对pygame不熟悉,但是从文档来看,QUIT 是一个字面量,而不是一个方法。如果是这种情况,执行QUIT() 会导致此错误。以下是使用QUIT的其他代码的一些示例:nullege.com/codes/search?cq=pygame.event.get
  • 抱歉,不知怎的打错字了,意思是上面写着 i 的事件。

标签: python events loops pygame typeerror


【解决方案1】:
>>> pygame.QUIT
12

所以,

>>> pygame.QUIT() >> 12()
TypeError: 'int' object is not callable

在文本中,pygame.QUIT = 12 所以做pygame.QUIT() 相当于做12(),这是一个调用,这不是你想要的。

只需将您的行更改为:

if event.type == pygame.QUIT:

【讨论】:

    【解决方案2】:

    pygame.QUIT 是一个常数(如果你想知道的话,是 12)。它后面不需要任何()。这就是你的全部问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多