使用pygame.time.delay()时Pygame有输入延迟

时间:2016-10-24 13:52:15

标签: python python-3.x pygame pygame-clock

我的程序目标是让它循环显示图像并在屏幕上显示。当用户按下空格时,它应该进入新的屏幕。由于某种原因,从用户按下空格和跳出game1循环时有一个延迟。

def game_loop1():
        game1=True
        winScreen=True
        current=0
        myfont = pygame.font.SysFont("monospace", 25)
        label = myfont.render("Who is this person? (Press Space)", 1, (0,0,0))


    while game1:
            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                   pygame.quit()
                if event.type == pygame.KEYDOWN:
                    if event.key==pygame.K_SPACE:
                       game1=False # doesn't break out of loop immediately here
        gameDisplay.fill(white)
        gameDisplay.blit(label,(0,500))
        gameDisplay.blit(obama[current], (0,0))
        current+=1
        pygame.display.flip()
        clock.tick(50)
        pygame.time.delay(2500)


    while winScreen:
        gameDisplay.fill(white)
        winLabel=myfont.render("The person was Obama! ", 1, (0,0,0))
        gameDisplay.blit(winLabel,(0,500))
        gameDisplay.blit(obama[len(obama)-1], (0,0))
        pygame.display.flip()
        pygame.time.delay(2500)
        winScreen=False 

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你可以尝试:

game1=False
break

立即退出while循环。

但是,在这种情况下,您并不真正需要game1变量。您可以将循环条件更改为True,并将行game1=False替换为break,您的代码应该按预期工作。