Pygame window immediately opens and closes - python

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

Related

Python Cant exit a Pygame game [duplicate]

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()

pygame.quit(), quit(), or run = False?

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()).

pygame and gnome-shell using TOO much CPU [duplicate]

i am new to pygame and i was wondering what an event loop is and what clock does in this situation, like what is clock.tick(60)? I don't understand any explanations online
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time.
That meas that the loop:
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
runs 60 times per second.
for event in pygame.event.get() handles the internal events an retrieves a list of external events (the events are removed from the internal event queue).
If you press the close button of the window, than the causes the QUIT event and you'll get the event by for event in pygame.event.get(). See pygame.event for the different event types. e.g. KEYDOWN occurs once when a key is pressed.
e.g. The following loop prints the names of the a key once it it pressed:
run = True
while run:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
print(pygame.key.name(event.key))

Pygame, screen only updates when exiting pygame window?

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.

Pygame not exiting on keypress

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.

Categories

Resources