我正在尝试编写一个游戏,它会在后台执行一些操作,直到您按下某个键为止,因此它会在等待输入的同时执行其他操作。我以前从未做过这样的事情,但我听说解决方案与事件处理有关。我正在尝试使用“asyncore”库或“signal”库(Python),但我不理解文档,并且我认为我缺少基本概念。您能向我解释一下我如何使用信号处理吗? (或者也许我还能做点什么?)
谢谢!
I am trying to write a game that does stuff in the background until you hit a key, so it's waiting for input while doing other stuff at the same time. I have never done something like this before but I hear the solution has to do with event handling. I am trying to use either the "asyncore" library or the "signal" library (Python), but I don't understand the documentation and I think I'm missing basic concepts. Can you explain to me how I might go about using signal handling? (Or maybe there's something else I can do?)
Thanks!
发布评论
评论(2)
Python 的 asyncore 库用于网络通信,而 signal 库用于计时器和 操作系统信号(与键盘输入无关)。
首先,您应该找到适合您目的的 Python 游戏编程库。
如果您想要在没有游戏编程库帮助的情况下完成像键盘输入这样看似简单的事情,那么您很快就会被迫使用 Win32 和 X11 等本机 API。通过使用游戏编程库,您将有机会首先了解事件和后台任务。
Python's asyncore library is for network communication, and the signal library is used for timers and operating system signals (not related to keyboard input).
To get started, you should find a Python game programming library that suits your purposes.
If you want to do something as seemingly simple as keyboard input without help from a game programming library, you'll quickly be forced to use native APIs like Win32 and X11. By using a game programming library, you'll have get a chance to learn about events and background tasks first.
如果你想用 Python 编写一个支持 SDL 的游戏,你应该考虑使用 pygame。
SDL:Simple DirectMedia Layer 是一个跨平台多媒体库,旨在通过 OpenGL 提供对音频、键盘、鼠标、操纵杆、3D 硬件和 2D 视频帧缓冲区的低级访问。 [ http://www.libsdl.org/ ]
Pygame 是与 SDL 的 python 绑定: http://www.pygame.org
但如果你真的想以困难的方式做到这一点,我认为你应该考虑使用多处理包裹。
原因是您的游戏应该有一个主循环,用于分析输入(鼠标、键盘)并更新游戏的屏幕。这个进程不应该有太多的开销,否则游戏会显示出性能不佳的迹象...
第二个进程应该是您想要用来在后台编写其他内容的工作进程...
多处理包为您提供了足够的资源进程间通信的选择(管道、队列、事件)... http://docs.python .org/library/multiprocessing.html
总而言之,即使无论您的游戏是否使用框架,您的背景内容都应该位于与游戏主循环不同的进程中。 (Python 中的线程仅适用于 I/O 的高使用,因此它不是您现在想要的包)。
If you want to write a game in python with an SDL support, you should consider using pygame.
SDL: Simple DirectMedia Layer is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer. [ http://www.libsdl.org/ ]
Pygame is python bindings with SDL: http://www.pygame.org
But if you really want to do it the hard way, I think that you should consider using the multiprocessing package.
The reason is that your game should have a main loop which is used for analysing the inputs (mouse, keyboard) and update the screen of your game. This process should not have too much overhead or the game will show signs of poor performance...
The second process should be the worker process that you want to use to code your other stuff in the background...
the multiprocessing package gives you plenty of choices for the interprocess communications (Pipe, Queue, Event)... http://docs.python.org/library/multiprocessing.html
To conclude, even if you use a framework or not for your game, your background stuff should be on a different process that your game's main loop. (Threading in python is good only for high use of I/O, so it's not the package you want right now).