container—graphics containers

class GraphicsGroup(x=0, y=0)[source]

Bases: object

Convenience wrapper for grouping a number of graphics in a simple way.

Arguments determine the group’s position (pos); unlike for graphics, this may be floating-point.

This is a {graphic: rel} mapping, where graphic is a Graphic instance and rel is the graphic’s (x, y) position relative to this group. Adding graphics is possible with something like group[graphic] = rel (instead of using add()).

graphic_attrs contains some properties of this GraphicsGroup which correspond to those of Graphic. These can be set to apply to all contained graphics.

graphic_attrs = ('layer', 'visible', 'blit_flags', 'anchor', 'rot_anchor', 'scale_fn', 'rotate_fn', 'rotate_threshold')

Attributes which are mapped to Graphic attributes.

rect

The pygame.Rect covered by graphics in this group.

The top-left of this is not necessarily the same as pos.

x

x co-ordinate of the group’s top-left corner.

y

y co-ordinate of the group’s top-left corner.

pos

[x , y ].

w

Width of rect.

h

Height of rect.

size

(w , h ).

move_by(dx=0, dy=0)[source]

Move by the given number of pixels.

add(*graphics)[source]

Add graphics.

Call either as add(graphic, dx=0, dy=0) for a single graphic, or pass any number of arguments which are (graphic, dx=0, dy=0) tuples or just graphic. In each case:

Parameters:
  • graphicGraphic instance or the img argument to Graphic to create one.
  • dx,dy – position relative to the group.
Returns:

a list of added Graphic instances (possibly created in this call), in the order given.

If any graphic is already in the group, this call changes its relative position (and unspecified dx and dy are unchanged, rather than set to 0).

Note that graphics need not be added to a GraphicsManager individually —set this using manager.

rm(*graphics)[source]

Remove graphics previously added using add().

Raises KeyError for missing graphics.

manager

The GraphicsManager to put graphics in.

class GraphicsManager(scheduler, [sfc, ]pos=(0, 0), layer=0)[source]

Bases: engine.gfx.graphic.Graphic

Draws things to a surface intelligently.

Parameters:
  • scheduler – a sched.Scheduler instance this manager should use for timing.
  • sfc – the surface to draw to; can be a (width, height) tuple to create a new transparent surface of this size. If not given or None, nothing is drawn. This becomes orig_sfc and can be changed using this attribute.

Other arguments are as taken by Graphic. Since this is a Graphic subclass, it can be added to other GraphicsManager instances and supports transformations. None of this can be done until the manager has a surface, however, and transformations are only applied in Graphic.surface, not in orig_sfc.

scheduler = None

The scheduler argument passed to the constructor.

graphics = None

{layer: graphics} dict, where graphics is a set of the graphics in layer layer, each as taken by add().

layers = None

A list of layers that contain graphics, lowest first.

orig_sfc

Like Graphic.orig_sfc.

This is the sfc argument passed to the constructor. Retrieving this causes all graphics to be drawn/updated first.

orig_size

The size of the surface before any transforms.

overlay

A Graphic which is always drawn on top, or None.

There may only ever be one overlay; changing this attribute removes any previous overlay from the GraphicsManager.

add(*graphics)[source]

Add graphics.

Takes any number of Graphic instances, and returns a list of added graphics.

rm(*graphics)[source]

Remove graphics.

Takes any number of Graphic instances. Missing graphics are ignored.

fade_to(t, colour=(0, 0, 0)[, resolution])[source]

Fade to a colour.

Parameters:
  • t – how many seconds to take to reach colour.
  • colour – the (R, G, B[, A = 255]) colour to fade to.
  • resolution – as taken by Scheduler.interp().

If already fading, the current colour is used as the initial colour; otherwise, the initial colour is taken to be (R, G, B, 0) for the given value of colour. After fading, the overlay persists; set overlay to None to remove it.

fade_from(t[, colour][, resolution])[source]

Fade from a colour to no overlay.

Parameters:
  • t – how many seconds to take to reach transparency.
  • colour – the (R, G, B[, A = 255]) colour to fade from; if not given, the current colour is used, else (0, 0, 0).
  • resolution – as taken by Scheduler.interp().

Any running fade is canceled, and the final colour is taken to be (R, G, B, 0) for the given value of colour. After fading, the overlay is removed.

fade(get_val, *args, **kw)[source]

Fade between colours.

Takes arguments like Scheduler.interp(), with set_val omitted.

Any currently running fade will be canceled. After fading, the overlay persists; set overlay to None to remove it.

cancel_fade()[source]

Cancel any currently running fade and remove the overlay.

draw(handle_dirty=True)[source]

Update the display (orig_sfc).

Parameters:handle_dirty – whether to propagate changed areas to the transformation pipeline implemented by Graphic. Pass False if you don’t intend to use this manager as a graphic.

Returns True if the entire surface changed, or a list of rects that cover changed parts of the surface, or False if nothing changed.