问题描述
嗨,我正在编写一个简单的国际象棋程序,不幸的是,我遇到了一些意想不到的问题。在添加了一个跟踪所有图形位置的列表之后,我无法使用到目前为止使用的方法关闭该窗口。原来是
for event in pygame.event.get():
# print(event)
# Checking if the user clicks the red quit cross
if event.type == pygame.QUIT:
# run control the mainloop,wheather True or not
run = False
添加列表后,此操作停止了,因此我现在使用:
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
我尝试添加一些异常处理:
try:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
except pygame.error():
print('Program closed')
但是未达到except语句并显示错误:(pygame.error:视频系统未初始化)
能否请您告诉我如何正确处理此异常,或者提出另一种制动主回路的方法。
解决方法
从异常捕获中删除(),即:
apply plugin: 'io.fabric'
代替except pygame.error
我大概是自己弄清楚的,以防万一有人在找答案。问题是我试图在pygame.display.quit()之后调用重绘函数。因此,在同时移动重绘调用和keys = pygame.key.get_pressed()[也利用pygame模块]之后,程序将终止,而不会发生任何错误调用。该代码应如下所示:
while run:
pygame.time.delay(100)
window_redrawing()
# Gathering user keyboard input
keys = pygame.key.get_pressed()
# making sure the window can be closed
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
run = False
fig_pos.clear()