如何让屏幕等待用户输入

时间:2017-12-27 23:01:14

标签: python pygame

在创建我的游戏时,我遇到了这个我认为在测试播放时我已经摆脱的错误,但是当我不断尝试修复它时,它使代码无效。帮助!

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!

1 个答案:

答案 0 :(得分:1)

我不知道您尝试做什么,但我会使用while循环来检查密钥,直到您更改run_mestage2

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()

当然,它仍可能无法正常使用其他代码。