【发布时间】:2014-08-12 11:27:28
【问题描述】:
我在 Ubuntu 上使用 PyGame,我想创建一个 while 循环,当用户按下键盘上的任何按钮时结束。
此代码不会离开循环,Eclipse 不会给出错误和警告,但不会离开循环。怎么了?
import time
import pygame
pygame.init()
test = False
while not test:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
print "gotcha"
test = True;
break;
print "Still looping"
time.sleep(1);
print "made it out of the loop" ;
理想情况下,在我按下任意键之前,每秒钟都应将“仍在循环中”打印到屏幕上,此时应打印“使其脱离循环”。
这不会发生:循环永远继续(直到我终止脚本)。
【问题讨论】:
-
仅供参考,对于 Python,您不需要在行尾使用分号。这不是无效的,只是不必要的。
标签: python loops pygame keydown