PyMunk 不创建 window - Python
PyMunk does not create window - Python
我正在尝试学习 PyMunk 库,我使用了他们网站上的示例。
这是一个代码:
import pymunk # Import pymunk..
space = pymunk.Space() # Create a Space which contain the simulation
space.gravity = 0,-1000 # Set its gravity
body = pymunk.Body(1,1666) # Create a Body with mass and moment
body.position = 50,100 # Set the position of the body
poly = pymunk.Poly.create_box(body) # Create a box shape and attach to body
space.add(body, poly) # Add both body and shape to the simulation
while True: # Infinite loop simulation
space.step(0.02) # Step the simulation one step forward
当我 运行 时,window 没有显示,在 CMD 中它说:Loading chipmunk for Windows (64bit) [C:\Users\Theo\AppData\Local\Programs\Python\Python35\lib\site-packages\pymunk\chipmunk.dll]
并且没有加载任何东西。我等了一个小时。有什么问题?
您应该尝试将 PyMunk 与 PyGame 或 PyGlet 连接,以便能够通过 window 可视化任何结果。
在此处查看更多信息:http://www.pymunk.org/en/latest/
我在尝试 运行 演示时也感到震惊。
所以,正如@Teodor Cristian所说,基本的演示不会画任何东西,除非你连接一个可视化库。这些是可选的,如文档中所述:PyGame and PyGlet.
此代码不会尝试打开 window,但如果您添加打印语句,您会看到它会打开 "move":
while True:
space.step(0.02)
print(body.position) # add this line
您可以查看这些视频教程以插入 Pyglet(但请以 1.5 倍速观看...):
https://www.youtube.com/watch?v=0l2QrTNPCdc&list=PL1P11yPQAo7pH9SWZtWdmmLumbp_r19Hs&index=2
Pyglet 的基本演示看起来像这样:
import pymunk
import pyglet
from pymunk.pyglet_util import DrawOptions
options = DrawOptions()
window = pyglet.window.Window(800, 600, "Brackets")
space = pymunk.Space()
space.gravity = (0, -1000)
body = pymunk.Body(1, 1666)
body.position = 400, 500
poly = pymunk.Poly.create_box(body, size=(100, 20))
space.add(body, poly)
# from here, the rest of the code is the render loop
@window.event
def on_draw():
window.clear()
space.debug_draw(options)
def update(dt):
space.step(dt) # Step the simulation one step forward
pyglet.clock.schedule_interval(update, 1.0 / 60)
pyglet.app.run()
此外,下载 Pymunk 示例文件夹并试用它们。其中一些使用pyglet,一些pygame:
https://github.com/viblo/pymunk/tree/master/examples
Pyglet 也有一个示例文件夹,因此您可以看到它也可以独立运行:
https://github.com/pyglet/pyglet/blob/master/examples/hello_world.py
我正在尝试学习 PyMunk 库,我使用了他们网站上的示例。 这是一个代码:
import pymunk # Import pymunk..
space = pymunk.Space() # Create a Space which contain the simulation
space.gravity = 0,-1000 # Set its gravity
body = pymunk.Body(1,1666) # Create a Body with mass and moment
body.position = 50,100 # Set the position of the body
poly = pymunk.Poly.create_box(body) # Create a box shape and attach to body
space.add(body, poly) # Add both body and shape to the simulation
while True: # Infinite loop simulation
space.step(0.02) # Step the simulation one step forward
当我 运行 时,window 没有显示,在 CMD 中它说:Loading chipmunk for Windows (64bit) [C:\Users\Theo\AppData\Local\Programs\Python\Python35\lib\site-packages\pymunk\chipmunk.dll]
并且没有加载任何东西。我等了一个小时。有什么问题?
您应该尝试将 PyMunk 与 PyGame 或 PyGlet 连接,以便能够通过 window 可视化任何结果。 在此处查看更多信息:http://www.pymunk.org/en/latest/
我在尝试 运行 演示时也感到震惊。
所以,正如@Teodor Cristian所说,基本的演示不会画任何东西,除非你连接一个可视化库。这些是可选的,如文档中所述:PyGame and PyGlet.
此代码不会尝试打开 window,但如果您添加打印语句,您会看到它会打开 "move":
while True:
space.step(0.02)
print(body.position) # add this line
您可以查看这些视频教程以插入 Pyglet(但请以 1.5 倍速观看...):
https://www.youtube.com/watch?v=0l2QrTNPCdc&list=PL1P11yPQAo7pH9SWZtWdmmLumbp_r19Hs&index=2
Pyglet 的基本演示看起来像这样:
import pymunk
import pyglet
from pymunk.pyglet_util import DrawOptions
options = DrawOptions()
window = pyglet.window.Window(800, 600, "Brackets")
space = pymunk.Space()
space.gravity = (0, -1000)
body = pymunk.Body(1, 1666)
body.position = 400, 500
poly = pymunk.Poly.create_box(body, size=(100, 20))
space.add(body, poly)
# from here, the rest of the code is the render loop
@window.event
def on_draw():
window.clear()
space.debug_draw(options)
def update(dt):
space.step(dt) # Step the simulation one step forward
pyglet.clock.schedule_interval(update, 1.0 / 60)
pyglet.app.run()
此外,下载 Pymunk 示例文件夹并试用它们。其中一些使用pyglet,一些pygame:
https://github.com/viblo/pymunk/tree/master/examples
Pyglet 也有一个示例文件夹,因此您可以看到它也可以独立运行:
https://github.com/pyglet/pyglet/blob/master/examples/hello_world.py