In Python turtle, with tracer off, is there a way to update the screen without using screen.update()?
Asked Answered
C

1

0

I'm new to programming in Python. About three weeks ago I finished a Tetris clone using the turtle module to test my skills. I was under the impression that when I turned off the animation tracer, I had to manually update the window with the update() method. Yesterday, while coding a snake clone with the turtle module, I had to explain to a friend why I was manually updating the screen. For proof, I opened Tetris and commented out the update() methods to show that the game would not work. To my surprise, it actually worked and I can't get my head around how it did as even my snake code would not work without the update() method.

Here is a part of the main loop of the Tetris code:

while not game_over:

if not current_piece.does_piece_fit(starting_row + 2, starting_column, current_piece.current_piece):
    game_over = True
wn.update()
movement()
marco.update_grid()
marco_next.show_next_piece(next_piece)
show_score()
current_piece.proyeccion()
marco_next.show_text(x_text_next_piece, y_text_next_piece, "Next Piece:", normal_style)
marco_next.show_text(x_text_hold, y_text_hold, "Hold:", normal_style)
show_instructions(x_text_instructions, y_text_instructions, instructions_text, smaller_style)

if holding:
    marco_hold.show_hold_piece(hold_piece)

If the wn.update() line is commented out, it shouldn't work, but somehow it does. The code is a bit lengthy and I'm new on this site so I don't know if posting it entirely might be considered bad or rude on my part. Thanks for reading this, sorry for the bad english and messy code.

Chou answered 2/8, 2020 at 19:55 Comment(1)
create minimal working example with this problem. Show how you turn off tracing. As I remeber you can turn off totally or set how often it redraws screen so it may still works without update(). Or maybe there are some functions with may force turtle to redraw screen. For example update_grid()Crampton
B
1

Some turtle commands call update() independent of you calling it. Some like end_fill() do it directly, some like dot() do it indirectly due to other methods they invoke:

from turtle import *

tracer(False)

dot(100)  # change 'dot' to 'forward' and run again

done()

If you run the above, you'll get a dot despite the tracer(False) call. If you change the dot to forward you'll get a blank screen unless you comment out the call to tracer.

My rules of thumb for update() are:

  • don't assume that anyone will call it for you

  • don't assume that you are the only one calling it

This is not just true for Python turtle. My advice, put back your update() calls where you believe you need them, otherwise a future update of turtle might break your code.

Bostow answered 2/8, 2020 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.