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()
Title says it all. Is there a way with pygame to get keyboard input when the window is minimized?
Thanks.
Yes, although you have to have it focused.
import pygame
pygame.init()
pygame.display.iconify()
while 1:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
print(event.key)
Otherwise it seems to not be possible according to this answer and this answer
I have just downloaded pygame 1.9.2 for python 3.3 and the keydown function is not working. The IDLE shell keeps telling me the same error:
NameError: name 'KEYDOWN' is not defined
How do I solve this problem? I am kinda new to programming so could you please explain this to me.
NOTE: Sorry for deleting my previous answer but Stack Overflow just randomly posted my answer before I was done.
Well, there could be several things wrong. You could be using pygame.key.keydown(), which is wrong, and you should be using pygame.key.get_pressed().
But what I suspect is wrong with your code is that you're not using pygame.KEYDOWN properly.
If you're trying to do something while a key is being held down, use pygame.key.get_pressed(), and do something like this:
key = pygame.key.get_pressed()
if key[pygame.K_WHATEVER_KEY_YOU_WANT]:
# do code
and just repeat that for every key you want to check.
If you're just trying to check if a key is being pressed, use pygame.KEYDOWN with an if statement, then check if the key you want is being pressed in a nested if statement under the first if statement. Like so:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_WHATEVER_KEY_YOU_WANT:
#do code
I'm assuming you have an event variable your using to iterate over pygame.event.get() function that 'gets' events. If not then here is the code above in a game/window loop:
running = True # variable to control while loop
while running: # while the variable running is equal to true
for event in pygame.event.get(): # use the variable 'event' to iterate over user events
if event.type == pygame.QUIT: # test if the user is trying to close the window
running = False # break the loop if the user is trying to close the window
pygame.quit() # de-initialize pygame module
quit() # quit() the IDE shell
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_WHATEVER_KEY_YOU_WANT:
#do code
This is basically how you use pygame.KEYDOWN. It's probably not the only way however. I recommend (since you're new to programming) to read the pygame docs on their official website for more info about checking key input in pygame. And read some of the answers to this question.
I'm creating an android game in PyDev using pygame_sdl2 and RAPT, but I keep getting these errors on specific lines of code:
# V here V
elif ev.type == pygame.KEYDOWN and ev.key == pygame.K_AC_BACK:
which gives me:
Undefined variable from import: K_AC_BACK
I'm wondering if this is a pygame error or a PyDev error; some people seem to be having similar problems with PyDev itself (like so) but I tried those and nothing happens.
Could anyone please give me some direction on how to fix this?
With the latest version of Pygame at the time of writing, the problem is not reproducible. The following minimal example runs without any problems:
import pygame
screen = pygame.display.set_mode()
clock = pygame.time.Clock()
run = True
while run:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
run = False
elif ev.type == pygame.KEYDOWN and ev.key == pygame.K_AC_BACK:
print('K_AC_BACK')
pygame.display.update()
clock.tick(60)
pygame.quit()
I'm working on a game. I have pygame imported.
I am using Python 3.3 and Pygame 3.3.
The same error message all the time "LEFT" is not defined.
I did exact copies of what were on the internet as I already did a search for the problem.
Here is my code. I have tried a few different ways and neither seem to work.
method 1:
import pygame
event = pygame.event.poll()
if event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
....command
method 2: (basically going all in)
from pygame import *
from pygame.locals import *
import pygame
event = pygame.event.poll()
if event.type == MOUSEBUTTONDOWN and event.button == LEFT:
......command
Before telling me, on both those methods I tried switching between "LEFT" and "pygame.LEFT", and so on.
Any ideas?
Hugely appreciated.
Define LEFT yourself:
LEFT = 1
The Pygame tutorial I think you are following does exactly that. In other words, the pygame library has no constants for this value, it's just an integer.
As Martijn Pieters pointed out, the pygame library has no constants for the possible values of event.button.
Here's what I think is a complete list of the possible values (as a class, so it won't clutter your global namespace):
class MouseButtons:
LEFT = 1
MIDDLE = 2
RIGHT = 3
WHEEL_UP = 4
WHEEL_DOWN = 5