if stage2 == 1:
screen.fill(black)
pygame.display.flip()
time.sleep(1)
screen.fill(blue)
pygame.display.flip()
time.sleep(0.25)
screen.fill(black)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_me = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
screen.fill(red)
stage2 = stage2 + 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
screen.fill(white)
stage2 = stage2 + 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
screen.fill(blue)
stage3 = stage3 + 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
screen.fill(green)
stage2 = stage2 + 1
顺便说一句,颜色和其他所有代码的代码都在代码中,但我们只能说你可以看到它。
当我运行此代码时,屏幕一直闪烁蓝色,HELP!
答案 0 :(得分:1)
我不知道您尝试做什么,但我会使用while
循环来检查密钥,直到您更改run_me
或stage2
。
if stage2 == 1:
screen.fill(black)
pygame.display.flip()
time.sleep(1)
screen.fill(blue)
pygame.display.flip()
time.sleep(0.25)
screen.fill(black)
pygame.display.flip()
while run_me and stage2 == 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_me = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
screen.fill(red)
stage2 = stage2 + 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
screen.fill(white)
stage2 = stage2 + 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
screen.fill(blue)
stage3 = stage3 + 1
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
screen.fill(green)
stage2 = stage2 + 1
它可能不适用于其他代码,但您没有显示其余代码。
使用sleep()
时不会检查密钥,因此您可能需要重新考虑代码并使用其他内容。即
def wait(time)
quit = False
key = None
current_time = pygame.time.get_ticks()
end_time = current_time + time*1000
while end_time > current_time and not key and not quit:
clock.ticks(25)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit = True
elif event.type == pygame.KEYDOWN:
key = event.key
current_time = pygame.time.get_ticks()
return quit, key
if stage2 == 1:
screen.fill(black)
pygame.display.flip()
quit, key = wait(1)
if quit:
run_me = False
# - or -
#run_me = not quit
screen.fill(blue)
pygame.display.flip()
quit, key = wait(0.25)
if quit:
run_me = False
# - or -
#run_me = not quit
screen.fill(black)
pygame.display.flip()
当然,它仍可能无法正常使用其他代码。