PyMunk does not create window - Python

527 views Asked by At

I am trying to learn PyMunk library and I used the example from their website. Here is a code:

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

When I run it the window does not show up and in CMD it says: Loading chipmunk for Windows (64bit) [C:\Users\Theo\AppData\Local\Programs\Python\Python35\lib\site-packages\pymunk\chipmunk.dll] and does not load anything. I waited for an hour. What is the problem?

2

There are 2 answers

0
Teodor Cristian On BEST ANSWER

You should try connecting PyMunk with PyGame or PyGlet to be able to viualize any results through a window. See more here: http://www.pymunk.org/en/latest/

1
mayid On

Me too had a shocking surprise trying to run the demo.

So, as @Teodor Cristian said, the basic demo will not draw anything, unless you connect a visualization library. These are optional, as stated in the documentation: PyGame and PyGlet.

This code will not try to open a window, but if you add a print statement you will see that it does "move":

while True:              
    space.step(0.02) 
    print(body.position)  # add this line

You can check these video tutorials to plug Pyglet (but watch them at 1.5x velocity...):

https://www.youtube.com/watch?v=0l2QrTNPCdc&list=PL1P11yPQAo7pH9SWZtWdmmLumbp_r19Hs&index=2

The basic demo with Pyglet would look something like this:

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()

Also, download the Pymunk examples folder and try them out. Some of them uses pyglet, some pygame:

https://github.com/viblo/pymunk/tree/master/examples

Pyglet also has an example folder, so you can see it working isolated too:

https://github.com/pyglet/pyglet/blob/master/examples/hello_world.py