0

I made a Pong game with Turtle module, but got a problem that while the game was running, I closed the window suddenly and received a wall of traceback message:

(img)

Is there any way that I can exit the window during the game without getting error messages? I don't want to use Screen.exitonclick because I think it is not user-friendly.

You can take a look at my source code at: https://github.com/AnhQuoc533/Pong-Game/tree/sandbox

Javad
  • 2,033
  • 3
  • 13
  • 23
Gold_Leaf
  • 53
  • 7
  • I can't reproduce what you are speaking about. Closing window during the game doesn't create any error messages. Have you solved the problem already? – Claudio Jun 20 '22 at 06:43
  • Nice graphics though ... :) I have played the old good game last time at the railway station in the waiting area . on x-ray monitor ... it was quite long ago :) . – Claudio Jun 20 '22 at 07:16
  • @Claudio, sometimes when you suddenly exit the window while the game is still running, there is no error message showing up thanks to Screen.mainloop. However, if you exit fast enough when the ball is out of bound and the scoreboard is about to update, you will get the error. [link_1](https://i.stack.imgur.com/DIcEb.png) [link_2](https://prnt.sc/yEN0uvlzZZzz) – Gold_Leaf Jun 20 '22 at 16:10
  • OK ... with your description I was able to reproduce an Error. Not exactly the same, but of same kind. Maybe it is bug in the turtle module? – Claudio Jun 20 '22 at 16:38
  • Although in case of multiple Turtles you need the object oriented interface, but you don't need own heavily used and in my eyes unnecessary object oriented code. Maybe it would make sense to restructure the code and get rid of the classes where possible and where it helps to better see the code-flow? The changes proposed in the answer don't fix the problem in my own test, but ... the answer seem to be based on another program version as that which I use. – Claudio Jun 20 '22 at 18:55
  • As turtle is based on tkinter probably you can capture in your script the WM_DELETE_WINDOW protocol callback with which you maybe gain some hints where the Error comes from? – Claudio Jun 20 '22 at 19:19

1 Answers1

0

there is no error message showing up thanks to Screen.mainloop

I'm not sure what you mean by this. My analysis of your code is you get the errors because instead of mainloop() you use:

def play(self):
        while True:
            self.screen.update()
            time.sleep(1/FPS)
            ...

PongGame().play()

Which really should be (re)structured more like:

def play(self):
        ...
        self.screen.update()
        ontimer(play, 1000/FPS)

PongGame().play()

screen.mainloop()

Loops like while True:, and for that matter sleep(), don't belong in an event-based environment like turtle as they can block events from being handled, which may trickle up to the user as errors.

Another thing in your code to (re)consider:

def __init__(self):
    self.speed = 0

    super().__init__(random.choice(BALL_ICONS))

speed() is already a method of turtle, you're redefining it as a property. Calling super().__init__() could have invoked a call to speed as a method. These statements should be the other way around and you should rename speed to something like ball_speed.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • That is my old code. The lastest code should be in 'sandbox' branch that can be accessed from the [link](https://github.com/AnhQuoc533/Pong-Game/tree/sandbox) I shared above. However, you can re-enter and download the source code because I recently merged the lastest code to the 'main' branch. Sorry for the inconvenience and thank you for your help. – Gold_Leaf Jun 20 '22 at 20:50