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.