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.

8
  • 2
    Why does your door() function, also have an event loop ? Commented Jun 1, 2021 at 3:02
  • what do you mean? Commented Jun 1, 2021 at 3:02
  • 3
    You call pygame.event.get() inside door(). That will remove all events from the queue: pygame.org/docs/ref/event.html#pygame.event.get Commented Jun 1, 2021 at 3:05
  • sorry I do not quite understand what you mean. I do call pygame.event.get() inside door() Commented Jun 1, 2021 at 3:09
  • 1
    Yes, don't do that. Only have one place in the code where the events are consumed. Commented Jun 1, 2021 at 3:13

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

this didn't work, error said door takes 0 positional arguments but one was given
Yes, you have to add the parameter to the definition of door(), I updated my answer
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.

4 Comments

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.
What I am wondering is how do i do what you suggested, and will that fix my (new) problem?
That should not be happening. Can you edit with the new code pls?
it got fixed so don't worry. Thank you for trying tho :D
-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().

1 Comment

sorry I do not quite understand what you want me to do

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.