1

I am making a textual RPG game with Visual Studio in Python, where all the story is printed in the python command window with the print() function. And sometimes, I ask the Player to make some choices with the input() function.

The problem is, when the Player taps anything during the print (I use the time.sleep() between print for style), all these inputs are saved until the next input() call.

For example :

print("You're going to fight")
time.sleep(1) 

#player tap 'sdlkem ENTER' during this time

print("What do you want to do")
input("Your choice: ")

#the console tap automatically 'sdlkem ENTER' and process it  

I first search for a solution to disable keyboard, and found How to temporarily disable keyboard input using Python.

It was interesting but none of the solutions suited my issue because I can't nor disable printing, neither disable the keyboard forever...

Any ideas or alternatives?

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Fly
  • 13
  • 3
  • 4
    Does this answer your question? [How to temporarily disable keyboard input using Python](https://stackoverflow.com/questions/29289945/how-to-temporarily-disable-keyboard-input-using-python) – Fatih Atalay Oct 21 '21 at 17:39
  • @FatihAtalay: that link is already in the post of OP – Thomas Weller Oct 21 '21 at 17:59
  • 1
    If the user knows what input they want to provide, they’re going to get frustrated very quickly if they have to wait for every single one of your stylishly-slowly lines to appear before they can press a key and get it actioned. Think of one of those voice answering services when the spiel goes on and on and you to wait for it to end before you enter each digit, comared to shsortcircuiting the voice when you press a digit it recognizes. More user-friendly would be to immediately use the keypress and abort or very quickly show the remaining print output and immediately action the pressed key. – DisappointedByUnaccountableMod Oct 21 '21 at 18:05

1 Answers1

1

On Windows, you can combine kbhit() and getch():

import time
import msvcrt


def cleanup_accidental_input():
    while msvcrt.kbhit():
        msvcrt.getch()


print("Type during the next 3 seconds or so ...")
time.sleep(5)
print("Cleaning up your typing!")
cleanup_accidental_input()
non_accidental_input = input("Enter stuff now: ")
print(non_accidental_input)

This will not prevent user input during a sleep, but it will consume all input which was made during the sleep. IMHO, the net effect should be the same and your text adventure should work nicely.

Regarding gameplay, I agree to the comment of @balmy: If the user has to wait until he can make his choice, this may be frustrating. An alternative might be that you shorten the pause for impatient players:

import time
import msvcrt


def sleepmax(seconds: float):
    while seconds > 0:
        time.sleep(0.03)
        seconds -= 0.03
        if msvcrt.kbhit():
            seconds = 0
        
    while msvcrt.kbhit():
        msvcrt.getch()


print("Type during the next 3 seconds or so ...")
sleepmax(5)
non_accidental_input = input("Enter stuff now: ")
print(non_accidental_input)
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222