我正在学习使用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())
Run Code Online (Sandbox Code Playgroud)
它实际上只是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
Run Code Online (Sandbox Code Playgroud)
......然后程序不会退出.我在这做错了什么?因为我注意到这段代码是用于过时的Python版本.
sys.exit()
Run Code Online (Sandbox Code Playgroud)
单独对pygame有点邪恶..退出pygame应用程序的正确方法是首先打破mainloop然后退出pygame然后退出程序.即.
while running == True:
# catch events
if event_type == quit:
running = False # breaks out of the loop
pygame.quit() # quits pygame
sys.exit()
Run Code Online (Sandbox Code Playgroud)
在我看来,你没有正确地抓住这个事件..它应该是
if event.type == pygame.QUIT:
Run Code Online (Sandbox Code Playgroud)
你可以在这里阅读更多关于pygame中的事件.
| 归档时间: |
|
| 查看次数: |
8942 次 |
| 最近记录: |