while 1:
for event in pygame.event.get():
if event.type == QUIT: ## defined in pygame.locals
pygame.quit()
sys.exit()
if event.type == ## Handle other event types here...
## Do other important game loop stuff here.
# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://cs.simpson.edu/?q=python_pygame_examples
import pygame
# Define some colors
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
pygame.init()
# Set the height and width of the screen
size=[700,500]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
#Loop until the user clicks the close button.
done=False
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
# -------- Main Program Loop -----------
while done==False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
# Set the screen background
screen.fill(black)
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit ()
Mach1723的answer是正确的,但是我想建议主循环的另一种变体:
我推荐以下代码。首先,它包括时钟,这样你的程序就不会让CPU除了轮询事件什么也不做。其次,它调用pygame.quit(),防止程序在windows上空闲运行时冻结。
这是一个非常简单的问题,您需要处理“QUIT”事件,请参阅位于:http://www.pygame.org/docs/ref/event.html的事件文档
编辑: 我突然想到你可能正在处理“退出”事件,但它不起作用 但没有更多的细节你的代码我不知道。
处理“退出”事件的简单方法的快速示例:
相关问题 更多 >
编程相关推荐