<p>在你的事件循环中</p>
<pre><code>#print "Exit value ", pygame.QUIT
for et in pygame.event.get():
#print "Event type ", et.type
#-----------------------------------------------------------------#
if et.type == pygame.KEYDOWN:
if (et.key == pygame.K_ESCAPE) or (et.type == pygame.QUIT):
#-----------------------------------------------------------------#
print "In Here"
return True;
pygame.event.pump() # not quite sure why we do this
return False;
</code></pre>
<p><strong>问题</strong>(在2<code>#------------#</code>之间)<br/>
让我们分析一下这一部分:</p>
<ol>
<li>如果输入了If块,<code>et.type == KEYDOWN</code></li>
<li>你对<code>QUIT</code>的检查在<code>if et.type == KEYDOWN</code>中。</li>
<li>因为<code>et.type</code>是<code>KEYDOWN</code>,所以它不能是<code>QUIT</code>。。</li>
<li>所以,您没有检查<code>et.type == QUIT</code>,<br/>
所以,即使你点击了“X”,你的窗口也不会退出。</li>
</ol>
<p><strong>怎么办?</strong><br/>
将<code>QUIT</code>从<code>KEYDOWN</code>的条件中拉出,类似于:</p>
<pre><code>done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
break # break out of the for loop
elif event.type == pygame.QUIT:
done = True
break # break out of the for loop
if done:
break # to break out of the while loop
# your game stuff
</code></pre>
<p>注:</p>
<ul>
<li>在这些返回语句之后不需要<code>;</code></li>
<li><p>总是在不同的if elif块中检查<code>event.type</code>,比如</p>
<pre><code>if event.type == pygame.QUIT:
#...
elif event.type == pygame.KEYDOWN:
#...
</code></pre></li>
<li>你不需要<code>pygame.event.pump()</code>在那里,见<a href="http://www.pygame.org/docs/ref/event.html#pygame.event.pump" rel="nofollow">Here</a></li>
</ul>