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
or, for python 2.6
python -m pygame.docs.__main__These docs are the most recent version for the Pygame they come with.
Where can I find simple Pygame examples?
The book Making Games with Python & Pygame has the source code for several simple games (Tetris, Bejeweled, Nibbles, etc.) with detailed explanation.
The Pygame documents folder in the download contains several examples. These examples include the chimp program from the tutorials as well as two versions of aliens.
The Pygame Cookbook has some drop in code for doing various tasks in Pygame.
Another set of cookbook style example code is available from Simpson College here, this code also links to an on-line text that explains the answers and beginning coding concepts.
The Pygame Projects pages contain programs with a wide range of sophistication, starting with beginning programmers first attempt. Have a look at the arcade and 2d tags. There are many versions of pong and snake. The source code for the book GAME PROGRAMMING by Andy Harris [ISBN-13: 978-0-470-06822-9] is here. The first Pygame example is in chapter four, "Building a Game Example
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() 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) + "," + str(position)
See "How do I centering the pygame window on the screen?" above for more information.
What environment variables does Pygame/SDL recognize?
Does Pygame work with Python 3?
Mostly yes. Only the scrap, _movie, surfarray for Numeric, and threads modules have not been ported. The Pygame alternative, pgreloaded (Pygame Reloaded), also supports Python 3.
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.