Python pyGame 做 3d 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4865636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does pyGame do 3d?
提问by eggbert
I can't seem to find the answer to this question anywhere. I realise that you have to use pyOpenGL or something similar to do openGL stuff, but I was wondering if its possible to do very basic 3d graphics without any other dependencies.
我似乎无法在任何地方找到这个问题的答案。我意识到你必须使用 pyOpenGL 或类似的东西来做 openGL 的东西,但我想知道是否可以在没有任何其他依赖的情况下做非常基本的 3d 图形。
采纳答案by Kylotan
No, Pygame is a wrapper for SDL, which is a 2D api. Pygame doesn't provide any 3D capability and probably never will.
不,Pygame 是 SDL 的包装器,它是一个 2D api。Pygame 不提供任何 3D 功能,而且可能永远不会。
3D libraries for Python include Panda3Dand DirectPython, although they are probably quite complex to use, especially the latter.
Python 的 3D 库包括Panda3D和DirectPython,尽管它们使用起来可能非常复杂,尤其是后者。
回答by Wes P
Well, if you can do 2d you can always do 3d. All 3d really is is skewed 2 dimensional surfaces giving the impression you're looking at something with depth. The real question is can it do it well, and would you even want to. After browsing the pyGame documentation for a while, it looks like it's just an SDL wrapper. SDL is not intended for 3d programming, so the answer to the real question is, No, and I wouldn't even try.
好吧,如果你能做 2d,你总是可以做 3d。所有的 3d 都是倾斜的二维表面,给人的印象是你正在看有深度的东西。真正的问题是它能否做得好,你甚至愿意。浏览了一段时间的 pyGame 文档后,它看起来只是一个 SDL 包装器。SDL 不适用于 3d 编程,所以真正问题的答案是,不,我什至不会尝试。
回答by Samuel Breese
Python Soya can render 3d graphics on pygame surfaces.
Python Soya 可以在 pygame 表面上渲染 3d 图形。
回答by Horst JENS
You can do pseudo-3d games ( like "Doom" ) with pygame only:
你只能用 pygame 做伪 3d 游戏(比如 "Doom" ):
http://code.google.com/p/gh0stenstein/
http://code.google.com/p/gh0stenstein/
and if you browse the pygame.org site you may find more "3d" games done with python and pygame.
如果您浏览 pygame.org 站点,您可能会发现更多使用 python 和 pygame 完成的“3d”游戏。
However, if you really want to go into 3d programming you should look into OpenGl, Blender or any other real 3d lib.
但是,如果您真的想进行 3d 编程,则应该查看 OpenGL、Blender 或任何其他真正的 3d 库。
回答by alexpinho98
If you want 3D projection functions and all of that stuff you'll have to use a 3D API but if you want to do a basic 3D look I recommend Peter's Website tutorial: http://www.petercollingridge.co.uk/pygame-3d-graphics-tutorial
如果您想要 3D 投影功能和所有这些东西,您将不得不使用 3D API,但如果您想做基本的 3D 外观,我推荐 Peter 的网站教程:http: //www.petercollingridge.co.uk/pygame- 3d图形教程
回答by Douglas - 15 year old Pythoner
Pygame was never originally meant to do 3d, but there is a way you can do 3d with any 2d graphics library. All you need is the following function, which converts 3d points to 2d points, which allows you to make any 3d shape by just drawing lines on a screen.
Pygame 最初从未打算做 3d,但有一种方法可以使用任何 2d 图形库来做 3d。您只需要以下函数,它将 3d 点转换为 2d 点,它允许您通过在屏幕上绘制线条来制作任何 3d 形状。
def convert_to_2d(point=[0,0,0]):
return [point[0]*(point[2]*.3),point[1]*(point[2]*.3)]
This is called pseudo 3d, or 2.5d. This can be done, but may be slow, and is extremely difficult to do, so it is suggested that you use a library meant for 3d.
这称为伪 3d 或 2.5d。这可以做到,但可能很慢,而且很难做到,因此建议您使用专为 3d 设计的库。
回答by Anton
It is easy to make 3D driver for PyGame. PyGame has some assets for 3D game development. Now I am developing Py3D driver with using PyGame. When I'll end then I'll show you link to download Py3D. I tried to make 3D game with PyGame and I need just small addon for PyGame. It is wrong you think you must use SDL, PyOpenGL, OpenGL, PyQt5, Tkinter. All of them are wrong for making 3D games. OpenGL and PyOpenGL or Panda3D are very hard for learning. All my games maked on those drivers was awful. PyQt5 and Tkinter aren't drivers for making games, but them've got addons for it. Don't try to make any game on those drivers. All drivers where we need to use module math.py are hard. You can easily make small addon for them, I think everybody can make driver for PyGame by 1-2 weeks.
为 PyGame 制作 3D 驱动程序很容易。PyGame 有一些用于 3D 游戏开发的资产。现在我正在使用 PyGame 开发 Py3D 驱动程序。当我结束时,我将向您展示下载 Py3D 的链接。我试图用 PyGame 制作 3D 游戏,我只需要 PyGame 的小插件。你认为必须使用 SDL、PyOpenGL、OpenGL、PyQt5、Tkinter 是错误的。所有这些对于制作 3D 游戏都是错误的。OpenGL 和 PyOpenGL 或 Panda3D 非常难学。我在这些驱动程序上制作的所有游戏都很糟糕。PyQt5 和 Tkinter 不是制作游戏的驱动程序,但它们有插件。不要试图在这些驱动程序上制作任何游戏。我们需要使用模块 math.py 的所有驱动程序都很困难。你可以很容易地为他们制作小插件,我认为每个人都可以在 1-2 周内为 PyGame 制作驱动程序。
回答by Jonas De Schouwer
What you see as a 3D is actually a 2D game. After all, you are watching your screen, which (normally ;) ) is 2D. The virtual world (which is in 3D) is projected onto a plane, which is then shown on your screen. Our brains then convert that 2D image into a 3D one (like they do with the image of our eyes), making it look like it's 3D.
您所看到的 3D 实际上是一款 2D 游戏。毕竟,您正在观看您的屏幕,它(通常 ;) )是 2D 的。虚拟世界(3D)被投影到一个平面上,然后显示在您的屏幕上。然后我们的大脑将该 2D 图像转换为 3D 图像(就像他们对我们眼睛的图像所做的那样),使其看起来像是 3D。
So it's relatively easy to make a 3D game: you just create a virtual world using a multidimensional matrix and then project it each loop on a 2D plane, which you display on your screen.
因此,制作 3D 游戏相对容易:您只需使用多维矩阵创建一个虚拟世界,然后将每个循环投影到 2D 平面上,然后显示在屏幕上。
One tutorial that you can put on your way to 3D programs (using pygame) is this one .
您可以将其用于 3D 程序(使用 pygame)的一个教程是这个。
回答by kCODINGeroo
Pygame is just a library for changing the color of pixels (and some other useful stuff for programming games). You can do this by blitting images to the screen or directly setting the colors of pixels.
Pygame 只是一个用于更改像素颜色的库(以及其他一些对游戏编程有用的东西)。您可以通过将图像块传输到屏幕或直接设置像素颜色来实现此目的。
Because of this, it is easy to write 2D games with pygame, as the above is all you really need. But a 3D game is just some 3D objects 'squashed' (rendered) into 2D so that it can be displayed on the screen. So, to make a 3D game using only pygame, you would have handle this rendering by yourself, including all the complex matrix maths necessary.
正因为如此,用 pygame 编写 2D 游戏很容易,因为上面是你真正需要的。但是 3D 游戏只是将一些 3D 对象“压扁”(渲染)为 2D 以便它可以显示在屏幕上。因此,要仅使用 pygame 制作 3D 游戏,您必须自己处理此渲染,包括所有必要的复杂矩阵数学运算。
Not only would this run slowly because of the immense processing power involved in this, but it would require you to write a massive 3D rendering/rasterisation engine. And because of python being interpreted it would be even slower. The correct approach would be to have this process run on the GPU using (Py)opengl.
这不仅会因为其中涉及的巨大处理能力而运行缓慢,而且还需要您编写大型 3D 渲染/光栅化引擎。而且由于 python 被解释,它会更慢。正确的方法是使用 (Py)opengl 在 GPU 上运行此过程。
So, yes it is technically possible to do 3D using only pygame, but definitely not recommended. I would suggest you learn Panda3D or some similar 3D engine.
所以,是的,技术上可以只使用 pygame 来做 3D,但绝对不推荐。我建议你学习 Panda3D 或一些类似的 3D 引擎。
回答by Henry
You can make like this :
你可以这样:
def convert_2d(x, y, z, horizon):
d = 1 - (z/horizon)
return x*d, y*d
def draw_list_of_points(lst):
'''Assume that lst is a list of 3 dimentionnal points like [(0, 0, 0), (1, 6, 2)...
Let's take 200 for the horizon, it can give us a pretty clean 3D'''
for x, y, z in lst:
pygame.draw.circle(screen, color, convert_2d(x, y, z, 200), 1)
But it's not very fast. If you want fast try to implement in C++/SDL2 or C.
Pygame is not very good for 3d graphics.

