Examples

Here you will find a list of the included examples. Each example have a short description and a screenshot (if applicable).

To look at the source code of an example open it on github by following the link. The examples are also included in the source distribution of Pymunk (but not if you install using the wheel file). You can find the source distribution at PyPI, https://pypi.org/project/pymunk/#files (file named pymunk-x.y.z.zip).

Jupyter Notebooks

There are a couple examples that are provided as Jupyter Notebooks (.ipynb). They are possible to either view online in a browser directly on github, or opened as a Notebook.

matplotlib_util_demo.ipynb

Displays the same space as the pygame and pyglet draw demos, but using matplotlib and the notebook.

Source: examples/matplotlib_util_demo.ipynb

_images/matplotlib_util_demo.png

newtons_cradle.ipynb

Similar simulation as newtons_cradle.py, but this time as a Notebook. Compared to the draw demo this demo will output a animation of the simulated space.

Source: examples/newtons_cradle.ipynb

Standalone Python

To run the examples yourself either install pymunk and run the mobule. Alternatively you can run each file separately.

Given that pymunk is installed:

$> python -m pymunk.examples.breakout

To list all the examples, use the -l option:

$> python -m pymunk.examples -l

Each example contains something unique. Not all of the examples use the same style. For example, some use the pymunk.pygame_util module to draw stuff, others contain the actual drawing code themselves. However, each example is self contained. Except for external libraries (such as pygame) and pymunk each example can be run directly to make it easy to read the code and understand what happens even if it means that some code is repeated for each example.

If you have made something that uses pymunk and would like it displayed here or in a showcase section of the site, feel free to contact me!

arrows.py

Source: examples/arrows.py

Showcase of flying arrows that can stick to objects in a somewhat realistic looking way.

_images/arrows.png

balls_and_lines.py

Source: examples/balls_and_lines.py

This example lets you dynamically create static walls and dynamic balls

_images/balls_and_lines.png

basic_test.py

Source: examples/basic_test.py

Very simple example that does not depend on any third party library such as pygame or pyglet like the other examples.

bouncing_balls.py

Source: examples/bouncing_balls.py

This example spawns (bouncing) balls randomly on a L-shape constructed of two segment shapes. Not interactive.

_images/bouncing_balls.png

box2d_pyramid.py

Source: examples/box2d_pyramid.py

Remake of the pyramid demo from the box2d testbed.

_images/box2d_pyramid.png

box2d_vertical_stack.py

Source: examples/box2d_vertical_stack.py

Remake of the veritcal stack demo from the box2d testbed.

_images/box2d_vertical_stack.png

breakout.py

Source: examples/breakout.py

Very simple breakout clone. A circle shape serves as the paddle, then breakable bricks constructed of Poly-shapes.

The code showcases several pymunk concepts such as elasitcity, impulses, constant object speed, joints, collision handlers and post step callbacks.

_images/breakout.png

camera.py

Source: examples/camera.py

Basic showcase on how the transform property on SpaceDebugDrawOptions can be used as a camera to allow panning. Use arrows to move the camera.

_images/camera.png

colors.py

Source: examples/colors.py

An example of the determinism of pymunk by coloring balls according to their position, and then respawning them to verify each ball ends up in the same place. Inspired by Pymunk user Nam Dao.

_images/colors.png

colors_pyglet_batch.py

Source: examples/colors_pyglet_batch.py

An example of the determinism of pymunk by coloring balls according to their position, and then respawning them to verify each ball ends up in the same place. Inspired by Pymunk user Nam Dao.

This is also a demonstration of the performance boost from the batch api in pymunk.batch.

Running this script for 30 seconds with cProfile, about 57% of the time is spent on drawing without batching, but with batching only uses 34% of the time. The rest of the time is mainly spent on space.Step stepping the simulation forward.

To try yourself:

> python -m cProfile -o batch_on.prof -m pymunk.examples.colors_pyglet_batch
_images/colors_pyglet_batch.png

constraints.py

Source: examples/constraints.py

Pymunk constraints demo. Showcase of all the constraints included in Pymunk.

Adapted from the Chipmunk Joints demo: https://github.com/slembcke/Chipmunk2D/blob/master/demo/Joints.c

_images/constraints.png

contact_and_no_flipy.py

Source: examples/contact_and_no_flipy.py

This example spawns (bouncing) balls randomly on a L-shape constructed of two segment shapes. For each collision it draws a red circle with size depending on collision strength. Not interactive.

_images/contact_and_no_flipy.png

contact_with_friction.py

Source: examples/contact_with_friction.py

