Creating PointShapes - skytreader/PyGame-Objects GitHub Wiki

PyGame Objects provides a convenient way to draw simple 2D shapes in the form of core.shapes.PointShape. As the name implies, a PointShape is just a collection of points that defines a shape.

Why PointShapes?

PyGame Objects already supports [images and sprites](https://github.com/skytreader/PyGame-Objects/wiki/Image-Objects-as-(PyRo\)Sprite-Models). Why would anyone want to use a PointShape (which can get cumbersome) when there are images and sprites?

As discussed, images (and, consequently, sprites) are not scalable. For simple figures that would appear in your games, it may be more feasible to define a PointShape rather than maintain images for the figure. Also, PointShape objects support a limited form of animation.

PointShape objects also support collision detection with other PointShape objects. However there is still much work to be done. See this issue.

Creating PointShapes

The PointShape constructor requires two optional arguments: the first one is a list of core.shape.Point objects which defines the shape while the other one is the stroke color of your PointShape when it gets drawn. The point list defaults to an empty list when not specified while the color defaults to black.

Note that the point list is drawn by connecting the first point with the second point, the second point with the third point, and so on. The last point is then connected to the first point to form a closed figure.

core.shape.Point

This class represents 2D points. It holds two numbers, no more no less, as well as some common functions applied to Points like getting the Euclidean distance (distance) and checking for equality (__eq__).

The x and y components of a Point can be accessed and set as properties. The class also defines the __str__ method for printing out Points during debugging.

Basic Animation

As discussed earlier, PointShape objects support limited animation. This comes in the form of translation and transformation.

(In Adobe-Flash speak, translation is "motion tween" while transformation is "shape tween". But then, PointShape's version of these features are still very rough to merit comparison.)

Translation

PointShape objects have the translate method which takes in the amount of movement dx along the x-axis and the amount of movement dy along the y-axis, both specified in pixels.

Transformation

PointShape provides the add_point method to facilitate transformation. As the name implies, this adds a point to the point list and thus alters the way a PointShape is drawn.

add_point takes in two parameters, the second one optional. The first parameter is a Point object which is the point to be added to the list. The second parameter, when present, indicates the index in the list in which this Point is added (remember how a PointShape drawn and remember that the list is zero-indexed).

If the second parameter is not specified, the Point is added at the end of the list.

Collision Detection

PointShapes support collision detection but, as of now, it is only able to detect collision between fellow PointShape objects. The collision detection algorithm supported is similar to that of pygame.Sprite.

First of all, note that PointShapes have a collision_box property (from core.shape.CollisionBox), similar to pygame.Sprite.sprite's rect. This collision box is automatically managed by PointShape whenever the shape changes (say, due to translation or tranformation).

To detect collision between two PointShapes apply the has_collided method of the collision_box of one PointShape to the collision_box of the other PointShape.

Dirty code sample: pointshape_a.collision_box.has_collided(pointshape_b.collision_box)