FrequentlyAskedQuestions — wiki
Frequently asked questions regarding pygame and development with pygame. If you do not find your development question answered here, you might want to take a look at the CookBook or read the documentation and tutorials once more.
The Pygame documentation is also installed with Pygame itself. They are located in site-packages/pygame/docs and can be accessed with:
python -m pygame.docs
In IDLE why does the Pygame window not close correctly?¶
This is caused by the IDLE python interpreter, which seems to keep the references around somehow. Make sure, you invoke pygame.quit() on exiting your application or game.
# ... running = True
while running:
event = pygame.event.wait ()
if event.type == pygame.QUIT:
running = False # Be IDLE friendly
pygame.quit ()
and if you also want to close the window on exceptions you might solve it like this:
try:
main()
except Exception, e:
tb = sys.exc_info()[2]
traceback.print_exception(e.__class__, e, tb)
pygame.quit()
On Windows if you open a python file with a ">right-click: Edit with Idle" the editor and interactive prompt both run from the same Python process. To make sure the game runs in a separate process first start IDLE from the Start Menu (it is under Python X.X in the programs section) then open the edit window for the game.
SDL, on which Pygame is based, uses the environment variable SDL_VIDEO_CENTERED to indicate the windows should be centered. Note the environment variable is set before Pygame is initialized by the call to init().
import pygame
import os
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
try:
screen = pygame.display.set_mode((200, 200))
# This should show a blank 200 by 200 window centered on the screen
pygame.display.flip()
while True:
event = pygame.event.wait()
if event.type == pygame.QUIT:
break
finally:
pygame.quit() # Keep this IDLE friendly
So why is this undocumented? While SDL recognizes several environment variable that set up initial conditions, their use is unofficial. But they have been around long enough that they are unlikely to change now. A list of environment variable recognized by SDL 1.2 is provided. Other configurable parameters include which video and audio drivers to use as well as the id of existing window to draw within.
How do I set the video driver Pygame uses?¶
SDL, on which Pygame is based, checks the environment variable SDL_VIDEODRIVER for a video driver to use. If not found it uses a default for the operating system. This code snippet sets the driver to "windib" for Windows. Note the environment variable must be set before Pygame is initialized by the call to pygame.init().
import os
import platform
if platform.system() == 'Windows':
os.environ['SDL_VIDEODRIVER'] = 'windib'
# rest or program goes here.
A list of supported video drivers is provided here. See the "How do I centering the pygame window on the screen?" above for more information.
The initial position of the Pygame window can be set with the SDL_VIDEO_WINDOW_POS environment variable. It takes a string, two comma separated integer values. The position is the upper left corner of the window in screen coordinates.
import os
position = 100, 50
os.environ['SDL_VIDEO_WINDOW_POS'] = str(position[0]) + "," + str(position[1])
See "How do I centering the pygame window on the screen?" above for more information.
What environment variables does Pygame/SDL recognize?¶
See "How do I center the pygame window on the screen?" above.
Miscellaneous¶
Does Pygame work with Python 3?¶
Yes. Pygame 1.9.2 supports Python 3.2 and up. Only the orphaned _movie module (not built by default) does not.
Why do the draw functions leave unfilled pixels (holes) in thick circles or arcs?¶
Why does drawing thick circles or arcs give "moire patterns"?¶
The Pygame draw package uses a crude, aliasing, algorithm to draw single pixel width circles. Thicker circles are composed of multiple single pixel width circles. There is not much overlapping of circles so, regrettably, it leaves holes.
Pygame 1.9.0 now includes the SDL_gfx package. This includes a filled-ellipse draw function so a thick circle can be built up from two concentric filled circles.