0

The code I'm use its from geekforgeeks website

https://www.geeksforgeeks.org/create-a-snake-game-using-turtle-in-python/

And the error happen when im close the turtle windows i have to do the answer in another question, but it still not work for me

the description of the error:

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
  • 1
    Hello, welcome to Stackoverflow! Can you provide some code on what you have tried? – Diogo Silva Jan 17 '21 at 15:03
  • 3
    Welcome to Stack Overflow! Does this answer your question? [Python Turtle.Terminator even after using exitonclick()](https://stackoverflow.com/questions/45534458/python-turtle-terminator-even-after-using-exitonclick) – Red Jan 17 '21 at 15:22
  • 1
    @DiogoSilva the code im used https://pastebin.com/hPHFVcMf or from this https://www.geeksforgeeks.org/create-a-snake-game-using-turtle-in-python/ – Vanness Pratama Jan 18 '21 at 03:51
  • 1
    @AnnZen eumm i have to try use turtleScreen._RUNNING = True, i put exactly in top op wn.mainloop(), but it still have that error – Vanness Pratama Jan 18 '21 at 04:13

2 Answers2

0

The GeeksForGeeks example you cited is implemented poorly. It uses while True: and time.sleep(delay) which have no place in an event-driven environment like turtle. Turtle provides the ontimer() event to better handle this situation, which will allow you to exit the program cleanly.

Here's an SO example with the same issue that you should have been able to find with a search.

cdlane
  • 40,441
  • 5
  • 32
  • 81
0

I agree with cdlane that GeeksForGeeks implementation is not an optimal implementation. However, considering it is a learning website and demonstrates a basic gamified learning code before you become expert in writing event-driven code (or even to test the difference between both), a potential workaround is instead of while True: you could use while turtle.TurtleScreen._RUNNING==True and enclose the rest of the loop routine within try except finally or try except. event-driven is the right way to go.

while turtle.TurtleScreen._RUNNING==True
    try:
        wn.update()
        ...
        ... 
        time.sleep(delay)
    except:
        pass #Or Exception handling

Since you have asked for an explanation, when you close the window the screen is not running and this makes the turtle.TurtleScreen._RUNNING flag to become false. During the next update this throws the Terminator error.

William
  • 464
  • 2
  • 16