Elements/Documentation: Difference between revisions

From OLPC
Jump to navigation Jump to search
m (New page: __NOTOC__ == elements == : ''Physics API for easy usage of chipmunk physics engine in pygame (with pymunk)'' <table border="0"> <tr><td style="padding-left:80px;">[[#elements.__init__|ele...)
 
(Replacing page with 'The documentation for the new '''Elements''' with support for the Box2D engine will come very soon. In the meantime, the Chipmunk engine is almost fully supported by the prev...')
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
The documentation for the new '''Elements''' with support for the Box2D engine will come very soon.
__NOTOC__
== elements ==
: ''Physics API for easy usage of chipmunk physics engine in pygame (with pymunk)''


In the meantime, the Chipmunk engine is almost fully supported by the previous version of '''Elements''': [[Elements/Chipmunk]] ([[Elements/Chipmunk/Documentation|Doc]])
<table border="0">
<tr><td style="padding-left:80px;">[[#elements.__init__|elements.__init__]]</td>
<td style="padding-left:20px;">Init function: pymunk, get flags, get screen size, init space</td></tr>

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

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

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

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

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

<tr><td style="padding-left:80px;">'''[[#elements.set_info|elements.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;">[[#elements.messagebox_show|elements.messagebox_show]]</td>
<td style="padding-left:20px;"></td></tr>

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

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

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

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

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

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

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

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

<tr><td style="padding-left:80px;">[[#elements.vec2df|elements.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;">'''[[#elements.autoset_screen_size|elements.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;">[[#elements.is_inside|elements.is_inside]]</td>
<td style="padding-left:20px;">Check if pos is inside screen + tolerance</td></tr>

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

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

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

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

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

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

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

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

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

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



=== elements.__init__ ===
:<big>elements.__init__ (gravity= (0.0,-900.0))</big>
::* Init function: init pymunk, get screen size, init space, ...
::* Parameter: gravity == (int(x), int(y))
::* Returns: Elements()


=== elements.init_colors ===
:<big>elements.init_colors ()</big>
::* Init self.colors with a fix set of hex colors
::* Returns: -


=== elements.set_color ===
:<big>elements.set_color (clr)</big>
::* Set a color for all future Elements, until reset_color() is called
::* Parameter: clr == (Hex or RGB)
::* Returns: -


=== elements.reset_color ===
:<big>elements.reset_color ()</big>
::* All Elements from now on will be drawn in random colors
::* Returns: -


=== elements.get_color ===
:<big>elements.get_color ()</big>
::* Get a color - either the fixed one or the next from self.colors
::* Returns: clr = ((R), (G), (B))


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


=== elements.set_info ===
:<big>elements.set_info (txt)</big>
::* Create the Surface for the Infotext at the Upper Left Corner
::* Parameter: txt == str()
::* Returns: -


=== elements.messagebox_show ===
:<big>elements.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: -


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


=== elements.screenshot ===
:<big>elements.screenshot (filename='elements.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: -


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


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


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


=== elements.save_surface ===
:<big>elements.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)


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


=== elements.flipy ===
:<big>elements.flipy (y)</big>
::* Convert pygame y-coordinate to chipmunk's
::* Parameter: y == int()
::* Returns: int(y_new)


=== elements.vec2df ===
:<big>elements.vec2df (pos)</big>
::* pos -> vec2d (with flipped y)
::* Parameter: pos == (int(x), int(pygame_y))
::* Returns: vec2d(int(x), int(chipmunk_y))


=== elements.autoset_screen_size ===
:<big>elements.autoset_screen_size (size=None)</big>
::* Get the current PyGame Screen Size, or sets it manually
::* Optional: size == (int(width), int(height))
::* Returns: -


=== elements.is_inside ===
:<big>elements.is_inside (pos, tolerance=3000)</big>
::* Check if pos is inside screen + tolerance
::* Parameter: pos == (int(x), int(y))
::* Optional: tolerance == int(pixels)
::* Returns: True if inside, False if outside


=== elements.update ===
:<big>elements.update (fps=50.0, steps=5)</big>
::* Update the Physics Space
::* Optional: fps == int(fps), steps == int(space_steps_per_udate)
::* Returns: -


=== elements.draw ===
:<big>elements.draw (surface, addtext=True)</big>
::* 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: -


=== elements.draw_shape ===
:<big>elements.draw_shape (surface, shape)</big>
::* 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)


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


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


=== elements.add_square ===
:<big>elements.add_square (pos, a=18, density=0.1, friction=0.2, elasticity=0.3)</big>
::* 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())


=== elements.add_poly ===
:<big>elements.add_poly (points, density=0.1, friction=2.0, elasticity=0.3)</big>
::* 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())


=== elements.apply_impulse ===
:<big>elements.apply_impulse (shape, impulse_vector)</big>
::* Apply an Impulse to a given Shape
::* Parameter: shape == pymunk.Shape(), impulse_vector == (int(x), int(y))
::* Returns: -


=== elements.get_element_count ===
:<big>elements.get_element_count ()</big>
::* Get the current (approx.) element count
::* Returns: int(element_count)

Latest revision as of 19:43, 18 March 2008

The documentation for the new Elements with support for the Box2D engine will come very soon.

In the meantime, the Chipmunk engine is almost fully supported by the previous version of Elements: Elements/Chipmunk (Doc)