我在使用 pygame 时遇到了一个问题,在 python 中突破了 for 循环。 具体来说,我试图在释放密钥时打破循环。 例如,当按住键"W"时,代码应该执行,但在释放"W"时立即脱离循环。
我试图使用pygame.key.get_pressed()
和if event.type == KEYUP:
但我似乎无法正确处理。 谁能帮忙?
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
doQuitStuff()
elif event.type == pygame.MOUSEBUTTONDOWN:
doMouseButtonStuff()
elif event.type == KEYDOWN:
if event.key == pygame.K_p:
doPStuff()
elif event.key == pygame.K_e:
doEStuff()
elif event.key in foo:
doFooStuff()
for i in xrange(100):
doThisStuffOnlyIfKeyInFooIsHeldDown() #This for loop finishes
#execution even if I release the
#key in "foo"
你必须
没有循环for
- while True
将作为循环。
run_loop = False
i = 0
while True:
# - events -
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == pygame.K_w:
# start loop
run_loop = True
i = 0
elif event.type == KEYUP:
if event.key == pygame.K_w:
# stop loop
run_loop = False
# - updates -
if run_loop and i < 100:
i += 1
doThisStuffOnlyIfKeyInFooIsHeldDown()
# - draws -