【问题标题】:pygame event handlingpygame 事件处理
【发布时间】:2011-05-05 17:25:46
【问题描述】:

只是一个关于 python 和 pygame 事件处理的菜鸟问题。

我在一个 pygame 教程中得到以下代码:

while 1:
   for event in pygame.event.get():
       if event.type in (QUIT, KEYDOWN):
            sys.exit()

...但由于某种原因它返回此错误:

if event.type in (QUIT, KEYDOWN):
NameError: name 'QUIT' is not defined

谁能解释一下?

【问题讨论】:

    标签: python event-handling pygame


    【解决方案1】:

    我认为你的意思是:

    if event.type in (pygame.QUIT, pygame.KEYDOWN)
    

    本教程可能使用了from pygame import *,这个例子完美地说明了为什么这是一个坏习惯。

    【讨论】:

      【解决方案2】:

      代替from pygame import *,使用:

      from pygame.locals import *

      【讨论】:

      • 这对我不起作用(使用 Python 3.4)。我不得不使用from pygame import *
      猜你喜欢
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多