Lets says I just have a normal game loop using pygame.
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
Where run = False is, should I use run = False and pygame.quit() at the end? Should I just put pygame.quit() where run = False is? Should I use quit() without pygame in front of it at all? Thanks.
I recommend to do it exactly as you do it in the question.
pygame.quit() uninitialize all pygame modules. Any further call to a pygame module (except pygame.init()) will cause an exception. To terminate a pygame application correctly, pygame.quit() has to be the called at the end. If you do pygame.quit() in the main application loop, then the application will crash if you do something after it (e.g. pygame.disaply.update()).
Related
I just spent a fair amount of time finding a 64-bit installation of pygame to use with python 3.3, (here) and now am trying to make a window. However, although the window opens up fine it does not close when it hit the x button. In fact, I have to close IDLE to close the window. I am running a 64 bit version of Win 7. Here is my code:
import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
When I append
time.sleep(5)
pygame.quit()
It still doesn't close. My only guess would be that pygame.quit might go inside one of the loops, but even if that were resolved I would greatly prefer being able to close the window when I want to.
Most pygame tutorials seem to suggest exiting by calling pygame.quit() and then sys.exit(). I have personally run into problems (was on a unix system though) where this still did not close the window properly. The solution was to add pygame.display.quit() specifically before pygame.quit(). That should not be necessary as far as I can tell, and I'm afraid I don't know why that solved the issue but it did.
if you want to make pygame close when window button x is pressed, put the code like this:
from sys import exit
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
We put exit() after pygame.quit(), because pygame.quit() makes the system exit and exit() closes that window.
Not sure but try this Because you code runs fine on my system after I add pygame.quit() at the end
import pygame
import time
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
pygame.display.set_caption("Hello World")
running = True
try:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
except SystemExit:
pygame.quit()
Its perhaps because as Idle is made on Tkinter and thus Tkinter and Pygame main loop do not have a mutual understanding.
Your code will run very well on command prompt though.
This was the final code that worked for me on OSX whilst keeping the kernel alive on Jupyter. EDIT - it does still crash the kernel sometimes :-(
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.quit()
pygame.quit()
exit()
Also needed to downgrade ipython to get rid of some magic alias warning messages using:
conda install ipython=7.2.0
apparently that issue is due to be fixed in ipython 7.6.0
Suffered the same issues on Python 3.7.4 while running it from in IDE (Spyder 3.3.6). In my case the pygame.quit() would not completely close the program.
Nonetheless, adding quit() or exit() did the trick for me!
Add this at the top:
import sys
Add this where you need to quit:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
try using the following command:
sys.exit(0)
notice: You will need to import the sys library in order to use it.
The IDE interferes with how pygame runs the code. Try to run it from the commandline or the terminal. The problem should disappear.
To answer the original question: You must call pygame.quit() after breaking the main loop. One elegant solution goes as follows:
def run():
pygame.init()
while True:
# ...
for event in pygame.event.get():
# Handle other events
if event.type == pygame.QUIT:
return pygame.quit()
Originally after I did some searching here, I found a question with the exact same problem I had:
Pygame window not responding after a few seconds . I reviewed all the answers and tried them, but none of them worked. I tried using for loops to loop through every single event;
run = True
while run:
for event in pygame.event.get():
if event == pygame.QUIT()
run = False
But the window still closed. I also tried:
run = True
while run:
event = pygame.event.get()
if event == pygame.QUIT():
run = False
Which had the same results as the one above.
Can anyone help?
Edit: I use PyCharm and MacOS Catalina.
pygame.QUIT is a constante, but pygame.QUIT() is a call statement. Remove the braces. Anyway, the condition won't work, because you have to compare the type attribute of the event to the event type constant (see pygame.event). Furthermore the : is missing at the end of the if-statement.
if event == pygame.QUIT()
if event.type == pygame.QUIT:
Furthermore the Indentation is not correct:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
After a long time trying to install pygame for 2.7 it finally installs and I now have it downloaded, but there is now the problem that it keeps not responding after a couple seconds of being open. Any answer will be appreciated, the code that I have so far is just.
import pygame
pygame.init()
pygame.display.set_mode((640,480))
so I need some help please.
So what you want to do, like what skrx said, is a while loop to continuously keep the code inside the while loop and the pygame window running, and as well as a for event loop, in order to be able to shut down the window. Here's how you can do it:
import pygame
pygame.init()
pygame.display.set_mode((640, 480)) # opens the display
while True: # the while loop that will keep your display up and running!
for event in pygame.event.get(): # the for event loop, keeping track of events,
if event.type == pygame.QUIT: # and in this case, it will be keeping track of pygame.QUIT, which is the X or the top right
pygame.quit() # stops pygame
There are other ways of stopping the while loop, and you could do this:
running = True
while running: # the while loop that will keep your display up and running!
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
Hope this helps!
I'm fairly new to pygame and i needed some help because my code is not working properly.
Okay so here's the problem: I want the screen to turn white when i run it but it remains black, however when i press the exit, it turns white for about a second and then closes.
This also happens when i put a picture (like the player.png) it appears for about a second before exiting. I don't know what i'm doing wrong,
please help fix the code and explain why it is happening?
here is the code:
import pygame
pygame.init()
screen = pygame.display.set_mode((640,480))
image = pygame.image.load('player.png')
gameExit = False
gameLoop = True
pygame.display.update()
white = (255,255,255)
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
for event in pygame.event.get():
screen.fill(white)
pygame.display.update()
pygame.quit()
quit()
PS. I don't get any errors
Python is indentation sensitive. In your code, you call pygame.display.update() only once your main loop ends.
Also, you only paint the background white in the case there's an event in the event queue between the two for loops, and then you fill the background for every event in the queue.
Note that this could also lead to a situation in which your QUIT event is "swallowed" by the second loop.
So this
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
for event in pygame.event.get():
screen.fill(white)
pygame.display.update()
should be
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
screen.fill(white)
pygame.display.update()
Ok, so I agree with Sloth, but instead of doing all the screen updating and filling and blitting at the end, making the script less readable, you should make like an animate function that runs at the end of the while loop each time, and put the blitting and screen updating stuff in that function. Sorry for the run-on sentence.
Oh and you don't need your GameLoop variable.
I've been following along with a bouncing ball example just to get my chops warmed up with pygame: every time I test my code I have to kill the game window by causing it to freeze, though in my code (taken directly from the pygame website) I state that the game should exit if the Escape key is pressed or the X button on the screen. I get an error
running == False
NameError: name 'running' is not defined
my code is
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running == False
if event.type ==pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running == False
can I define "running?" such that the game doesn't simply freeze when I try to quit.
First off, you should define running to True (running = True) above your while loop. Secondly, you should be checking that value somewhere; easiest is to change while 1 to while running Third, == is checking for equality, = is setting a value. You want to check event.type to be pygame.QUIT or KEYDOWN, so those == are correct, but then you want to set running to False, which is running = False. Doing running == False as a statement is not effective.