Search code examples
pythonpygamegame-development

Why doesn't my click work immediately in pygame?


I am recreating mine sweeper.

My code goes through every square on a 800 by 600 display and each square is 50 by 50 so there are 192 squares. I check to see if the click is in each square and draw a bomb or an open space depending on what it is.

def check_for_click(x_click,  y_click, x_square, y_square, x_circle, y_circle, a_round, a_count):
    if x_click < x_square and y_click < y_square and x_click > (x_square - 50) and y_click > (y_square - 50):
        if grid[(a_round - 1)][(a_count)] == 1:
            bomb.play()
            choose = random.randint(0, 6)
            the_color = random.choice(light_colors[choose])
            make_square(the_color, (x_square - 50), (y_square - 50))
            the_color = random.choice(dark_colors[choose])
            make_circle(the_color, x_circle, y_circle)
            pygame.display.update()
        if grid[(a_round - 1)][(a_count)] == 0:
            the_grass.play()
            make_square(light_brown, (x_square - 50), (y_square - 50))
            pygame.display.update()
            grid[(a_round - 1)][(a_count)] = 2

The above function checks to see if the click was in a certain square

word = True
while word:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            word = False
    
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            click = pygame.mouse.get_pos()
            check = True
            while check == True:
                round = 1
                count = 0
                the_x = 50
                the_y = 50
                cir_x = 25
                cir_y = 25
                x = click[0]
                y = click[1]
                for i in range(12):
                    for i in range(16):
                        check_for_click(x, y, the_x, the_y, cir_x, cir_y, round, count)
                        the_x = the_x + 50
                        cir_x = cir_x + 50
                        count = count + 1
                    the_y = the_y + 50
                    cir_y = cir_y + 50
                    the_x = 50
                    cir_x = 25
                    count = 0
                    round = round + 1
                check = False

Above is the game loop and then it goes into a while loop every time you click and checks every square position to see if the click was inside of it

    update_score()
    draw_numbers()
    pygame.display.update()

pygame.quit()

Above are some more functions that I wrote but not include for the sake of brevity.


Solution

  • pygame.event.get() get all the messages and remove them from the queue. See the documentation:

    This will get all the messages and remove them from the queue. [...]

    If pygame.event.get() is called in multiple event loops, only one loop receives the events, but never all loops receive all events. As a result, some events appear to be missed.

    Call pygame.event.get() just once:

    word = True
        while word:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    word = False
    
            # for event in pygame.event.get(): <--- DELETE
            
                if event.type == pygame.MOUSEBUTTONDOWN:
                    click = event.pos
    
                    # [...]