This question already has an answer here:
Pygame error : video system not initialized [duplicate]
(1 answer)
Closed 2 years ago.
Why this error is coming I am not understanding can anyone please tell why is it coming and how to correct it?
For every event in the event queue, you call pygame.quit(), which will deinitialize all pygame modules:
pygame.quit()
uninitialize all pygame modules
quit() -> None
Uninitialize all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue. It is safe to call this function more than once as repeated calls have no effect.
Note
Calling pygame.quit()uninitialize all pygame modules will not exit your program. Consider letting your program end in the same way a normal Python program will end.
So in the next iteration of the while loop, the call to pygame.event.get() will fail because the video system is no longer initialzed.
You should check the type attribute of the event object against the pygame.QUIT constant, and not call the pygame.quit() function:
...
if event.type == pygame.QUIT:
...
Usually there's no need to ever call pygame.quit().
Related
This question already has answers here:
Pygame window not responding after a few seconds
(3 answers)
Pygame unresponsive display
(1 answer)
How does the for event in pygame.event.get() in python work?
(1 answer)
Closed 3 months ago.
So, a while true loop that only passes, freezes the window. But a while true loop that checks the pool of events for a Quit event won't freeze. Why is that? checking the events take enough time to prevent the program from overloading? What is going on inside the hood?
Loop that freezes the Pygame window:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
while True:
pass
Loop that won't freeze the Pygame window:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
So if both are running infinitely (The second one virtually, supposed that the QUIT event is never called), why the first one consumes so many resources that it overloads it an freezes the window, but the second one "behaves" and doesn't overload it, even if the exit condition of the loop is not called? Is there some waiting time in the pygame.event.get() function?
I tried looking for info on Google but I got lost in subprocesses, etc.
When running I am blit'ing some text onto my surface (WIN) and I want this to be shown in the game so I call pygame.display.update to update the display, this works perfectly fine until the loop has iterated around 75 times and after this the display stops updating. Code below
def dialogue(x,y,text):
global clock
global run
typing=True
tempstring=""
counter=0
switcher=0
for z in range(0,(len(text))):
clock.tick(15)
tempstring=text[0:z+1]
counter+=1
print(counter)
print("temp is "+tempstring)
writing=TITLE_FONT.render(tempstring, 1, (255,255,255),(0,0,0))
if x==-1 and y>-1:
pygame.display.update(WIN.blit(writing, (640-(writing.get_width()//2),y-(writing.get_height()//2))))
elif x>-1 and y==-1:
pygame.display.update(WIN.blit(writing, (x-(writing.get_width()//2),360-(writing.get_height()//2))))
elif x==-1 and y==-1:
pygame.display.update(WIN.blit(writing, (640-(writing.get_width()//2),360-(writing.get_height()//2))))
else:
pygame.display.update(WIN.blit(writing, (x-(writing.get_width()//2),y-(writing.get_height()//2))))
print(typing)
while True:
event = pygame.event.wait()
if event.type == pygame.QUIT:
run=False
pygame.quit()
break
if event.type == pygame.KEYDOWN:
break
I have tried just using pygame.display.update() at the end of the line of if statements too but this also fails.
On some systems PyGame doesn't work correctly if you don't get pygame.event from system - system may think that program hangs and it may even close it.
You may need to use pygame.even.get() (or similar functions) inside your loop.
In doc pygame.event you can find (but it is hidden in long description)
To prevent lost events, especially input events which signal a quit command, your program must handle events every frame (with pygame.event.get(), pygame.event.pump(), pygame.event.wait(), pygame.event.peek() or pygame.event.clear()) and process them.
Not handling events may cause your system to decide your program has locked up.
To keep pygame in sync with the system, you will need to call pygame.event.pump() internally process pygame event handlers to keep everything current.
I wrote a game with pygame. The game works fine but when I quit and close the window, while the window does close Python itself does not quit and instead shows (not responding) and I am forced to, well, force quit.
I start the code by importing several modules
import random, pygame, sys
from pygame.locals import*
There is only one place in the code that directly commands the program to shut down
def terminate():
pygame.quit()
sys.exit()
To quit the program always runs the terminate() function.
I tried adding pygame.display.quit() above pygame.quit() and substituting raise SystemExit for sys.exit()
What could cause python to close the window but not shut down itself?
I personnay don't imoprt sys to exit. It simply isn't effective for me to type sys.exit() and you can simply call exit() to exit the program.
So you can close the window like that:
# main loop
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT: # if the user closes the window
running = False
# after exiting mail loop
pygame.quit()
exit()
[EDIT] According to this answer, you should prefer using quit() instead of exit():
However, like quit, exit is considered bad to use in production code and should be reserved for use in the interpreter. This is because it too relies on the site module.
I don't get any problems now that I use quit().
This question already has answers here:
pygame.event.get() not returning any events when inside a thread
(1 answer)
Is it possible to make the keyboard module work with pygame and threading
(1 answer)
Closed 1 year ago.
I was writing a small program in python 2.7.10 with pygame. Im using multithreading to play sounds (with winsound) and still draw things fast. I was trying to replicate a similar idea of some videos i saw on youtube of sorting algorithms. Unfortunately it runs for about 10 seconds then randomly crashes, the sounds keep going but the pygame window goes not responding and the drawing does not change. Even if i put a print "hi" in the code somewhere it will still be printing in the console but pygame will still have gone not responding. Cant think of whats wrong but it may be something with the multithreading (im very new to it!)
from random import randint
import pygame,winsound,threading,time
from pygame.locals import *
screen=pygame.display.set_mode([1000,500])
def bubbleSort(listName):
sorting=True
while sorting:
listChanged=False
for i in range(len(listName)-1):
draw(listName,1000/len(listName),500/11,i)
if listName[i] < listName[i+1]:
listName[i],listName[i+1]=listName[i+1],listName[i]
threading.Thread(target=playsound,args=([listName[i]])).start()
listChanged=True
if not listChanged:
sorting=False
time.sleep(10)
pygame.quit()
def playsound(frequency):
winsound.Beep(frequency*100,100)
def draw(listName,width,height,comparing):
print "hi"
screen.fill([0,0,0])
for i in range(len(listName)):
if i == comparing or i == comparing+1:
pygame.draw.rect(screen,[0,255,0],[width*(len(listName)-i),500-(height*listName[i]),width,500])
else:
pygame.draw.rect(screen,[255,255,255],[width*(len(listName)-i),500-(height*listName[i]),width,500])
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
pygame.display.update()
list1=[]
for i in range(100):
list1.append(randint(1,10))
threading.Thread(target=bubbleSort,args=([list1])).start()
I am having trouble trying to grasp a couple of things inside the py game code, firstly why are there two types of quit or are these the same? for example:
pygame.QUIT
pygame.quit()
Also I can't grasp this small piece code fully:
for event in pygame.event.get():
if event.type == pygame.QUIT:
I understand the first line of code, but I don't understand event.type?, is .type a function inside of pygame or python in general? what does it do?
pygame.QUIT
Is an enumeration for an input that signals for the program to quit
pygame.quit()
Is a function call to unload pygame modules. According to the docs it won't actually quit the game:
http://www.pygame.org/docs/ref/pygame.html#pygame.quit
You could use sys.exit(0) for that.
This loop here:
for event in pygame.event.get():
if event.type == pygame.QUIT:
Checks each event, if one of them has the value pygame.QUIT then some exit code should follow.
pygame.QUIT is simply a constant, while pygame.quit() is a function within the pygame module that uninitializes it. The fragment of code:
for event in pygame.event.get():
if event.type == pygame.QUIT:
...
is part of the typical control loop of a pygame program. The method pygame.event.get() returns the next event. If the type of event (an attribute of the event object) is pygame.QUIT then the application should do what is necessary to exit, including possibly calling pygame.quit().
The manual for pygame.quit() says:
Uninitialize all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue.
So you should normally call this method yourself.
pygame.QUIT is a so called event type. Pygame uses events to tell you something happened. The code checks each events in the queue and checks if a QUIT event happened.
event.type is something from pygame, and it tells you what type of event it is. By comparing it to pygame.QUIT, you can check if the quit() method has been called, and take steps to shutdown the game.