I'm tinkering around with pygame right now, and it seems like all the little programs that I make with it hang when I try to close them.
Take the following code, for example:
from pygame.locals import *
pygame.init()
# YEEAAH!
tile_file = "blue_tile.bmp"
SCREEN_SIZE = (640, 480)
SCREEN_DEPTH = 32
if __name__ == "__main__":
screen = pygame.display.set_mode(SCREEN_SIZE, 0, SCREEN_DEPTH)
while True:
for event in pygame.event.get():
if event.type == QUIT:
break
tile = pygame.image.load(tile_file).convert()
colorkey = tile.get_at((0,0))
tile.set_colorkey(colorkey, RLEACCEL)
y = SCREEN_SIZE[1] / 2
x = SCREEN_SIZE[0] / 2
for _ in xrange(50):
screen.blit(tile, (x,y))
x -= 7
y -= 14
I don't see anything wrong with the code, it works (ignore the fact that the tile isn't blit in the right spots), but there's no traceback and the only way to close it is to kill the python process in Task Manager. Can anyone spot a problem with my code?
I had the same problem, but solved it by doing the following:
try:
while True:
for event in pygame.event.get():
if event.type==QUIT or pygame.key.get_pressed()[K_ESCAPE]:
pygame.quit()
break
finally:
pygame.quit()
'if event.type==QUIT' generates a syntax error. Should be == pygame.QUIT Also, the rest of the line is incorrect but I can't see how. There's a cleaner variant here:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
If you are running it from IDLE, then you are missing pygame.quit().
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.
See: In IDLE why does the Pygame window not close correctly?
And also: Pygame Documentation - pygame.quit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With