pygame window keeps not responding - python

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!

Related

Pygame window does not close properly when clock.tick in the loop

I'm working on a project and when I add the clock.tick to my main game loop, my pygame window doesnt close.
def game_loop():
"""The main game loop that runs as the game runs. Returns when the pygame window is closed."""
global running
global timer
while running:
while timer > screen.fixed_fps:
fixed_update()
timer -= screen.fixed_fps
update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
return
screen.clock.tick(screen.fps)
timer += delta_time()
pygame.quit()
return
When I click the X, the screen freezes until I let go, but unless I click the X in a very specific time frame( I usually need to click it 20 times to close) it doesnt work.
This is likely because of the way you are handling the QUIT event in your event loop. The pygame.event.get() function returns all of the events that have occurred since the last time it was called, so if there is a delay in your loop, the QUIT event may not be handled until multiple events have accumulated. To fix this, you should move the QUIT event handling code to the top of your event loop so that it is handled as soon as the event occurs. Also you could try to add pygame.display.update() after the event handling to make sure it is updated.
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
return
while timer > screen.fixed_fps:
fixed_update()
timer -= screen.fixed_fps
update()
screen.clock.tick(screen.fps)
timer += delta_time()

Initiating a controller in pygame causes script to stop

I've tried a few examples of pygame being used with controllers and they all seem to encounter the same issue, I can't figure out if I haven't installed pygame correctly or if im doing something else wrong.
import pygame
pygame.joystick.init()
for index in range(pygame.joystick.get_count()):
joystick = pygame.joystick.Joystick(index)
print(joystick) # <- does actually return a controller
while True:
pass # <- runs a few times and then stops
See PyGame window not responding after a few seconds. 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.
e.g.:
while True:
pygame.event.pump()
or
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

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 window is not responding after just making it

I wanted to make a game using pygame and I started my code making the pygame window
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
But every time I run the program, my pygame window does not respond.
Does anyone know what is happening, this did happen to me on previous codes too but it wasn't all the time like it is now
you need a game loop to start the game and you need events to work on.
for python code i suggest:
import pygame
pygame.init()
pygame.display.set_mode((1500, 1000))
start_game = True
while start_game:
print("Game Started!")
and for the events you can use this code in the while loop:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
print("mouse button up pressed")
the source of this code is : RipTutorial
I assume that by 'my pygame window does not respond' you mean that you believe the window is not displaying. I think that you are just missing it because it is closing too quickly.
The window does display, but because your program ends right after you create the window, the window is immediately closed again. This is likely just happening so quickly that you are not noticing it. If you want to see this, then just put a call to sleep after opening the window and you will see the window sit there until the sleep expires and then it will close.
If you do not mean that and you are seeing the window but expecting it to respond, then I do not understand what you expect it to respond to. You do not have any code that tries to make it respond to anything.
EDIT:
I just noticed that #AnassABEA suggests that in a comment under his answer, though not in his answer. #AnassABEA you should update your answer to include this since I believe that is the actual answer to his question, not the missing event loop.
Add this below to your code:
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
Lines 4 to 5 of the code are for closing pygame when the window is closed.

Pygame : The game stops temporarily when I am moving the window

I'm a new in PyGame and I was making a game. Until I discovered that when you were moving the window to another place of your desktop for example, the game temporarily stops. But ticks of the game still running when I used pygame.time.get_ticks().
So I made a completely new program with only the necessary code and it does the same thing.
Can someone explain me why it does it and if we can resolve this problem?
import pygame
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('The Test Program')
running = True
update_counter = 1
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
print(str(update_counter) + " updates")
update_counter += 1
pygame.quit()
quit()
# So try to move the window across your screen and you will see that, prints will stop and they will resume when you will release the click
So someone tell me that was normal, so I think it's only on Windows but there are no solutions. I put a script that show a pause symbol on screen when the cursor leave the window to make it normal.

Categories

Resources