4

I am trying to exit the program when the escape key is pressed. I am having some trouble doing that.

When I take out the door() line it works;

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            print("key pressed")
            if event.key == pygame.K_ESCAPE:
                print("key pressed")
                pygame.quit()
                sys.exit()
    #door()

This is the command prompt readout

C:\Users\Me\Documents\Fan game>python testing_file.py
pygame 2.0.1 (SDL 2.0.14, Python 3.9.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
key pressed
key pressed

C:\Users\Me\Documents\Fan game>

But as soon as I add that line back in, it stops working.

My door function;

def door():
    
    global var_door
    
    image_door_1 = pygame.image.load(r'textures\door\frame_1.png')
    image_door_2 = pygame.image.load(r'textures\door\frame_2.png')
    image_door_3 = pygame.image.load(r'textures\door\frame_3.png')
    image_door_4 = pygame.image.load(r'textures\door\frame_4.png')
    image_door_5 = pygame.image.load(r'textures\door\frame_5.png')
    image_door_6 = pygame.image.load(r'textures\door\frame_6.png')
    
    image_door_1_size = image_door_1.get_rect().size
    image_door_2_size = image_door_2.get_rect().size
    image_door_3_size = image_door_3.get_rect().size
    image_door_4_size = image_door_4.get_rect().size
    image_door_5_size = image_door_5.get_rect().size
    image_door_6_size = image_door_6.get_rect().size
    
    centered_image_door_1 = [(display_size[0] - image_door_1_size[0])/2, (display_size[1] - image_door_1_size[1])/2]
    centered_image_door_2 = [(display_size[0] - image_door_2_size[0])/2, (display_size[1] - image_door_2_size[1])/2]
    centered_image_door_3 = [(display_size[0] - image_door_3_size[0])/2, (display_size[1] - image_door_3_size[1])/2]
    centered_image_door_4 = [(display_size[0] - image_door_4_size[0])/2, (display_size[1] - image_door_4_size[1])/2]
    centered_image_door_5 = [(display_size[0] - image_door_5_size[0])/2, (display_size[1] - image_door_5_size[1])/2]
    centered_image_door_6 = [(display_size[0] - image_door_6_size[0])/2, (display_size[1] - image_door_6_size[1])/2]
    
    mouse_down = False
    
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN:
            mouse_down = True
    
    x, y = pygame.mouse.get_pos()
    if 0 < x < 253 and 0 < y < 226:
        if mouse_down:
            if var_door == 0:
                screen.blit(image_door_1, centered_image_door_1)
                pygame.display.update()
                time.sleep(0.01)
                    
                screen.blit(image_door_2, centered_image_door_2)
                pygame.display.update()
                time.sleep(0.01)

                screen.blit(image_door_3, centered_image_door_3)
                pygame.display.update()
                time.sleep(0.01)

                screen.blit(image_door_4, centered_image_door_4)
                pygame.display.update()
                time.sleep(0.01)

                screen.blit(image_door_5, centered_image_door_5)
                pygame.display.update()
                time.sleep(0.01)

                screen.blit(image_door_6, centered_image_door_6)
                pygame.display.update()
                        
                var_door = 1
                    
            else:
                screen.blit(image_door_6, centered_image_door_6)
                pygame.display.update()
                time.sleep(0.01)
                    
                screen.blit(image_door_5, centered_image_door_5)
                pygame.display.update()
                time.sleep(0.01)

                screen.blit(image_door_4, centered_image_door_4)
                pygame.display.update()
                time.sleep(0.01)

                screen.blit(image_door_3, centered_image_door_3)
                pygame.display.update()
                time.sleep(0.01)

                screen.blit(image_door_2, centered_image_door_2)
                pygame.display.update()
                time.sleep(0.01)

                screen.blit(image_door_1, centered_image_door_1)
                pygame.display.update()
                        
                var_door = 0

I do not understand why it does not work. I tried rearranging the order that the while loop checks but even that doesn't work. My guess is for some reason it never checks if the escape key is pressed and its getting stuck on the door function, but the door function doesn't have any loops in it.

Wolfmann Games
  • 142
  • 1
  • 13

3 Answers3

2

As you are calling pygame.event.get() inside door(), the event queue will be empty in the main loop.

A possible solution is to store the list returned from pygame.event.get() and use it in both loops.

Rafael Setton
  • 352
  • 1
  • 8
  • So i removed the pygame.event.get() in my door function and now my door animation doesn't work. It just opens then closes really quickly. If I spam the button that opens/closes it I can get it to change state. But what i want is that when the button is clicked, the door opens, and when it is clicked again, it closes. – Wolfmann Games Jun 01 '21 at 03:20
  • What I am wondering is how do i do what you suggested, and will that fix my (new) problem? – Wolfmann Games Jun 01 '21 at 03:20
  • That should not be happening. Can you edit with the new code pls? – Rafael Setton Jun 01 '21 at 03:31
  • it got fixed so don't worry. Thank you for trying tho :D – Wolfmann Games Jun 01 '21 at 03:36
2

You're querying the even loop twice which is why you loose some events. Change your main loop to this:

while True:
    mouse_down = False
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            print("key pressed")
            if event.key == pygame.K_ESCAPE:
                print("key pressed")
                pygame.quit()
                sys.exit()
        elif event.type == MOUSEBUTTONDOWN:
            mouse_down = True

    door(mouse_down)

and change def door() to def door(mouse_down), then remove the second event loop from the door() function

Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23
-2

Place the door() function cose to the left side aligning below the while . As python works the door() is under while loop. Inform me if it works or please add detail about why is the door() in while loop and the use of door().

Binil George
  • 266
  • 3
  • 10