2011-05-05 130 views
2

只是一个关于python和pygame事件处理的noob问题。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 

任何人都可以解释一下吗?

回答

16

我想你的意思是这样的:

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

教程可能使用from pygame import *,和这个例子完美展现为什么这是一个坏习惯。

0

代替from pygame import *,使用方法:

from pygame.locals import *

+0

这对我来说没有工作(使用Python 3.4)。我不得不使用'from pygame import *' – starcorn 2015-01-25 20:34:26