how to make a timed readchar()
I'm trying to make a snake game, but I need a timed readchar() to check if the snake is supposed to change direction or not.
Voters
Vandesm14
What do you mean by a timed readchar()? Can you explain more?
AlexanderTarn
@Vandesm14 Every 0.5 seconds the snake should move, if the readchar() gets an input during this time the direction of the snake changes. This is just like any other game.
I've never used "readchar" so i'm assuming it's a synchronous (blocking) function
If this is the case, you can make it asynchronous by using the
"threading"
module and using a loop to store the value ofreadchar()
to a global variable[Note]: If the "readchar()" function blocks the console like the "input()" function, then this will not work
Start the thread once at the beginning of your code and then that's it
print(x)
toprint('\r' + x)