То, что окно остается открытым в IDLE, является проблемой для tkinter, оно должно фактически закрыться немедленно, потому что программа завершается после последней строки, интерпретатор Python выключается и вызывает pygame.quit
.
Если вы хотите оставить окно открытым, вы можете использовать цикл while
, в котором вы обрабатываете события, игровую логику и рисование и, наконец, обновляете отображение с помощью pygame.display.flip
.
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
BG_COLOR = pygame.Color('gray12')
done = False
while not done:
# Handle the events.
for event in pygame.event.get():
# If the 'x' button of the window gets clicked.
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
print('mouse button pressed')
# Insert game logic here.
# Draw everything.
screen.fill(BG_COLOR)
# Flip the display buffer.
pygame.display.flip()
# Limit the frame rate to 60 FPS.
clock.tick(60)
# This `pygame.quit` call is only needed to close the window in IDLE.
pygame.quit()