简体   繁体   中英

Python Catching user input at any time

I would like to develop a snake game in Python. For that, I need to execute a loop to check if the snake is beating himself and also to make him headway. I also need to catch the input of the user to give the direction to the snake... But the user can change the direction at any time...

I read some articles about multithreading. I understand that I need to use that but I really don't get how to make it...

I know I ask a lot but I'm blocked in a complete standstill.

Thank you.

Try using pygame

There are plenty of examples of snake games made with pygame that you can learn from.

The thing you are trying to do in your question is handle key events, pygame has many tools to help you with this. I'd recommend getting started learning how to use a game library like this instead of attempting to code your own multithreaded keyboard listener.

Here is a very simple example of a snake game using pygame if you want to pick it apart to see how it works.

I'd recommend doing some pygame tutorials first, however.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM