I'm making a text based game and I want the user to be able to press enter while the text is appearing letter by letter to make the remaining of the text appear immediately.
This is my code so far
import time
import sys
def print(s):
for c in s:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.01)
def sceneOne():
print ("insert\n"
"text\n"
"here\n")
input("\n[Press enter to continue]\n")
print ("insert\n"
"text\n"
"here\n")
input("\n[Press enter to continue]\n")
sceneOne()
I want the "Press enter to continue" text to be under the "insert text here" while the text is appearing letter by letter so that the user can make the text appear faster if they have already played the game and want to speed run through this part to get to the next choice faster.
install the keyboard module and then you can create an event listener. Then, modify the print function so that it does not sleep if they hit enter. See below:
def print(s):
for c in s:
if keyboard.is_pressed('enter'):
sys.stdout.write(c)
sys.stdout.flush()
else:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.05)