1

I'm creating a simple two-player board game where each player must place pieces on their own boards. What I would like to do is by either:

  • opening a new terminal window (regardless which OS the program is run on) for both players so that the board is saved within a variable but the other player cannot scroll up to see where they placed their pieces.

  • clearing the current terminal completely so that neither player could scroll and see the other player's board. I am aware of the unix 'clear' command but it doesn't achieve the effect I'm after and doesn't work with all OS's (though this might be something that I'll have to sacrifice to get a working solution)

I have tried clearing the screen but haven't been able to completely remove all the text. I don't have a preference; whichever method is easier. Also, if it would be easier to use a different method that I haven't thought of, all other suggestions are welcome. Thanks in advance!

EDIT: Other solutions give the appearance that text has been cleared but a user could still scroll up and see the text that was cleared. I'd like a way to remove any way that a user could see this text.

EDIT 2: Please read the other answers and the comments as they provide a lot of information about the topic as a whole. In particular, thanks to @zondo.

braX
  • 11,506
  • 5
  • 20
  • 33
  • Possible duplicate of [clear terminal in python](http://stackoverflow.com/questions/2084508/clear-terminal-in-python) – zondo Mar 05 '16 at 11:50
  • This still allows a user to scroll back up and see the supposedly cleared text. I'm looking for a solution where the user cannot see this text again if that is possible? –  Mar 05 '16 at 11:53
  • Don't just read the top answer. There are answers in there that will do what you want. For example, `print('\033c')` works for me on Linux. – zondo Mar 05 '16 at 11:55
  • Sorry, I didn't see that. It does work for me on linux but does it work on other OS's? –  Mar 05 '16 at 11:58
  • I don't know. I don't have any others to test it on. – zondo Mar 05 '16 at 11:59
  • Doesn't work on windows 10 –  Mar 05 '16 at 12:16
  • Is this a sockets programming game? Or would both players play on the same console? – Mixone Mar 05 '16 at 15:10
  • @zondo: it does work on Windows if you [call `colorama.init()`](https://pypi.python.org/pypi/colorama) before `print('\x1b[H\x1b[J')` ([the result of `clear` command on my system](http://stackoverflow.com/a/34388642/4279)). (note: `'\033' == '\x1b'`) – jfs Mar 05 '16 at 15:32
  • @MichaelMorgan: are you sure the screen is not erased from the scroll buffer? If I call `os.system('cls')` on Windows 7 inside a Windows console running `cmd.exe` that runs `python.exe` then the scroll buffer is cleared too. – jfs Mar 05 '16 at 15:41

4 Answers4

2

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 printing 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.

das-g
  • 9,718
  • 4
  • 38
  • 80
  • I just saw in the documentation that the Windows version of Python doesn't include the `curses` module. [Newer versions of the tutorial](https://docs.python.org/3/howto/curses.html#what-is-curses) mention the [`UniCurses` package](https://pypi.python.org/pypi/UniCurses) as an alternative. – das-g Mar 05 '16 at 12:42
  • Perhaps OP doesn't need `curses` (its Windows analog) and something lighter such as `colorama` + `blessings` would be enough. – jfs Mar 05 '16 at 15:44
1

I'd personally just use this.

import os
os.system("cls" if os.name == "nt" else "clear") #"cls" for Windows, otherwise "clear"
GarethPW
  • 399
  • 1
  • 3
  • 11
  • I have tried this but you can still scroll up and see anything printed beforehand. E.g. using a for loop to print some quantity of text and then using os.system('cls' if os.name == 'nt' else 'clear') would appear to clear the terminal but a user could still just scroll up and see the text anyway. –  Mar 05 '16 at 11:51
  • @MichaelMorgan Interesting. I didn't realise that was the case. – GarethPW Mar 06 '16 at 00:58
  • @MichaelMorgan: as [I said](http://stackoverflow.com/questions/35813667/python-hide-already-printed-text-solved#comment59299691_35813667), the answer works fine for me. – jfs Mar 09 '16 at 20:43
0

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.

  • I have plans to develop a GUI around it but I was curious if there was a way to achieve what would be simple(ish) in a graphical environment but within a text-based one instead. –  Mar 05 '16 at 12:18
  • There is a difference between clearing the terminal screen and asking a printer to remove ink off a page: clearing the terminal screen has been done in many ways. – zondo Mar 05 '16 at 12:19
  • Teach me your wondrous ways @zondo I know nothing of this. As far as I was aware there was no way of actually doing this with simplistic code. – That One Random Scrub Mar 05 '16 at 12:29
  • They aren't *my* wondrous ways :). I gave a link below the question to a very similar one. The accepted answer clears only what's visible, but there are other answers that clear everything. – zondo Mar 05 '16 at 12:30
0

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 import init
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 = 1
print("Hello world!")
print("\033[<{0}>A".format(numLines), "This came AFTER hello world line")
Mixone
  • 1,327
  • 1
  • 13
  • 24
  • 1
    For me, I had to remove the <> from the ANSI code ("\033[{0}A".format(numLines)). –  Mar 07 '16 at 19:14
  • ANSI escape codes behave somewhat differently from one OS to another sometimes, I find that the best methode with it most of the times is try and error – Mixone Mar 07 '16 at 21:29