![]() |
Problem with pygame.event.clear - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Problem with pygame.event.clear (/thread-36180.html) |
Problem with pygame.event.clear - qq12346 - Jan-25-2022 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. # 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) RE: Problem with pygame.event.clear - patriciainman - Oct-05-2023 Hello, instead of immediately processing key events as they occur, I think you can queue them and process them one at a time. This ensures that only one key event is processed before moving on to the next trial. You can use a list or a queue to store the key events and process them sequentially. online games Here's an example of how you can modify your code to implement event queuing: python event_queue = [] # Create an empty event queue # Inside the event loop for event in pygame.event.get(): if event.type == pygame.KEYDOWN: event_queue.append(event) # Add key events to the queue # Process the key events one by one for event in event_queue: if event.key == pygame.K_LEFT: # Process left key event elif event.key == pygame.K_RIGHT: # Process right key event elif event.key == pygame.K_ESCAPE: # Process escape key event event_queue.clear() # Clear the event queue after processing ```This way, only the events in the queue will be processed, and any other key events that occurred during the trial will be ignored. |