I am trying to make a command-line-app/game and followed this website to get the basic functionality making use of tty.setraw(sys.stdin)
and ANSI escape codes to draw to the terminal. The problem is, I want to have an animation - looping through frames with time.sleep()
between - but the user can still make inputs which are handled all at once after the animation finishes.
The code is kind of lengthy, but the the structure of the code is kind of like this:
while running:
#get currently pressed key from stdin
pressed = ord(sys.stdin.read(1))
if pressed == spacebar_key:
for i in frames:
#print out the current frame . . .
sleep(0.2)
#do other stuff
I was wondering if there was a way to either pause the input while the animation is playing, or dump it after the animation finishes so they are not picked up in the next iteration of the game loop. I saw this question about checking if there is data in stdin and I was hoping that I could just check if stdin had any buffered inputs so I could dump them, but I am not exactly sure what exactly it is doing or if it is even related to what I am looking for.
Edit: I am using a Unix system