I am trying to make a text-based adventure game and I want to be able to have text self-delete itself.
For example, the game will use the print()
statement to print the text, which will then be deleted or hidden from the player after 5 seconds.
I’ve tried to find other people’s questions about this problem and can’t find any info. I don’t know what commands to use or anything related to that, please help.
ShuckleZeke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
Do you want to clear the whole terminal after the specified time?
import os
os.system('cls' if os.name == 'nt' else 'clear')
See original source here
If you want to clear just one line:
print("your game text here", end = 'r')
- use the
r
as the end character while displaying the text (which is supposed to be deleted after 5 seconds), - then print blank spaces (
print(" "*length)
) where length is the supposed max length of your game text
See original source here
Hope you find this useful!
2
Let’s do this by changing the Cursor position in the Windows console
befor you startd read about ctypes if you are not familiar with that
each character in console have position of X
& Y
so lets make class named COORD
for this like way documents says
import ctypes
class COORD(ctypes.Structure):
_fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]
def __init__(self, x, y):
self.X = x
self.Y = y
now lets make class named TextField
for your text with behavior you want
(show & hide)
import ctypes
from time import sleep
class TextField():
STD_OUTPUT_HANDLE = -11
hOut = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
text = ""
def __init__(self,x,y,maxlegnth):
self.pos = COORD(x,y)
self.maxlegnth = maxlegnth
def setText(self,text):
if len(text) > self.maxlegnth:
raise OverflowError(f"yout text legnth is {len(text)} but max legnth for this text field is {self.maxlegnth}")
self.text = text
def show(self,timelive=None):
# if timelive not be set text never will be deleted
ctypes.windll.kernel32.SetConsoleCursorPosition(self.hOut, self.pos)
print(self.text)
if timelive:
sleep(timelive)
del self
def clearText(self):
ctypes.windll.kernel32.SetConsoleCursorPosition(self.hOut, self.pos)
print(" "*self.maxlegnth)
def __del__(self):
self.clearText()
so when you use del
on obejct in python the __del__
method will be called
I could hide text like this
def show(self,timelive=None):
ctypes.windll.kernel32.SetConsoleCursorPosition(self.hOut, self.pos)
print(self.text)
if timelive:
sleep(timelive)
self.clearText()
and this is work fine but i did use del
and __del__
on purpose to be more flexible if you need to use it somewhere outside the class and make you familiar with that
so this the code will be something like this
import ctypes
from os import system
from time import sleep
class COORD(ctypes.Structure):
_fields_ = [("X", ctypes.c_short), ("Y", ctypes.c_short)]
def __init__(self, x, y):
self.X = x
self.Y = y
class TextField():
STD_OUTPUT_HANDLE = -11
hOut = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
text = ""
def __init__(self,x,y,maxlegnth):
self.pos = COORD(x,y)
self.maxlegnth = maxlegnth
def setText(self,text):
if len(text) > self.maxlegnth:
raise OverflowError(f"yout text legnth is {len(text)} but max legnth for this text field is {self.maxlegnth}")
self.text = text
def show(self,timelive=None):
ctypes.windll.kernel32.SetConsoleCursorPosition(self.hOut, self.pos)
print(self.text)
if timelive:
# sleep takes a float number if you need half second use 0.5 etc.
sleep(timelive)
del self
def clearText(self):
ctypes.windll.kernel32.SetConsoleCursorPosition(self.hOut, self.pos)
print(" "*self.maxlegnth)
def __del__(self):
self.clearText()
def main():
# use this to celar screen
system("cls")
# text position x,y zero is top left corner
textfield = TextField(x=0,y=0,maxlegnth=10)
textfield.setText("test1")
# creating a time delay of 2 seconds
textfield.show(2)
textfield.setText("test2")
textfield.show(2)
main()
this is just dummy example. It might give you the right insight
in solving the problem
the Cursor Position start at top left corner so its
x=0
andy=0
But in some operating systems it is different, which is rare
ctypes.WinDLL
import time
import sys
def print_and_replace(text, replacement_text, delay=5):
print(text, end='', flush=True)
time.sleep(delay)
sys.stdout.write('r' + ' ' * len(text))
sys.stdout.write('r')
sys.stdout.write(replacement_text)
sys.stdout.flush()
print_and_replace("This will be replaced in 5 seconds...", "This is the new text!", 5)
Omar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2