【问题标题】:Issue with sys.exit() in pygamepygame 中的 sys.exit() 问题
【发布时间】:2012-07-14 23:16:53
【问题描述】:

我正在学习使用 Pygame,当我使用 sys.exit() 时,我遇到了问题。代码如下:

import pygame, sys,os
from pygame.locals import * 

pygame.init() 

window = pygame.display.set_mode((468, 60)) 
pygame.display.set_caption('Game') 
screen = pygame.display.get_surface() 

file_name = os.path.join("data","image.bmp")

surface = pygame.image.load(file_name)

screen.blit(surface, (0,0)) 
pygame.display.flip() 

def input(events): 
   for event in events: 
      if event.type == QUIT: 
         sys.exit(0) 
      else: 
         print event 

while True: 
   input(pygame.event.get()) 

这实际上只是 pygame 教程中的代码。当我实际尝试退出时会出现问题,无论我尝试对sys.exit() 使用什么事件。

Traceback (most recent call last):
  File "C:/Python27/Lib/site-packages/pygame/examples/test.py", line 25, in <module>
    input(pygame.event.get())
  File "C:/Python27/Lib/site-packages/pygame/examples/test.py", line 20, in input
    sys.exit(0)
SystemExit: 0

... 然后程序不退出。我在这里做错了什么?因为我确实注意到这段代码是针对 Python 的过时版本的。

【问题讨论】:

    标签: python pygame


    【解决方案1】:
    sys.exit() 
    

    单独使用 pygame 有点邪恶。退出 pygame 应用程序的正确方法是先跳出主循环,然后退出 pygame,然后退出程序。即。

    while running == True:
        # catch events
        if event_type == quit:
            running = False  # breaks out of the loop
    
    pygame.quit()  # quits pygame
    sys.exit()
    

    在我看来,你没有正确地捕捉到这个事件..它应该是

    if event.type == pygame.QUIT:
    

    您可以在 pygame here 中阅读有关事件的更多信息。

    【讨论】:

    • 在我看来你没有正确地捕捉到事件:不,他导入了 pygame.locals,所以event.type == QUIT 完全没问题。但是你的回答是正确的,你应该先退出主循环。
    • 这是我学习 pygame 时的答案,它让我很头疼。
    【解决方案2】:

    如果您仍然有问题,(在中断循环后)尝试使用sys.exit(0) 而不是sys.exit()。希望它会有所帮助。它对我有用。 pygame 似乎希望显式传入“状态”参数(即此处的 0)。

    请看下面的例子:

    isRunning = True
    while isRunning:
    # Catch events
    if event.type == pygame.QUIT:
        isRunning = False # Breaks the loop
    
    pygame.quit()
    sys.exit(0)
    

    【讨论】:

      【解决方案3】:

      sys.exit 只是抛出一个异常(SystemExit 异常)。这有两个不同寻常的效果:

      1. 它只退出多线程应用程序中的当前线程
      2. 异常可能已被捕获。

      【讨论】:

        【解决方案4】:

        我解决了这个问题,正确的代码如下:

        running = True
        while running == True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    running = False  # Exiting the while loop
        
            screen.blit(background, (0,0))
            pygame.display.update()
        
        pygame.quit() # Call the quit() method outside the while loop to end the application.
        

        【讨论】:

          【解决方案5】:

          我在一些资料中读到,运行 Python shell 的 Tkinter 中的 mainloop() 和 sys.exit() 命令遵循的 Pygame.init() 之间存在冲突。

          建议是从命令行运行游戏来解决问题,而不是在 shell 中使用 run (F5) 加载游戏。

          这样做的一个很好的副作用是,在我的太空侵略者游戏中,它进行了大量的变量更新:每秒 35 次,动画运行正确,而从 shell 中运行很差并且很生涩。

          如果我使用以下代码:

          if event.type == QUIT:
              pygame.quit()
              sys.exit()
          

          游戏正确退出,但在 shell 中留下一条错误消息,这对游戏没有影响,而且在很大程度上是多余的。只是有点难看。这不会从命令行发生。

          总结:尝试从命令行运行游戏以避免Tkinter问题

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-01
            • 2023-02-20
            • 2021-04-02
            相关资源
            最近更新 更多