Why is my pygame display not responding while waiting for input?
You cannot use input
in the application loop. input
waits for an input. While the system is waiting for input, the application loop will halt and the game will not respond.
Use the KEYDOWN
event instead of input
:
run = True
while run:
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if pygame.key == pygame.K_1:
# [...]
if pygame.key == pygame.K_2:
# [...]
Another option is to get the input in a separate thread.
Minimal example:
import pygame
import threading
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
color = "red"
def get_input():
global color
color = input('enter color (e.g. blue): ')
input_thread = threading.Thread(target=get_input)
input_thread.start()
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window_center = window.get_rect().center
window.fill(0)
pygame.draw.circle(window, color, window_center, 100)
pygame.display.flip()
pygame.quit()
exit()
Pygame program stops responding, giving no error
When asking for input
the program stops and wait until the user types something in. When the program stops it cannot handle events which makes your os to believe the program has crashed. So when using pygame you cannot have console input (unless you create processes).
If all you needed was a delay, you could use a while
-loop
def wait(time):
clock = pygame.time.Clock()
while time > 0:
dt = clock.tick(30) / 1000 # Takes the time between each loop and convert to seconds.
time -= dt
pygame.event.pump() # Let's pygame handle internal actions so the os don't think it has crashed.
If you want the user to decide how long to wait, you could check for user events
def wait_user_response():
clock = pygame.time.Clock()
waiting = True
while waiting:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN: # Or whatever event you're waiting for.
waiting = False
There are of course other better ways to go about, but since I don't know how your program functions, this was all I could provide.
Pygame window not responding after a few seconds
Call pygame.event.get()
at the beginning of the while loop.
Why does the pygame window become unresponsive?
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()
Pygame window crashing on run
- Every pygame program needs pygame.init() at the beginning
- Use pygame.display.update() only once at the end of the program
- Your program freezes because input() breaks the main loop of your program
Pygame start file:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#Your code...
pygame.display.update()
clock.tick(FPS)
You can solve this by adding an input box to the pygame window: https://stackoverflow.com/a/46390412/15500012
I hope it will help you, Adam
Pygame Window not Responding after few seconds
Your game is not responding, because you ask for an input
inside the application loop. input
stops the application loop and waits for input to be confirmed. If you stop the application loop, the window stops responding. Use the KEYDOWN
event to get an input in PyGame (see pygame.event
):
for event in pygame.event.get():
# [...]
if event.type == pygame.KEYDOWN:
guessed.append(event.unicode)
guessed
has to be initialized before the application loop. Don't reset it in the loop:
import pygame
pygame.init()
running = True
window_width = 600
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
clock = pygame.time.Clock()
word = "something"
guessed = []
answer = ""
for c in word:
answer += c + " " if c in guessed else "_ "
print(answer)
while running:
dt = clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
guessed.append(event.unicode)
answer = ""
for c in word:
answer += c + " " if c in guessed else "_ "
print(answer)
pygame.quit()
Related Topics
Scatter Plot and Color Mapping in Python
Get the Second Largest Number in a List in Linear Time
Subprocess.Call Using String VS Using List
Assigning to Variable from Parent Function: "Local Variable Referenced Before Assignment"
How to Interpret Conda Package Conflicts
Understanding Matplotlib.Subplots Python
Why Does '.Sort()' Cause the List to Be 'None' in Python
Run Python Script Without Windows Console Appearing
Matplotlib: Save Plot to Numpy Array
How Would I Access Variables from One Class to Another
"Cannot Open Include File: 'Config-Win.H': No Such File or Directory" While Installing MySQL-Python
Pandas - Plotting a Stacked Bar Chart
Python Round Up Integer to Next Hundred
Access Elementtree Node Parent Node
Draw a Transparent Rectangles and Polygons in Pygame