pygame is
Python
Simple DirectMedia Layer
 
 
pygame.org is
Site Swing
Wiki

FrequentlyAskedQuestions

      
Search:  
 
 
sections
Beginning Programmers
Where can I find simple Pygame examples?
In IDLE why does the Pygame window not close correctly?
How do I set the video driver Pygame uses?
What environment variables does Pygame/SDL recognize?
Miscellaneous
Does Pygame work with 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"?

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.

Beginning Programmers

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

What environment variables does Pygame/SDL recognize?

Miscellaneous

Does Pygame work with 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 twilight 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.

spotlight

 
our projects
pygame.org welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
 
recent releases
Jul 28, 2014

Jul 22, 2014

Jul 21, 2014

Jul 20, 2014

Jul 19, 2014

Jul 15, 2014

Jul 10, 2014

Jul 9, 2014

Jun 27, 2014



Jun 24, 2014

... more!
 
for pygame related questions, comments, and suggestions, please see help (lists, irc)