working with the current version of python after a quick google search and a perusal of stack overflow I'm not seeing what I'm looking for. I'm trying to find a way to clear the screen with every new print to the screen. So you would get the intro text for a room and there is a monster in the room so when you press a to attack it would delete the intro text and the new text saying what weapon you used and how much damage you did would be printed. then when the monster dies you the damage prompts would go away and you would get the dead enemy empty room text with the press these keys to go a direction text. the current version of my game is here https://github.com/GusRobins60/AdventureGame.git hopefully that will give you an idea of the text i'd be displaying. thanks for any help.
clearing the terminal for my python text adventure
287 Views Asked by Gus Robins At
3
There are 3 best solutions below
0

Are you using windows? If so, you can try:
import os
os.system('cls')
Or something similar after each page is complete and needs to be cleared.
0

To clear the terminal using ANSI escape codes:
import sys
sys.stdout.write("\33[H\33[2J") #"\33" is the ESC character
sys.stdout.flush()
\33[H
moves the cursor to the top left of the terminal, and \33[2J
clears the screen. Using sys.stdout.write
here instead of print
avoids writing a newline, so the cursor stays on the first line of the screen, instead of being on the second line when using print()
.
For windows you can use a lambda function to easily create an anonymous function which you can use to clear the terminal again and again:
Or if you're on Linux then swap 'cls' with 'clear'.