How do I make a code that resets an output line when it is occupied by a print

0

Let's say: I have a code with pygame

print(pygame.mouse.pos) #se iprime un nuevo texto cada vez que la posicion de lursor dentro de la ventana cambie

I need to know how to make the text print in one place

30, 50, then in its place it comes out 31,49, etc. feel free to ask for more details

instead of

hola
hola
hola
hola

I want hello to be updated in the same line of the output

hola #esto sale varias veces en el mismo lugar
    
asked by Gabriel Mation 19.09.2018 в 02:50
source

1 answer

0

If that print() is the only one you have (or the one that comes out on the last line of the terminal) a simple way to achieve what you're looking for would be to move the cursor back to the beginning of the line before printing. That way what you print will overwrite what was there.

To move the cursor back to the beginning of a line, the character to be sent to the terminal is \r . At the same time you must make sure that Python does not send a "new line" character, for which you must use the extra end="" parameter in the print() (Python3) or a comma at the end of print (Python2).

For example (python3):

import time

for i in range(10,0,-1):
   print("\rCuenta atrás: {}      ".format(i), end="")
   time.sleep(1)
print("\nDespegue!")
    
answered by 19.09.2018 / 13:16
source