I've seen several games made in Pygame, and none of them uses try
/finally
to call pygame.quit()
when it finishes.
Instead of writing:
def main():
pygame.init()
try:
...
finally:
pygame.quit()
and using sys.exit()
to quit, if there are multiple functions implementing different stages of the game (such as the menu and the actual game) with an event loop in each, at least one of them writes:
def terminate():
pygame.quit()
sys.exit()
and uses the terminate()
function to quit. This means that cleanup is not done if there was an error in the program unless the code that may cause the error calls pygame.quit()
if the error condition is met.
In a game I'm making, there is a part that raises an error if the level is specified incorrectly; this shouldn't happen if the user runs it normally, but I use it when making levels. (Currently, there is no easy level editor, and the level format is a bit tedious to work with: the file just specifies the name of each tile and their neighbours, so it's easy to get a file where there's incorrect connectivity or vertex singularity; however, implementing a better file format seems like more work that it's worth to me.) In this case, to quit correctly, I probably have to write pygame.quit()
before each raise, but try
/finally
should take care of it.
I tried braving that, but couldn't find anything mentioning both pygame.quit()
and try
/finally
.