简体   繁体   English

随时使用Python捕获用户输入

[英]Python Catching user input at any time

I would like to develop a snake game in Python. 我想用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 尝试使用pygame

There are plenty of examples of snake games made with pygame that you can learn from. 您可以从中学到很多用pygame制作的蛇游戏的例子

The thing you are trying to do in your question is handle key events, pygame has many tools to help you with this. 您要在问题中尝试做的事情是处理关键事件,pygame有很多工具可以帮助您解决这一问题。 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. 是一个使用pygame的蛇游戏的非常简单的示例,如果您想将其拆开以查看其工作原理。

I'd recommend doing some pygame tutorials first, however. 但是,我建议您先做一些pygame教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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