Python 当我关闭Pygame时,它的屏幕会冻结
代码加载了一个pygame屏幕窗口,但是当我单击X关闭它时,它就没有响应了。我在64位系统上运行,使用32位python和32位pygamePython 当我关闭Pygame时,它的屏幕会冻结,python,pygame,Python,Pygame,代码加载了一个pygame屏幕窗口,但是当我单击X关闭它时,它就没有响应了。我在64位系统上运行,使用32位python和32位pygame from livewires import games, color games.init(screen_width = 640, screen_height = 480, fps = 50) games.screen.mainloop() 这是一个非常简单的问题,您需要处理“退出”事件,请参阅以下位置的事件文档: 编辑: 现在我突然想到,您可能正在
from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 50)
games.screen.mainloop()
这是一个非常简单的问题,您需要处理“退出”事件,请参阅以下位置的事件文档: 编辑: 现在我突然想到,您可能正在处理“退出”事件,但它不起作用 但是如果没有更多的代码细节,我不知道 处理“退出”事件的简单方法的一个简单示例: Mach1723是正确的,但我想建议主回路的另一种变体:
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.
在使用pygame时,您必须处理包括QUIT在内的所有事件,因此如果您不处理QUIT事件,您的程序将不会退出。这是密码
import sys
import pygame
from pygame.locals import *
def main():
running = True
while running:
for event in pygame.event.get():
if event.type==QUIT: #QUIT is defined at pygame.locals
runnning = False
#other game stuff to be done
if __name__=='__main__':
pygame.init()
pygame.display.set_mode((640,480))
main()
我推荐以下代码。首先,它包含时钟,这样你的程序就不会占用CPU,而只是轮询事件。其次,它调用pygame.quit(),以防止程序在windows上以空闲状态运行时冻结
# 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 ()
使用此选项效果最佳。一旦设置为false,就可以轻松保存任何状态。
# 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 ()