I am currently making a memory game where I print a few words and after a given time the words are shuffled and then one word is removed and replaced with a new one. Then I would get the user to answer which word has been removed and which word has then replaced that word. For instance, it will print:
CAT DOG MOUSE HORSE
And after 10 seconds, I would like those words to be hidden, be shuffled and replace one word with a new word so that it prints, for example:
DOG HORSE RABBIT CAT
I understand that I can use time.sleep()
for the program to suspend the execution of any other code.
Would it be easier to essentially "unprint" the first set of words and then print the new one OR replace the first printed set of words with the new one.
In Python, data hiding is achieved using a feature of double underscore (__) as a prefix. This initiates the hiding feature in the attribute. As the attribute becomes inaccessible for the users.
Here's a tip: '\r' erases what has been printed on the current line, and put the cursor back to column one.
Summary: The most straightforward way to overwrite the previous print to stdout is to set the carriage return ( '\r' ) character within the print statement as print(string, end = "\r") . This returns the next stdout line to the beginning of the line without proceeding to the next line.
You'll need to use a library that lets you write to screen coordinates. I've written the doscmd-screen package to do this (http://doscmd-screen.readthedocs.org/), and it would work something like:
import screen, time
scr = screen.Screen()
scr.writexy(scr.left, scr.top, "CAT DOG MOUSE HORSE")
time.sleep(10)
scr.writexy(scr.left, scr.top, " " * scr.width)
Note: the package was originally only for dos, but now also supports *nix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With