我在 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" ;
理想情况下,每隔一秒“仍然循环”应该打印到屏幕上,直到我按任意键,此时应该打印“使其脱离循环”。
这不会发生:循环永远持续下去(直到我终止脚本)。
你需要
根据游戏编程维基 http://content.gpwiki.org/index.php/Python:Pygame_keyboard_input:
如果您没有设置 pygame 显示 pygame 屏幕,则没有输入将进入 pygame 的事件处理。
import time
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Pygame Caption')
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"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)