This example spawns (bouncing) balls randomly on a L-shape constructed of two segment shapes. Displays collsion strength and rotating balls thanks to friction. Not interactive.

_images/contact_with_friction.png

copy_and_pickle.py

Source: examples/copy_and_pickle.py

This example shows how you can copy, save and load a space using pickle.

_images/copy_and_pickle.png

damped_rotary_spring_pointer.py

Source: examples/damped_rotary_spring_pointer.py

This example showcase an arrow pointing or aiming towards the cursor.

_images/damped_rotary_spring_pointer.png

deformable.py

Source: examples/deformable.py

This is an example on how the autogeometry can be used for deformable terrain.

_images/deformable.png

flipper.py

Source: examples/flipper.py

A very basic flipper game.

_images/flipper.png

index_video.py

Source: examples/index_video.py

Program used to generate the logo animation on the pymunk main page.

This program will showcase several features of Pymunk, such as collisions, debug drawing, automatic generation of shapes from images, motors, joints and sleeping bodies.

_images/index_video.png

newtons_cradle.py

Source: examples/newtons_cradle.py

A screensaver version of Newton’s Cradle with an interactive mode.

_images/newtons_cradle.png

planet.py

Source: examples/planet.py

Showcase of planets/satellites (small boxes) orbiting around a star.

Uses a custom velocity function to manually calculate the gravity, assuming the star is in the middle and is massive enough that the satellites does not affect it.

This is a port of the Planet demo included in Chipmunk.

_images/planet.png

platformer.py

Source: examples/platformer.py

Showcase of a very basic 2d platformer

The red girl sprite is taken from Sithjester’s RMXP Resources: http://untamed.wild-refuge.net/rmxpresources.php?characters

Note

The code of this example is a bit messy. If you adapt this to your own code you might want to structure it a bit differently.

_images/platformer.png

playground.py

Source: examples/playground.py

A basic playground. Most interesting function is draw a shape, basically move the mouse as you want and pymunk will approximate a Poly shape from the drawing.

_images/playground.png

point_query.py

Source: examples/point_query.py

This example showcase point queries by highlighting the shape under the mouse pointer.

_images/point_query.png

pygame_util_demo.py

Source: examples/pygame_util_demo.py

Showcase what the output of pymunk.pygame_util draw methods will look like.

See pyglet_util_demo.py for a comparison to pyglet.

_images/pygame_util_demo.png

pyglet_util_demo.py

Source: examples/pyglet_util_demo.py

Showcase what the output of pymunk.pyglet_util draw methods will look like.

See pygame_util_demo.py for a comparison to pygame.

_images/pyglet_util_demo.png

shapes_for_draw_demos.py

Source: examples/shapes_for_draw_demos.py

Helper function fill_space for the draw demos. Adds a lot of stuff to a space.

slide_and_pinjoint.py

Source: examples/slide_and_pinjoint.py

A L shape attached with a joint and constrained to not tip over.

This example is also used in the Get Started Tutorial.

_images/slide_and_pinjoint.png

spiderweb.py

Source: examples/spiderweb.py

Showcase of a elastic spiderweb (drawing with pyglet)

It is possible to grab one of the crossings with the mouse

_images/spiderweb.png

tank.py

Source: examples/tank.py

Port of the Chipmunk tank demo. Showcase a topdown tank driving towards the mouse, and hitting obstacles on the way.

_images/tank.png

using_sprites.py

Source: examples/using_sprites.py

Very basic example of using a sprite image to draw a shape more similar how you would do it in a real game instead of the simple line drawings used by the other examples.

_images/using_sprites.png

using_sprites_pyglet.py

Source: examples/using_sprites_pyglet.py

This example is a clone of the using_sprites example with the difference that it uses pyglet instead of pygame to showcase sprite drawing.

_images/using_sprites_pyglet.png

Additional Examples

kivy_pymunk_demo

Source: additional_examples/kivy_pymunk_demo

A rudimentary port of the intro video used for the intro animation on pymunk.org. The code is tested on both Windows and Android.

Note that it doesn’t display Kivy best practices, the intro_video code was just converted to Kivy in the most basic way to show that its possible, its not supposed to show the best way to structure a Kivy application using Pymunk.

_images/kivy_pymunk_demo.png

no_dependencies.py

Source: additional_examples/no_dependencies.py

Very simple example that does not depend on any third party library such as pygame or pyglet like the other examples.

py2exe_setup__basic_test.py

Source: additional_examples/py2exe_setup__basic_test.py

Simple example of py2exe to create a exe of the no_dependencies example.

Tested on py2exe 0.13.0.0 on python 3.11