Jan-25-2022, 05:46 AM
I use pygame to design a psychophysical experiment. 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. 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). 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. How can I solve it?
Here is the program segment. If you need to run the total program, I will provide it with you.
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)