简体   繁体   English

使用Cython进行游戏开发?

[英]Using Cython for game development?

How practical would it be to use Cython as the primary programming language for a game? 使用Cython作为游戏的主要编程语言有多实用?

I am a experienced Python programmer and I absolutely love it, but I'm admittedly a novice when it comes to game programming specifically. 我是一名经验丰富的Python程序员,我非常喜欢它,但对于游戏编程而言,我确实是个新手。 I know that typically Python is considered too slow to do any serious game programming, which is why Cython is interesting to me. 我知道通常认为Python太慢而无法进行任何严肃的游戏编程,这就是为什么Cython对我很有趣。 With Cython I can use a Python-like language with the speed of C. 使用Cython,我可以使用类似Python的语言,速度为C.

I understand that I'll probably need to learn a bit of C/C++ anyway, but it seems like Cython would speed up development time quite a bit in comparison. 我知道我可能还需要学习一些C / C ++,但看起来Cython会比较加快开发时间。

So, is it practical? 那么,它实用吗? And would I still be able to use C/C++ libraries like OpenGL, OpenAL, and Bullet Physics? 我还能使用OpenGL,Op​​enAL和Bullet Physics等C / C ++库吗?

If you're working with a combination like that and your goal is to write a 3D game, you'd probably get better mileage out of a ready-made 3D engine with mature physics and audio bindings and a Python API like OGRE 3D ( Python-OGRE ) or Panda3D . 如果您正在使用这样的组合,并且您的目标是编写3D游戏,那么您可能会通过成熟的物理和音频绑定以及像OGRE 3DPython等Python API)的现成3D引擎获得更好的里程数。 -OGRE )或Panda3D Even if you don't, this post about using Cython with Panda3D may be helpful. 即使你不这样做,这篇关于将Cython与Panda3D一起使用的帖子可能会有所帮助。

I'm not sure about now, but back in 2007 , the trade-off between the two was basically that: 我现在不确定,但在2007年 ,两者之间的权衡取决于:

  • Panda3D was better-documented and designed from the ground-up to be a C++ accelerated Python engine (apparently they made some API design decisions that don't occur to C++ engine projects) and, predictably, had a more mature Python API. Panda3D更好地记录并且从头开始设计为C ++加速的Python引擎(显然他们做出了一些C ++引擎项目不会出现的API设计决策),并且可以预见,它有一个更成熟的Python API。
  • PyOgre was built on top of a much more advanced engine and had a larger and more vibrant community. PyOgre建立在更先进的引擎之上,拥有更大,更有活力的社区。

...however it's quite possible that may have changed, given that, passage of time aside, in 2007, Panda3D was still under a GPL-incompatible license and that drove off a lot of people. ......然而,由于时间流逝,2007年,Panda3D仍然处于GPL不兼容的许可证之下,很可能会发生变化,并且驱使很多人离开了。 (Myself included) (包括我自己)

I'm the developer for the Ignifuga Game Engine , it's 2D oriented and Python/Cython/SDL based. 我是Ignifuga游戏引擎的开发者,它是面向2D的,基于Python / Cython / SDL。 What I generally do is develop the code in Python, and then profile the engine to see if there are some obvious bottlenecks (the main loop, the rendering code are good candidates), and convert those modules to Cython. 我通常做的是用Python开发代码,然后分析引擎以查看是否存在一些明显的瓶颈(主循环,渲染代码是好的候选者),并将这些模块转换为Cython。 I then run all the code (Python and Cython based) through Cython, and compile it statically against SDL. 然后我通过Cython运行所有代码(基于Python和Cython),并针对SDL静态编译它。 Another of Cython's big "pluses" is that binding to SDL, or any C based library, is almost trivial. Cython的另一个重要的“优点”是绑定到SDL或任何基于C的库几乎是微不足道的。 Regarding threads, the engine is currently single threaded with cooperative multitasking via Greenlets , though this comes from a design decision to mitigate potential multi threaded pitfalls that unexperienced developers might fall into, rather than a limitation on Cython's part. 关于线程,引擎目前是单线程的,通过Greenlets进行协同式多任务处理,尽管这来自于设计决策,以减轻无经验的开发人员可能陷入的潜在多线程陷阱,而不是对Cython的限制。

at this date (12th of April 2011) unixmab83 is wrong. 在这个日期(2011年4月12日),unixmab83错了。

Cython doesn't forbid the use of threads, you just needs to use the no_gil special statements. Cython不禁止使用线程,您只需要使用no_gil特殊语句。

Beside the bindins of c++ is now functional in cython. 除了c ++的绑定之外,现在在cython中起作用。

We do use it for something which is close to gamedev. 我们确实将它用于接近gamedev的东西。 So while I cannot be final on this, cython is a valid candidate. 因此,虽然我无法做到最终,但cython是一个有效的候选人。

I've found that a lot of the time, especially for larger libraries, you wind up spending a tremendous amount of time just configuring the Cython project to build, knowing which structures to import, bridging the C code into Python in either direction etc. While Cython is a nice stopgap (and significantly more pleasant than pure C/C++ development), the amount of C++ you'd have to learn to effectively use it basically means you're going to have to bite the bullet and learn C++ anyway. 我发现很多时候,特别是对于大型库,你花费了大量的时间来配置Cython项目来构建,知道要导入哪些结构,将C代码桥接到Python的任何一个方向等。虽然Cython是一个很好的权宜之计(并且比纯C / C ++开发更令人愉快),但是你必须学会​​有效使用它的C ++数量基本上意味着你必须咬紧牙关并学习C ++。

How about PyGame? PyGame怎么样?

I know Cython and you do not have to know C/C++. 我知道Cython,你不必知道C / C ++。 You will use static typing but very easy. 您将使用静态类型,但非常简单。 The hardest part is to get the compiling working, I think on Windows this is done over visual studio thing. 最困难的部分是让编译工作,我认为在Windows上这是通过visual studio完成的。 There is something like a standard library including math for example. 有一些像标准库,包括数学。 The speed gain is not too big but this depends on your scope. 速度增益不是太大,但这取决于你的范围。 ctypes was much faster (pure C) but the connection to Python was very slow so that i decided to look for Cython which can still be dynamic. ctypes要快得多(纯C),但与Python的连接非常慢,所以我决定寻找仍然是动态的Cython。

For speed gain in a game Cython will be the right choice but i would name this performance also limited. 对于游戏中的速度提升,Cython将是正确的选择,但我会说这个性能也有限。

Threads!!! 主题! A good modern game must use threads. 一个好的现代游戏必须使用线程。 Cython practically forbids their use , holding to GIL (global interpreter lock) the entire time, making your code run in sequence. Cython实际上禁止使用它们 ,一直持有GIL(全局解释器锁),使您的代码按顺序运行。

If you are not writing a huge game, than Python/Cython is okay. 如果你不是在写一个巨大的游戏,那么Python / Cython就可以了。 But Cython is no good as a modern language without good thread support. 但是如果没有良好的线程支持,Cython作为一种现代语言并不好。

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

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