0

In a 'Pygame loop', I'm trying to ask for user input but when I run the program, the pygame window becomes unresponsive if I hover my mouse over it or click anywhere. Does anyone know what's going wrong?

import pygame

win = pygame.display.set_mode((600, 600))
win.fill((240, 240, 240))  #white
pygame.display.update()

#Game loop
quit = False
while quit == False:

    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            exit()

    u_input = input("Enter 'q' to quit or 'n' to fill the window with navy: ")

    if u_input == 'q':
        quit = True

    elif u_input == 'n':
        win.fill((60, 55, 100))  #navy
        pygame.display.update()

Unresponsive pygame window image

The IDE I'm using is Visual Studio Code

  • 2
    Your code needs to be a [mre]; and as per [ask] you need all your relevant code to be in the post itself, not in any external links. For instance, we have no idea what's in `get_validSectN`, and we shouldn't have to visit an external site to see the definition. – Random Davis Jun 28 '22 at 16:12
  • 1
    What does `get_validSectN` do? Does it call `input`? – Rabbid76 Jun 28 '22 at 17:16
  • You need to call `pygame.quit()` at the end. – The_spider Jun 28 '22 at 17:43
  • @RandomDavis | Whoops sorry about that, I should've read the posting guidelines first. I'll make a minimal reproducible ex and update this post or repost it – Daniel Liu Jun 29 '22 at 23:31
  • @Rabbid76 | Yes, `fFuncts.display_menu()` displays a menu and prompts the user to enter a num representing the font section they want to display, while `get_validSectN()` calls `input()` and checks if it's an int between 1-11. Then the function returns a valid section number. (Sorry my post wasn't the best, I should've read the posting guidelines first) – Daniel Liu Jun 29 '22 at 23:41
  • 1
    @DanielLiu So that'S your problem. See [Why is my display not responding while waiting for input?](https://stackoverflow.com/questions/67111782/why-is-my-display-not-responding-while-waiting-for-input/67113040#67113040) – Rabbid76 Jun 30 '22 at 04:50
  • 1
    @Rabbid76 | Oh I see, my program works now! Thank you very much (I also see you were the one who responded in the other post ;) . I didn't see that post in my google search, so it may have been helpful if its title included "pygame display" – Daniel Liu Jul 01 '22 at 19:35

1 Answers1

0

Unfortunately, this is just a nature of pygame. When you ask for an input, the program stops and waits for the user to input something, preventing the pygame.diplay.flip() from occuring.

There is two ways I can think of to fix this. Using, threads, one for powershell (terminal) and one for pygame should work, however I'm not familiar with that at all, so you would need to research for yourself.

A different solution is to listen for user input instead of using terminal prompts

#Game loop
quit = False
while quit == False:

    for e in pygame.event.get():

        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_q:
                quit = True
            if e.key == pygame.K_n:
                win.fill((60, 55, 100))  #navy

pygame.display.update()