Python Hide Already Printed Text
Solution 1:
Consider using a portable terminal handling library. They abstract away the system specifica of common tasks like erasing the "screen" (i.e. terminal), or placing output at a specific position on the "screen" (again, meaning the text terminal). However, to use such a library effectively, you often have to switch to its style of generating output on the screen instead of naively print
ing strings.
curses
is one such library (based on the C library ncurses) and included in the Python standard library. To get started, be sure to have a look at the curses
tutorial in the official Python documentation.
Solution 2:
I'd personally just use this.
import osos.system("cls"ifos.name == "nt"else"clear") #"cls"for Windows, otherwise "clear"
Solution 3:
I would recomend a simple ANSI escape code to move the cursor position, Cursor Escape Codes, to the start of the board everytime. There is also an ANSI escape code that completly clears the console though, so you can choose.
If you are on windows you must first import colorama
a module that makes windows prompt be able to use the ANSI codes as such:
import colorama # OR: from colorama importinit
colorama.init() # AND THEN: init()
So if your board has n
rows, after the user input for their turn, you move the cursor UP n
rows + however many were required for user input, so if you wrote Input row, col: ...
then you would go UP n+1
, etc...
A simple example:
numLines = 1print("Hello world!")
print("\033[<{0}>A".format(numLines), "This came AFTER hello world line")
Solution 4:
You may not like this, it's a bit higher level than a basic two player board game, but there is always using some sort of GUI. I personally like tkinter myself.
You don't want the option of people scrolling up to see printed text, but you can't remove what has been printed, that's like asking a printer to remove ink off a page. It's going to stay there.
Research a GUI interface, and try and make the game in that. Otherwise, you could let me take a stab at creating a explanatory piece of code that shows you how to use tkinter. If you do, link me the game you have so I can understand what you want.
Post a Comment for "Python Hide Already Printed Text"