Pymunx/Documentation: Difference between revisions

From OLPC
Jump to navigation Jump to search
(ref 0.4)
mNo edit summary
 
(4 intermediate revisions by the same user not shown)
Line 5: Line 5:
<table border="0">
<table border="0">
<tr><td style="padding-left:80px;">[[#pymunx.__init__|pymunx.__init__]]</td>
<tr><td style="padding-left:80px;">[[#pymunx.__init__|pymunx.__init__]]</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Init function: pymunk, get flags, get screen size, init space</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.init_colors|pymunx.init_colors]]</td>
<tr><td style="padding-left:80px;">[[#pymunx.init_colors|pymunx.init_colors]]</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Init the random color array</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.set_color|pymunx.set_color]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.set_color|pymunx.set_color]]'''</td>
<td style="padding-left:20px;">Set a fixed color for new elements</td></tr>

<tr><td style="padding-left:80px;">'''[[#pymunx.reset_color|pymunx.reset_color]]'''</td>
<td style="padding-left:20px;">Set random colors for new elements</td></tr>

<tr><td style="padding-left:80px;">'''[[#pymunx.get_color|pymunx.get_color]]'''</td>
<td style="padding-left:20px;">Get either fixed or random color (in rgb)</td></tr>

<tr><td style="padding-left:80px;">'''[[#pymunx.toggle_help|pymunx.toggle_help]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;"></td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.reset_color|pymunx.reset_color]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.set_info|pymunx.set_info]]'''</td>
<td style="padding-left:20px;">Set the info-text (for the upper left corner)</td></tr>

<tr><td style="padding-left:80px;">[[#pymunx.messagebox_show|pymunx.messagebox_show]]</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;"></td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.get_color|pymunx.get_color]]</td>
<tr><td style="padding-left:80px;">[[#pymunx.messagebox_hide|pymunx.messagebox_hide]]</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;"></td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.set_info|pymunx.set_info]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.screenshot|pymunx.screenshot]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;"></td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.clear|pymunx.clear]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.screencast_start|pymunx.screencast_start]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;"></td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.flipy|pymunx.flipy]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.screencast_stop|pymunx.screencast_stop]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;"></td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.vec2df|pymunx.vec2df]]</td>
<tr><td style="padding-left:80px;">[[#pymunx.screencast_encode_callback|pymunx.screencast_encode_callback]]</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;"></td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.autoset_screen_size|pymunx.autoset_screen_size]]</td>
<tr><td style="padding-left:80px;">[[#pymunx.save_surface|pymunx.save_surface]]</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;"></td></tr>

<tr><td style="padding-left:80px;">'''[[#pymunx.clear|pymunx.clear]]'''</td>
<td style="padding-left:20px;">Clear & Restart the Physics Space</td></tr>

<tr><td style="padding-left:80px;">[[#pymunx.flipy|pymunx.flipy]]</td>
<td style="padding-left:20px;">Convert pygame y-coordinate to chipmunk's</td></tr>

<tr><td style="padding-left:80px;">[[#pymunx.vec2df|pymunx.vec2df]]</td>
<td style="padding-left:20px;">Convert a pygame pos to a vec2d (with flipped y coordinate)</td></tr>

<tr><td style="padding-left:80px;">'''[[#pymunx.autoset_screen_size|pymunx.autoset_screen_size]]'''</td>
<td style="padding-left:20px;">Get screensize from pygame or set manually. Call on resize</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.is_inside|pymunx.is_inside]]</td>
<tr><td style="padding-left:80px;">[[#pymunx.is_inside|pymunx.is_inside]]</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Check if pos is inside screen + tolerance</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.update|pymunx.update]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.update|pymunx.update]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Update the physics space</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.draw|pymunx.draw]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.draw|pymunx.draw]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Call draw_shape for each element and remove outside ones</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.draw_shape|pymunx.draw_shape]]</td>
<tr><td style="padding-left:80px;">[[#pymunx.draw_shape|pymunx.draw_shape]]</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Draw a given shape (circle, segment, poly)</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.add_wall|pymunx.add_wall]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.add_wall|pymunx.add_wall]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Add a fixed wall</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.add_ball|pymunx.add_ball]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.add_ball|pymunx.add_ball]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Add a Ball</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.add_square|pymunx.add_square]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.add_square|pymunx.add_square]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Add a Square (Box)</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.add_poly|pymunx.add_poly]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.add_poly|pymunx.add_poly]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Add a Polygon</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.apply_impulse|pymunx.apply_impulse]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.apply_impulse|pymunx.apply_impulse]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Apply an Impulse to a given Shape's body</td></tr>


<tr><td style="padding-left:80px;">[[#pymunx.get_element_count|pymunx.get_element_count]]</td>
<tr><td style="padding-left:80px;">'''[[#pymunx.get_element_count|pymunx.get_element_count]]'''</td>
<td style="padding-left:20px;"></td></tr>
<td style="padding-left:20px;">Returns the current element count</td></tr>


</table>
</table>
: ''(bold are the functions you'll likely want to use :)''
: ''(bold are the functions you'll likely want to use :)''





Line 98: Line 123:
::* Get a color - either the fixed one or the next from self.colors
::* Get a color - either the fixed one or the next from self.colors
::* Returns: clr = ((R), (G), (B))
::* Returns: clr = ((R), (G), (B))


=== pymunx.toggle_help ===
:<big>pymunx.toggle_help ()</big>
::* Toggle Help on and off
::* Returns: -




Line 105: Line 136:
::* Parameter: txt == str()
::* Parameter: txt == str()
::* Returns: -
::* Returns: -


=== pymunx.messagebox_show ===
:<big>pymunx.messagebox_show (txt, delay=None)</big>
::* Add a message box at the center on drawing
::* Parameter: txt == str()
::* Optional: delay (in seconds, until box disappears)
::* Returns: -


=== pymunx.messagebox_hide ===
:<big>pymunx.messagebox_hide ()</big>
::* Hide the message box
::* Returns: -


=== pymunx.screenshot ===
:<big>pymunx.screenshot (filename='screenshot', ext='tga')</big>
::* Make a Screenshot in .tga format, if there is no screencast running
::* Optional: filename == str() (no extension), ext == str() (does not work -- always saves as .tga)
::* Returns: -


=== pymunx.screencast_start ===
:<big>pymunx.screencast_start (fn='screencast')</big>
::* Start saving one image per frame in snapshots/ (as .tga), for encoding with mencoder
::* Optional: fn == str() (filename without extension)
::* Returns: -


=== pymunx.screencast_stop ===
:<big>pymunx.screencast_stop ()</big>
::* Stop the image saving and start encoding the images to a .avi video (with mencoder)
::* Returns: -


=== pymunx.screencast_encode_callback ===
:<big>pymunx.screencast_encode_callback ()</big>
::* Callback function when encoding is done -> remove info & resume physics
::* Returns: -


=== pymunx.save_surface ===
:<big>pymunx.save_surface (surface, fn='surface', ext='tga')</big>
::* Saves a surface to a local file
::* Parameter: surface == pygame.Surface()
::* Optional: fn == str(fn_without_ext), ext == str()
::* Returns: fullname == str(full_name_of_file)




=== pymunx.clear ===
=== pymunx.clear ===
:<big>pymunx.clear ()</big>
:<big>pymunx.clear ()</big>
::* Clears the Space
::* Clear & Reset the Physic Space (Remove all Elements)
::* Returns: -
::* Returns: -


Line 122: Line 201:
=== pymunx.vec2df ===
=== pymunx.vec2df ===
:<big>pymunx.vec2df (pos)</big>
:<big>pymunx.vec2df (pos)</big>
::* Return a vec2d with flipped y
::* pos -> vec2d (with flipped y)
::* Parameter: pos == vec2d(int(x), int(chipmunk_y))
::* Parameter: pos == (int(x), int(pygame_y))
::* Returns: -
::* Returns: vec2d(int(x), int(chipmunk_y))




Line 150: Line 229:


=== pymunx.draw ===
=== pymunx.draw ===
:<big>pymunx.draw (surface)</big>
:<big>pymunx.draw (surface, addtext=True)</big>
::* Draw All Shapes, and removes the ones outside
::* Draw All Shapes, and removes the ones outside
::* Parameter: surface == pygame.Surface()
::* Parameter: surface == pygame.Surface()
::* Optional: addtext == True/False (if True, also add Info-Text to surface)
::* Returns: -
::* Returns: -


Line 164: Line 244:


=== pymunx.add_wall ===
=== pymunx.add_wall ===
:<big>pymunx.add_wall (p1, p2, friction=1.0, elasticity=0.5, mass=inf, inertia=inf)</big>
:<big>pymunx.add_wall (p1, p2, friction=1.0, elasticity=0.1, mass=inf, inertia=inf)</big>
::* Adds a fixed Wall pos = (int(x), int(y))
::* Adds a fixed Wall pos = (int(x), int(y))
::* Parameter: p1 == pos(startpoint), p2 == pos(endpoint)
::* Parameter: p1 == pos(startpoint), p2 == pos(endpoint)

Latest revision as of 21:23, 10 March 2008

pymunx

Physics API for easy usage of chipmunk physics engine in pygame (with pymunk)
pymunx.__init__ Init function: pymunk, get flags, get screen size, init space
pymunx.init_colors Init the random color array
pymunx.set_color Set a fixed color for new elements
pymunx.reset_color Set random colors for new elements
pymunx.get_color Get either fixed or random color (in rgb)
pymunx.toggle_help
pymunx.set_info Set the info-text (for the upper left corner)
pymunx.messagebox_show
pymunx.messagebox_hide
pymunx.screenshot
pymunx.screencast_start
pymunx.screencast_stop
pymunx.screencast_encode_callback
pymunx.save_surface
pymunx.clear Clear & Restart the Physics Space
pymunx.flipy Convert pygame y-coordinate to chipmunk's
pymunx.vec2df Convert a pygame pos to a vec2d (with flipped y coordinate)
pymunx.autoset_screen_size Get screensize from pygame or set manually. Call on resize
pymunx.is_inside Check if pos is inside screen + tolerance
pymunx.update Update the physics space
pymunx.draw Call draw_shape for each element and remove outside ones
pymunx.draw_shape Draw a given shape (circle, segment, poly)
pymunx.add_wall Add a fixed wall
pymunx.add_ball Add a Ball
pymunx.add_square Add a Square (Box)
pymunx.add_poly Add a Polygon
pymunx.apply_impulse Apply an Impulse to a given Shape's body
pymunx.get_element_count Returns the current element count
(bold are the functions you'll likely want to use :)


pymunx.__init__

pymunx.__init__ (gravity= (0.0,-900.0))
  • Init function: init pymunk, get screen size, init space, ...
  • Parameter: gravity == (int(x), int(y))
  • Returns: pymunx()


pymunx.init_colors

pymunx.init_colors ()
  • Init self.colors with a fix set of hex colors
  • Returns: -


pymunx.set_color

pymunx.set_color (clr)
  • Set a color for all future Elements, until reset_color() is called
  • Parameter: clr == (Hex or RGB)
  • Returns: -


pymunx.reset_color

pymunx.reset_color ()
  • All Elements from now on will be drawn in random colors
  • Returns: -


pymunx.get_color

pymunx.get_color ()
  • Get a color - either the fixed one or the next from self.colors
  • Returns: clr = ((R), (G), (B))


pymunx.toggle_help

pymunx.toggle_help ()
  • Toggle Help on and off
  • Returns: -


pymunx.set_info

pymunx.set_info (txt)
  • Create the Surface for the Infotext at the Upper Left Corner
  • Parameter: txt == str()
  • Returns: -


pymunx.messagebox_show

pymunx.messagebox_show (txt, delay=None)
  • Add a message box at the center on drawing
  • Parameter: txt == str()
  • Optional: delay (in seconds, until box disappears)
  • Returns: -


pymunx.messagebox_hide

pymunx.messagebox_hide ()
  • Hide the message box
  • Returns: -


pymunx.screenshot

pymunx.screenshot (filename='screenshot', ext='tga')
  • Make a Screenshot in .tga format, if there is no screencast running
  • Optional: filename == str() (no extension), ext == str() (does not work -- always saves as .tga)
  • Returns: -


pymunx.screencast_start

pymunx.screencast_start (fn='screencast')
  • Start saving one image per frame in snapshots/ (as .tga), for encoding with mencoder
  • Optional: fn == str() (filename without extension)
  • Returns: -


pymunx.screencast_stop

pymunx.screencast_stop ()
  • Stop the image saving and start encoding the images to a .avi video (with mencoder)
  • Returns: -


pymunx.screencast_encode_callback

pymunx.screencast_encode_callback ()
  • Callback function when encoding is done -> remove info & resume physics
  • Returns: -


pymunx.save_surface

pymunx.save_surface (surface, fn='surface', ext='tga')
  • Saves a surface to a local file
  • Parameter: surface == pygame.Surface()
  • Optional: fn == str(fn_without_ext), ext == str()
  • Returns: fullname == str(full_name_of_file)


pymunx.clear

pymunx.clear ()
  • Clear & Reset the Physic Space (Remove all Elements)
  • Returns: -


pymunx.flipy

pymunx.flipy (y)
  • Convert pygame y-coordinate to chipmunk's
  • Parameter: y == int()
  • Returns: int(y_new)


pymunx.vec2df

pymunx.vec2df (pos)
  • pos -> vec2d (with flipped y)
  • Parameter: pos == (int(x), int(pygame_y))
  • Returns: vec2d(int(x), int(chipmunk_y))


pymunx.autoset_screen_size

pymunx.autoset_screen_size (size=None)
  • Get the current PyGame Screen Size, or sets it manually
  • Optional: size == (int(width), int(height))
  • Returns: -


pymunx.is_inside

pymunx.is_inside (pos, tolerance=3000)
  • Check if pos is inside screen + tolerance
  • Parameter: pos == (int(x), int(y))
  • Optional: tolerance == int(pixels)
  • Returns: True if inside, False if outside


pymunx.update

pymunx.update (fps=50.0, steps=5)
  • Update the Physics Space
  • Optional: fps == int(fps), steps == int(space_steps_per_udate)
  • Returns: -


pymunx.draw

pymunx.draw (surface, addtext=True)
  • Draw All Shapes, and removes the ones outside
  • Parameter: surface == pygame.Surface()
  • Optional: addtext == True/False (if True, also add Info-Text to surface)
  • Returns: -


pymunx.draw_shape

pymunx.draw_shape (surface, shape)
  • Draw a shape (can be either Circle, Segment or Poly).
  • Parameter: surface == pygame.Surface(), shape == pymunk.Shape()
  • Returns: True if shape is inside screen, else False (for removal)


pymunx.add_wall

pymunx.add_wall (p1, p2, friction=1.0, elasticity=0.1, mass=inf, inertia=inf)
  • Adds a fixed Wall pos = (int(x), int(y))
  • Parameter: p1 == pos(startpoint), p2 == pos(endpoint)
  • Optional: See #physical_parameters
  • Returns: pymunk.Shape() (=> .Segment())


pymunx.add_ball

pymunx.add_ball (pos, radius=15, density=0.1, inertia=1000, friction=0.5, elasticity=0.3)
  • Adds a Ball
  • Parameter: pos == (int(x), int(y))
  • Optional: See #physical_parameters
  • Returns: pymunk.Shape() (=> .Circle())


pymunx.add_square

pymunx.add_square (pos, a=18, density=0.1, friction=0.2, elasticity=0.3)
  • Adding a Square | Note that a is actually half a side, due to vector easyness :)
  • Parameter: pos == (int(x), int(y))
  • Optional: a == (sidelen/2) | #physical_parameters
  • Returns: pymunk.Shape() (=> .Poly())


pymunx.add_poly

pymunx.add_poly (points, density=0.1, friction=2.0, elasticity=0.3)
  • Mass will be calculated out of mass = A * density
  • Parameter: points == [(int(x), int(y)), (int(x), int(y)), ...]
  • Optional: See #physical_parameters
  • Returns: pymunk.Shape() (=> .Poly())


pymunx.apply_impulse

pymunx.apply_impulse (shape, impulse_vector)
  • Apply an Impulse to a given Shape
  • Parameter: shape == pymunk.Shape(), impulse_vector == (int(x), int(y))
  • Returns: -


pymunx.get_element_count

pymunx.get_element_count ()
  • Get the current (approx.) element count
  • Returns: int(element_count)