I am creating a text based game and in order to speed past dialogue I want the user to be able to hit 'Space' and skip past it.
import time
import sys
text_speed = .05
def slow_type(line, speed): #You input the dialogue and speed(smaller = faster)
for l in line:
sys.stdout.write(l)
sys.stdout.flush()
time.sleep(speed)
time.sleep(.5)
if <'Space'> pressed:
text_speed = 0
NL1 = "Huh, I see you finally came. "
slow_type(NL1, text_speed)
The Python representation of a space is u"\ "
, referred here .
And tested with:
if input('enter:') == u"\u0020":
print('pass')
else:
print('fail')
You can use the getch
module.
import getch
while getch.getch() != ' ':
pass
Or on Windows, you can use msvcr.getch
:
import msvcrt
while msvcrt.getch() != ' ':
pass
I will suggest you to use keyboard .
Here is a sample code to detect space, also note that the code will wait for space key to be pressed.
import keyboard
#.....
if keyboard.is_pressed("space"):
text_speed = 0
#....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.