Issue with sys.exit() in pygame

2019-05-10 06:18发布

问题:

I am learning to use Pygame, and when I use sys.exit(), I run into a problem. Here is the code:

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()) 

It's really just the code from the pygame tutorial. The problem occurs when I actually try to exit, regardless of what event I try to use to 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

... And then the program doesn't exit. What am I doing wrong here? Because I did notice that this code was for an antiquated version of Python.

回答1:

sys.exit just throws an exception (a SystemExit exception). This has two unusual effects:

  1. It only exits the current thread in a multithreaded application
  2. The exception could have been caught.


回答2:

sys.exit() 

alone is a bit unholy with pygame.. the proper way to exit a pygame app is to first break out of the mainloop then quit pygame then quit the program. ie.

while running == True:
    # catch events
    if event_type == quit:
        running = False  # breaks out of the loop

pygame.quit()  # quits pygame
sys.exit()

also it seems to me that you aren't catching the event properly.. it should be

if event.type == pygame.QUIT:

you can read more about events in pygame here.



回答3:

I solved this problem and the right code is below:

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.


回答4:

I have read in some sources there is a conflict between the mainloop() in Tkinter which runs the Python shell and Pygame.init() which the sys.exit() command follows.

The suggestion was to run the game from the command line to get round the problem rather than load the game using run (F5) from the shell.

A good side effect of this was that in my space invaders game, which does a lot of variable updating: 35 times a second, was that the animation ran correctly whereas from the shell it ran poorly and was jerky.

If i use the following code:

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

the game exits correctly but leaves an error message in the shell which has no effect on the game and is largely redundant. It is just a little ugly. This does not happen from the command line.

Summary: try running the game from the command line to avoid Tkinter problems



标签: python pygame