Pygame 仅在检测到输入时更新

Pygame only updating when input is detected

Pygame 在检测到某种输入之前不会更新屏幕。它应该在某个值等于某个数字时更新,但实际上只有在按下某个键时才会更新。按下的每个键都会发生这种情况,而不仅仅是实际执行某些操作的键。有没有办法在不需要用户引起更新的情况下更新屏幕?

我看过以前的 post,但没有答案得到原 post 人的认可,而且 none 无论如何对我有用。 Screen only updates when I check for user input pygame

感谢您提供的任何帮助:D

检测值的代码:

    if distance <= 25 and display_scroll[1] <= -135 and display_scroll[1] >= -250:
        print("scroll from man:", display_scroll[1])
        doSpeechMan = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_e:
                doText = True
                print("interact")
                teacherTalk = True
    if distance >= 25 and display_scroll[1] >= -135 and display_scroll[1] <= -250:
        doText = False
        doSpeechMan = False

应该更新屏幕的代码:

if doSpeechMan:
    
    display.blit(pygame.transform.scale(bubble_image, (72, 72)), (50-display_scroll[0], 75-display_scroll[1]))
    ptext.draw("Press E", (60-display_scroll[0], 90-display_scroll[1]), color="black", fontname="text/Pixel-y14Y.ttf", fontsize=10)

您必须 运行 应用程序循环中的代码而不是事件循环中的代码。事件循环仅在队列中有事件时执行。应用程序循环在每一帧中执行。

# applicaition loop
run = True
while run:

    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        # handle the events here
        # this code is executed once for each event
        # [...]

    # update the objects and draw the scene here
    # this code is executed once per frame
    # [...]

另见