Create a typewriter-effect animation for strings in Python
Question:
Just like in the movies and in games, the location of a place comes up on screen as if it’s being typed live. I want to make a game about escaping a maze in python. At the start of the game it gives the background information of the game:
line_1 = "You have woken up in a mysterious maze"
line_2 = "The building has 5 levels"
line_3 = "Scans show that the floors increase in size as you go down"
Under the variables, I tried to do a for loop for each line similar to this:
from time import sleep
for x in line_1:
print (x)
sleep(0.1)
The only problem with this is that it print one letter per line. The timing of it is ok, but how can I get it to go on one line?
Answers:
lines = ["You have woken up in a mysterious maze",
"The building has 5 levels",
"Scans show that the floors increase in size as you go down"]
from time import sleep
import sys
for line in lines: # for each line of text (or each message)
for c in line: # for each character in each line
print(c, end='') # print a single character, and keep the cursor there.
sys.stdout.flush() # flush the buffer
sleep(0.1) # wait a little to make the effect look good.
print('') # line break (optional, could also be part of the message)
To iterate over the lines, change the loop to:
for x in (line_1, line_2, line_3):
Because you tagged your question with python 3 I will provide a python 3 solution:
- Change your end character of print to an empty string:
print(..., end='')
- Add
sys.stdout.flush()
to make it print instantly (because the output is buffered)
Final code:
from time import sleep
import sys
for x in line_1:
print(x, end='')
sys.stdout.flush()
sleep(0.1)
Making it random is also very simple.
-
Add this import:
from random import uniform
-
Change your sleep
call to the following:
sleep(uniform(0, 0.3)) # random sleep from 0 to 0.3 seconds
You can change the end of line character automatically added by print with print("", end="")
. To printfoobar
, you could do this:
print("foo", end="")
print("bar", end="")
From the documentation:
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values.
Python Typewriter Effect
For every letter in the string, my answer provides 0.1 of a second to wait, so the text would appear one by one. Python 3 allows the use of sys.stdout.write
.
import time, sys
def anything(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.1)
anything("Blah Blah Blah...")
Your full code will look like this:
import time, sys
def anything(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.1)
anything("You have woken up in a
mysterious maze")
anything("The building has five
levels")
anything("Scans show that the floors
increase in size as you go down")
It’s easiest solving:
import time
def tt(text, delay):
for i in text:
print(end = i)
time.sleep(delay)
print(tt("sample text", 0.2)
This solution will not change ur algorithm
from time import sleep
line_1 = "You have woken up in a mysterious maze"
line_2 = "The building has 5 levels"
line_3 = "Scans show that the floors increase in size as you go down"
for x in line_1:
print(x, end='')
sleep(0.05)
Just like in the movies and in games, the location of a place comes up on screen as if it’s being typed live. I want to make a game about escaping a maze in python. At the start of the game it gives the background information of the game:
line_1 = "You have woken up in a mysterious maze"
line_2 = "The building has 5 levels"
line_3 = "Scans show that the floors increase in size as you go down"
Under the variables, I tried to do a for loop for each line similar to this:
from time import sleep
for x in line_1:
print (x)
sleep(0.1)
The only problem with this is that it print one letter per line. The timing of it is ok, but how can I get it to go on one line?
lines = ["You have woken up in a mysterious maze",
"The building has 5 levels",
"Scans show that the floors increase in size as you go down"]
from time import sleep
import sys
for line in lines: # for each line of text (or each message)
for c in line: # for each character in each line
print(c, end='') # print a single character, and keep the cursor there.
sys.stdout.flush() # flush the buffer
sleep(0.1) # wait a little to make the effect look good.
print('') # line break (optional, could also be part of the message)
To iterate over the lines, change the loop to:
for x in (line_1, line_2, line_3):
Because you tagged your question with python 3 I will provide a python 3 solution:
- Change your end character of print to an empty string:
print(..., end='')
- Add
sys.stdout.flush()
to make it print instantly (because the output is buffered)
Final code:
from time import sleep
import sys
for x in line_1:
print(x, end='')
sys.stdout.flush()
sleep(0.1)
Making it random is also very simple.
-
Add this import:
from random import uniform
-
Change your
sleep
call to the following:sleep(uniform(0, 0.3)) # random sleep from 0 to 0.3 seconds
You can change the end of line character automatically added by print with print("", end="")
. To printfoobar
, you could do this:
print("foo", end="")
print("bar", end="")
From the documentation:
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values.
Python Typewriter Effect
For every letter in the string, my answer provides 0.1 of a second to wait, so the text would appear one by one. Python 3 allows the use of sys.stdout.write
.
import time, sys
def anything(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.1)
anything("Blah Blah Blah...")
Your full code will look like this:
import time, sys
def anything(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(0.1)
anything("You have woken up in a
mysterious maze")
anything("The building has five
levels")
anything("Scans show that the floors
increase in size as you go down")
It’s easiest solving:
import time def tt(text, delay): for i in text: print(end = i) time.sleep(delay) print(tt("sample text", 0.2)
This solution will not change ur algorithm
from time import sleep
line_1 = "You have woken up in a mysterious maze"
line_2 = "The building has 5 levels"
line_3 = "Scans show that the floors increase in size as you go down"
for x in line_1:
print(x, end='')
sleep(0.05)