Python Forum
Problem with pygame.event.clear
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with pygame.event.clear
#1
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame double jump problem Yegor123 3 2,568 May-02-2023, 09:34 PM
Last Post: sudoku6
  (HELP GREATLY APPRECIATED) New user- Huge Pygame Installation Problem! Jbomb 1 2,822 Jan-12-2021, 07:32 PM
Last Post: MK_CodingSpace
  problem with pygame Aladdin 3 4,200 Jun-25-2020, 01:41 PM
Last Post: Aladdin
  Problem with music - Pygame.error GaseBall 1 3,195 Nov-28-2019, 07:46 PM
Last Post: SheeppOSU
  [PyGame] Rotation Problem in PyGame thunderbird028 1 2,694 Nov-14-2019, 06:49 AM
Last Post: Windspar
  [PyGame] Pong game key.event problem erickDarko 2 4,169 Dec-12-2018, 03:17 PM
Last Post: erickDarko
  My Pygame Project's Problem **huh** osmanb06 2 3,659 Nov-06-2018, 09:27 AM
Last Post: osmanb06
  Coding problem in a Pygame Sghet 4 8,023 Aug-13-2018, 05:39 PM
Last Post: MTGReen
  [PyGame] Problem importing pygame / installing pygame Klar 4 9,243 Dec-16-2017, 05:48 PM
Last Post: Klar
  Importing pygame Mac problem Benjipincus 1 3,067 Dec-16-2017, 01:54 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020