我使用的代码来自极客健忘网站
https://www.geeksforgeeks.org/create-a-snake-game-using-turtle-in-python/
当我关闭乌龟窗口时,我不得不在另一个问题上回答,但它仍然对我不起作用。
对错误的描述:
Traceback (most recent call last):
File "c:/Users/Zxed/Documents/1. DOCUMENT/Coding/Python/Game/snake.py", line 156, in <module>
snake()
File "c:/Users/Zxed/Documents/1. DOCUMENT/Coding/Python/Game/snake.py", line 88, in snake
layar.update()
File "C:\Users\Zxed\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1303, in update
t._update_data()
File "C:\Users\Zxed\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "C:\Users\Zxed\AppData\Local\Programs\Python\Python38-32\lib\turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
发布于 2021-01-18 05:24:17
您引用的GeeksForGeeks示例实现得很糟糕。它使用的是while True:
和time.sleep(delay)
,它们在像海龟这样的事件驱动环境中没有任何位置。which提供了ontimer()
事件来更好地处理这种情况,这将允许您干净地退出程序。
这里有一个SO example with the same issue,您应该能够通过搜索找到它。
发布于 2022-01-01 20:36:40
我同意GeeksForGeeks实现不是最优实现的观点。然而,考虑到它是一个学习网站,在您成为编写事件驱动代码的专家之前演示了一个基本的游戏化学习代码(甚至测试两者之间的差异),一个潜在的解决办法是使用while True:
,您可以使用while turtle.TurtleScreen._RUNNING==True
并将其余的循环例程封装在try以外或try以外。事件驱动是正确的方法。
while turtle.TurtleScreen._RUNNING==True
try:
wn.update()
...
...
time.sleep(delay)
except:
pass #Or Exception handling
由于您已经要求解释,当您关闭窗口时,屏幕将不运行,这将使turtle.TurtleScreen._RUNNING
标志变为假。在下一次更新期间,这会引发结束符错误。
https://stackoverflow.com/questions/65762189
复制相似问题