Python Forum
[Tkinter] Tkinter/Turtle Stopping and Starting Timers Cleanly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Tkinter/Turtle Stopping and Starting Timers Cleanly
#2
I think the problem is clear if you put the stop_bomb_drop cod inside the __continue_bomb_drop func.
def __continue_bomb_drop():
    global bomb_dropping, bomb_timer_id
    bomb.goto(bomb.xcor(), bomb.ycor() - 12)
    if bomb.ycor() < - height // 2 or bomb_collision():
        bomb.hideturtle()
        screen.onkey(start_bomb_drop, "space")
        canvas.after_cancel(bomb_timer_id)
    bomb_timer_id  = canvas.after(BOMB_DELAY, __continue_bomb_drop)
Notice that even if you stop dropping the bomb you continue executing the __continue_bomb_drop. See what happens if you use an else statement so either you stop the bomb or you continue, not both.
def __continue_bomb_drop():
    global bomb_dropping, bomb_timer_id
    bomb.goto(bomb.xcor(), bomb.ycor() - 12)
    if bomb.ycor() < - height // 2 or bomb_collision():
        bomb.hideturtle()
        screen.onkey(start_bomb_drop, "space")
        canvas.after_cancel(bomb_timer_id)
    else:
        bomb_timer_id  = canvas.after(BOMB_DELAY, __continue_bomb_drop)
The second and subsequent bombs dropped faster because you are calling the continue twice as often. At first I was really confused by why each bomb didn't drop faster than the previous, but then I realized the way you stopped and started the bombs prevented more than two "timer loops" from forming. If you had don't this on purpose it would be a really clever bit of programming (hopefully with a lot of comments).
Reply


Messages In This Thread
RE: Tkinter/Turtle Stopping and Starting Timers Cleanly - by deanhystad - Apr-01-2020, 04:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle / turtle question DPaul 2 2,158 Oct-04-2020, 09:23 AM
Last Post: DPaul
  [Tkinter] Tkinter/turtle doevents DPaul 0 1,811 Sep-24-2020, 07:59 AM
Last Post: DPaul
  tkinter window and turtle window error 1885 3 6,715 Nov-02-2019, 12:18 PM
Last Post: 1885
  [Tkinter] Is there a way to sleep without stopping user input? GalaxyCoyote 2 2,136 Oct-23-2019, 06:23 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020