Pygame window not responding after a few seconds
Call pygame.event.get()
at the beginning of the while loop.
Pygame unresponsive display
A minimal, typical PyGame application
has a game loop
has to handle the events, by either
pygame.event.pump()
orpygame.event.get()
.has to update the
Surface
whuch represents the display respectively window, by eitherpygame.display.flip()
orpygame.display.update()
.
See pygame.event.get()
:
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
See also Python Pygame Introduction
Minimal example: repl.it/@Rabbid76/PyGame-MinimalApplicationLoop
import pygame
pygame.init()
playerX = 50
playerY = 50
player = pygame.image.load("player.png")
width, height = 64*8, 64*8
screen = pygame.display.set_mode((width, height))
# main application loop
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# clear the display
screen.fill((255,255,255))
# draw the scene
screen.blit(player, (playerX, playerY))
# update the display
pygame.display.flip()
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 display becomes unresponsive when clicking outside of the display
You have to handle the events in the recursive function. See pygame.event.get()
respectively pygame.event.pump()
:
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
Call pygame.event.pump()
at the beginn of the recursive function.
Pygame display not responding
Add this to your loop. For me the only time it isnt responding is when I click the X and this could be to do with the fact that pygame doesn't know what to do when that happens.
import sys
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
pygame.quit()
sys.exit()
Pygame 'not responding' during animations
You have to handle the events in the application loop. See pygame.event.get()
respectively pygame.event.pump()
:
For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system.
while player.x != target_x and player.y != target_y:
pygame.event.pump() # <--- this is missing
if player.x < target_x:
player.x += 1
else:
player.x -= 1
if player.y < target_y:
player.y += 1
else:
player.y -= 1
draw() # updates / draws the screen
clock.tick(30)
Pygame window goes unresponsive randomly after a few clicks
intersection = False
must be set at the beginning of the loop, instead of before the loop, otherwise this will result in an endless loop once 2 circles intersect:
def addTarget():
# intersection = False <-- DELETE
while True:
intersection = False # <-- INSERT
t = Target(randint(RADIUS, WIDTH - RADIUS), randint(RADIUS, HEIGHT - RADIUS))
for t2 in targetList:
if not t.targetsDontIntersect(t2):
intersection = True
if not intersection:
targetList.append(t)
break
Pygame windows isn't responding after a few seconds of running
You missed to update the display at the end of main
by pygame.display.flip()
. Furthermore you have to control the frames per second with pygame.time.Clock.tick
in the application loop:
def main():
# [...]
while not done:
# [...]
pygame.display.flip()
clock.tick(60)
Related Topics
Add Scrolling to a Platformer in Pygame
What Is a Good Way to Draw Images Using Pygame
How to Implement Nested Dictionaries
Getting a Map() to Return a List in Python 3.X
How to Send a "Multipart/Form-Data" With Requests in Python
Binning a Column With Python Pandas
"Ask Forgiveness Not Permission" - Explain
Can't Send Input to Running Program in Sublime Text
Should I Put #! (Shebang) in Python Scripts, and What Form Should It Take
Why Does Integer Division Yield a Float Instead of Another Integer
How to Change the Order of Dataframe Columns
Finding the Source Code For Built-In Python Functions
Typeerror: 'List' Object Is Not Callable in Python
Is Floating Point Arbitrary Precision Available
Equivalent to Time.Sleep For a Pyqt Application
What Causes My Function to Return None At the End
How to Initialize a Dictionary of Distinct Empty Lists in Python