简体   繁体   English

如何清除 pygame 事件队列?

[英]How to clear pygame event queue?

I use pygame to design a psychophysical experiment.我用pygame设计了一个心理物理实验。 The general flow of the experiment is that two objects appear in each trial, then press the left and right direction keys to select one of them, the system will inform the result, and then make the next trial.实验的一般流程是,每次试验出现两个物体,然后按左右方向键select其中一个,系统会通知结果,然后进行下一次试验。 Now I have found such a problem: when running on some computers, if I press the left and right keys multiple times in one trial, the event queue should be cleared before the next selection when running on the normal computer, and the results should be given after the selection on this trial, which is also true on my personal laptop(the next selection will not be affected by pressing multiple times);现在我发现了这样一个问题:在某些电脑上运行时,如果我在一次试用中多次按下左右键,在普通电脑上运行时应该在下一次选择之前清除事件队列,结果应该是本次试用选择后给出,在我的个人笔记本上也是如此(多次按下不影响下次选择); However, on some computers, when running the same program, it will execute the selection of the next few times in sequence according to the sequence of pressing keys (it will automatically select according to the sequence about key without making a selection).但是在某些电脑上,运行同一个程序时,会按照按键的先后顺序依次执行后面几次的选择(不做选择,会自动按照关于按键的顺序进行select)。 In addition, even if the program is encapsulated with pyinstaller on the normal computer, the problem will still occur when the encapsulated program used on the problematic computer.另外,即使在普通电脑上用pyinstaller封装了程序,在问题电脑上使用封装的程序时,还是会出现问题。 How can I solve it?我该如何解决?

Here is the program segment.这是程序部分。 If you need to run the total program, I will provide it with you.如果您需要运行总程序,我将提供给您。

# wait for a click
def wait4click(duration=1500):
    left = (WIN_LENGTH / 4 - IMG_HEIGHT / 2, WIN_WIDTH / 2 - IMG_WIDTH / 2)
    right = (3 * WIN_LENGTH / 4 - IMG_HEIGHT / 2, WIN_WIDTH / 2 - IMG_WIDTH / 2)
    pygame.event.clear()
    t_start = pygame.time.get_ticks()
    while True:
        t_now = pygame.time.get_ticks()
        if t_now - t_start > duration:
            return 'BLANK'
        else:
            for event in pygame.event.get():
                if event.type == KEYDOWN:

                    if event.key == K_LEFT:
                        choice = 'left'
                        pygame.draw.polygon(win, red, [eval(choice),
                                                       (eval(choice)[0] + IMG_HEIGHT, eval(choice)[1]), (
                                                           eval(choice)[0] + IMG_HEIGHT,
                                                           eval(choice)[1] + IMG_WIDTH),
                                                       (eval(choice)[0], eval(choice)[1] + IMG_WIDTH)], 10)
                    elif event.key == K_RIGHT:
                        choice = 'right'
                        pygame.draw.polygon(win, red, [eval(choice),
                                                       (eval(choice)[0] + IMG_HEIGHT, eval(choice)[1]), (
                                                       eval(choice)[0] + IMG_HEIGHT,
                                                       eval(choice)[1] + IMG_WIDTH),
                                                   (eval(choice)[0], eval(choice)[1] + IMG_WIDTH)], 10)
                    elif event.key in [K_ESCAPE]:
                        resp = pygame.key.name(event.key)
                        pygame.event.clear()
                        return resp
                    else:
                        continue
                    pygame.event.clear()
                    return choice

train_list = ['p1', 'p1', 'p2', 'p2', 'p2', 'p2', 'p1', 'p1']
for i in range(len(train_list)):
    [correct_cue_loc, wrong_cue_loc] = draw_cue(train_list[i], introduction=True, symbol=True) # draw two objects
    subj_choose = wait4click(duration=1500)
    break_signal = train_judgement(subj_choose)  # judge and show the result on the screen
    if break_signal:
        break
    pygame.time.wait(1000)
    fadeout()    # equal to win.fill(grey),[output][/output] pygame.display.update()
    text_msg('+')
    pygame.time.wait(1000)
    win.fill(grey)

I've spend quite a bit of time on your question because it came up yesterday in a game I'm messing with/trying to fix.我在您的问题上花了很多时间,因为它昨天出现在我正在弄乱/试图修复的游戏中。 The answer seems to be答案似乎是

time.sleep()时间.sleep()

I have absolutely no idea why or how your going to implement it in your code.我完全不知道为什么或如何在代码中实现它。 The following code works consistently to get proper timings.以下代码始终如一地工作以获得正确的时间。 Even if your press and hold the key from the first to the second timer, the second timing will trigger on the KEYUP as you lift your finger:即使您从第一个定时器到第二个定时器按住键,当您抬起手指时,第二个定时器也会在 KEYUP 上触发:

import pygame, sys, time

if __name__ == '__main__':
    pygame.init()
    screen_width = 600
    screen_height = 600
    screen = pygame.display.set_mode((screen_width, screen_height))
    clock = pygame.time.Clock()

    print('timer started')
    t_start = pygame.time.get_ticks()
    pygame.event.clear()
    event =  pygame.event.wait()
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            print('we got a space')
        if event.key == pygame.K_RIGHT:
            print('we got a Right')
        if event.key == pygame.K_LEFT:
            print('we got a Left')
    pygame.event.clear()
    t_now = pygame.time.get_ticks()
    print(f"timer ended {t_now - t_start} ticks")
    time.sleep(1)  # without this line it doesn't work right
                   # and I have no ^&$% idea why.

    print('timer started again')
    t_start = pygame.time.get_ticks()
    pygame.event.clear()
    event =  pygame.event.wait()
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_SPACE:
            print('we got a space')
        if event.key == pygame.K_RIGHT:
            print('we got a Right')
        if event.key == pygame.K_LEFT:
            print('we got a Left')
    t_now = pygame.time.get_ticks()
    print(f"timer ended {t_now - t_start} ticks")

Good luck, but asynchronous interrupt processing will drive you bats**t crazy over time.祝你好运,但随着时间的推移,异步中断处理会让你发疯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM