[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] pygame.quit()



Hello

Under WinXP starting a pygame program from command prompt that does not use the pygame.quit() function will hang while quitting! Well documentation sais:

Uninitialize all pygame modules that have previously been initialized. When the Python interpreter shuts down, this method is called regardless, so your program should not need it, except when it wants to terminate its pygame resources and continue. It is safe to call this function more than once: repeated calls have no effect.

Note, that pygame.quit will not exit your program. Consider letting your program end in the same way a normal python program will end.


I do not know, why it will hang under WinXP (startet at command prompt)? Inserting the line "pygame.quit()" does the trick.



~DR0ID




#-------------Example 1-------------------------------------------------
while 1:
            for event in pygame.event.get():
                print "event"
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == pygame.KEYDOWN and event.key == pygame.KEY_ESCAPE:
                    sys.exit()


#-------------Example 2-------------------------------------------------
while 1:
            for event in pygame.event.get():
                print "event"
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == pygame.KEY_ESCAPE:
                    return


#-------------Example 3-------------------------------------------------
looping = True
while looping:
            for event in pygame.event.get():
                print "event"
                if event.type == pygame.QUIT:
                    looping = False
                if event.type == pygame.KEYDOWN and event.key == pygame.KEY_ESCAPE:
                    looping = False

#------------------------------------------------------------------------


I think it is not a bad idea to clean up at the end. I use a variable to control the loop. In that way I can be sure to catch all exit paths:
#--------------------------------------------------------------
looping = True
while looping:
            for event in pygame.event.get():
                print "event"
                if event.type == pygame.QUIT:
                    looping = False
                if event.type == pygame.KEYDOWN and event.key == pygame.KEY_ESCAPE:
                    looping = False

# single exit path, clean up your stuff or do whatever needed
pygame.quit()

#--------------------------------------------------------------