graphics
—Graphic
subclasses¶
-
class
Colour
(colour, rect, layer=0)[source]¶ Bases:
engine.gfx.graphic.Graphic
A solid rect of colour.
Parameters: - colour – a colour to draw, as accepted by
engine.util.normalise_colour()
. - rect – Pygame-style rect to draw in, or just a
(width, height)
size to use a rect with position(0, 0)
. - layer – as taken by
Graphic
.
fill()
corresponds to a builtin transform.-
colour
¶ As taken by constructor; set as necessary.
- colour – a colour to draw, as accepted by
-
class
Text
(text, renderer, pos=(0, 0), options={}, layer=0)[source]¶ Bases:
engine.gfx.graphic.Graphic
Graphic displaying rendered text.
Parameters: - text – text to render; may contain line breaks to display separate lines.
- renderer –
text.TextRenderer
instance or the name a renderer is stored under inGame.text_renderers
. - pos – as taken by
Graphic
. - options – rendering options to override defaults, as taken by
TextRenderer.render()
. All options can be get and set as properties of this instance, and all are guaranteed to exist, even if not given in this argument. - layer – as taken by
Graphic
.
-
nlines
= None¶ Number of lines of text rendered.
-
text
¶ Text to render (as taken by constructor).
-
renderer
¶ text.TextRenderer
instance to use.
-
class
Animation
(imgs, pos=(0, 0), layer=0, [scheduler, ]pool=conf.DEFAULT_RESOURCE_POOL, res_mgr=conf.GAME.resources)[source]¶ Bases:
engine.gfx.graphic.Graphic
An animated graphic.
Parameters: - imgs – a sequence of images as part of the animation; each can be a Pygame
surface, a filename to load an surface from, or a
Graphic
instance (in which case it is removed from anyGraphicsManager
it is in). Note that autil.Spritemap
instance is a valid form for this argument. - scheduler –
sched.Scheduler
instance to use for timing; if not given, animations can only be played when the graphic is contained by aGraphicsManager
(and trying to do so otherwise raisesRuntimeError
).
Other arguments are as taken by
Graphic
.Note that when an animation is playing and the image changes,
Graphic.anchor
is respected.For example, to play the frames in a spritemap consisting of a single row:
Animation(Spritemap('map.png', 4)).add('run', frame_time=.1).play('run')
-
graphics
= None¶ list
ofimgs
as passed to the constructor, except that they are never filenames.
-
sequences
= None¶ {name: (indices, frame_time)}
frame sequences (‘animations’) as added throughadd()
.
-
scheduler
= None¶ The
scheduler
argument passed to the constructor.
-
playing
= None¶ The currently playing sequence (name), or
None
.
-
queued
= None¶ list
of queued sequences to play after the current sequence has finished, each(name, repeat, frame_time, cb)
as added throughqueue()
, starting with the first to be played. Items may be removed manually.
-
repeat
= None¶ The current repeat of the playing sequence, starting from
0
if it hasn’t started repeating yet, orNone
.
-
repeats
= None¶ The total number of repeats that will be performed for the currently playing sequence (not including the first playthrough), or
True
if it will play forever, orNone
.
-
frame
= None¶ The current frame in the currently playing sequence (within the current repeat), as an integer starting from
0
, orNone
.
-
frame_time
¶ A default for the time between animation frames.
This is a value in seconds, that applies for all animation sequences. If not set, a value must be defined for each sequence. Changes do not take effect until the current frame has finished, if any is running.
-
speed
¶ Running speed of any animation.
This is a multiplier, where
1
is the default speed, and anything higher decreases frame times. Changes do not take effect until the current frame has finished, if any.
-
add
(name, *indices[, frame_time]) → self[source]¶ Add a sequence to
sequences
to play back later.Parameters: - name – the name to give the sequence (any hashable object). If a sequence with this name already exists, it is overwritten; if it is currently playing or queued, it is stopped/unqueued.
- indices – any number of indices in
graphics
, defining the sequence of frames, or pass none for all frames in order. - frame_time – a default for the time between animation frames in seconds whenever this sequence is played. If not given, a value must be defined either through the constructor or each time the sequence is played.
-
add_multi
(sequences) → self[source]¶ Add a number of frame sequences.
Parameters: sequences – {name: data}
, wheredata
can be anindices
list
or(indices[, frame_time])
, and each of these is as taken byadd()
.
-
play
(name, repeat=True[, frame_time][, cb]) → self[source]¶ Play a frame sequence.
Parameters: - name – sequence name to play.
- repeat – whether to repeat the sequence once it has finished.
True
to repeat forever, else a number of repeats to perform (that is, the sequence is played(repeats + 1)
times). (False
is also valid.) - frame_time – the time between animation frames in seconds. If not given, a
value must be defined either as through the constructor or
in
add()
. - cb – a function to call when the animation ends (but not if it is stopped
through
stop()
or by starting another animation).
If a sequence is already being played, that sequence is canceled.
-
stop
(n_queued) → self[source]¶ Stop the currently running sequence, if any.
Parameters: n_queued – the number of subsequent queued sequences to cancel after stopping the running sequence.
-
queue
(name, repeat=True[, frame_time][, cb]) → self[source]¶ Queue a frame sequence for playing after any running sequence.
Arguments are as taken by
play()
.
- imgs – a sequence of images as part of the animation; each can be a Pygame
surface, a filename to load an surface from, or a
-
class
Tilemap
(grid, tile_data, [tile_types, ]pos=(0, 0), layer=0, [translate_type, ]cache_graphic=False, pool=conf.DEFAULT_RESOURCE_POOL, res_mgr=conf.GAME.resources)[source]¶ Bases:
engine.gfx.graphic.Graphic
A finite, flat grid of tiles.
Parameters: - grid – a
util.grid.Grid
defining the size and shape of the tiles in the tilemap, or thetile_size
argument toutil.grid.Grid
to create a new one with standard parameters. - tile_data –
a way of determining the tile type ID for each
(x, y)
tile in the grid, which is any object. This can be:- a list of columns, where each column is a list of IDs;
- a string with rows delimited by line breaks and each row a whitespace-delimited set of string IDs;
(s, col_delim, row_delim)
to specify custom delimiter characters for a strings
, where either or both delimiters can beNone
to split by whitespace/line breaks;- a filename from which to load a string with delimited IDs (the name may not contain whitespace);
(filename, col_delim, row_delim)
for a custom-delimited string in a file;- a
Graphic
, Pygame surface or filename (may not contain whitespace) to load an image from, and use the(r, g, b[, a])
colour tuples of the pixels in the surface as IDs; - if
grid
is autil.grid.Grid
: a function that takescol
androw
arguments as column and row indices in the grid, and returns the corresponding tile type ID; or - if
grid
is not autil.grid.Grid
:(get_tile_type, w, h)
, where get_tile_type is a function as defined previously, andw
andh
are the width and height of the grid, in tiles.
- tile_types –
a
tile_type_id -> tile_graphic
mapping—either a function or an object that supports indexing. If not given, the identity function is used.tile_type_id
is the tile type ID obtained from thetile_data
argument.tile_graphic
determines how the tile should be drawn; it may be:None
for an an empty (transparent) tile;- a colour (as taken by
engine.util.normalise_colour()
) to fill with; - a
Graphic
, Pygame surface or filename to load from, to copy aligned to the centre of the tile, clipped to fit; or (graphic, alignment=0, rect=graphic_rect)
withalignment
orrect
in any order or omitted, andgraphic
as in the above form.alignment
is as taken byengine.util.align_rect()
, andrect
is the Pygame-style rect within the source surface ofgraphic
to copy from. Regardless ofalignment
,rect
is clipped to fit in the tile around its centre.
Note that a
util.Spritemap
is a valid form for this argument. - pos,layer – as taken by
Graphic
. - translate_type – a function that takes tile type IDs obtained from the
tile_data
argument and returns the ID to use with thetile_types
argument in obtainingtile_graphic
; does nothing by default. - cache_graphic – whether to cache and reuse
tile_graphic
for each tile type. You might want to passTrue
if requestingtile_graphic
fromtile_types
generates a surface. IfTrue
, tile type IDs must be hashable (after translation bytranslate_type
). - pool,res_mgr – as taken by
Graphic
.
This is meant to be used for static tilemaps—that is, where the appearance of each tile type never changes.
-
grid
= None¶ The
util.grid.Grid
covered.
- grid – a
-
class
Grid
(grid, gap_colour='aaa', bg_colour='0000', pos=(0, 0), layer=0)[source]¶ Bases:
engine.gfx.graphic.Graphic
Drawable wrapper for
util.grid.Grid
.Parameters: - grid – a
util.grid.Grid
instance. - gap_colour – colour in-between tiles, as accepted by
engine.util.normalise_colour()
; may have alpha. - bg_colour – colour within tiles, as accepted by
engine.util.normalise_colour()
. - pos,layer – as taken by
Graphic
.
- grid – a
-
class
InfiniteGrid
(grid, rect, gap_colour='aaa', bg_colour='0000', pos=(0, 0), layer=0)[source]¶ Bases:
engine.gfx.graphic.Graphic
Drawable wrapper for
util.grid.InfiniteGrid
.Parameters: - grid – a
util.grid.InfiniteGrid
instance. - rect – Pygame-style rect within
grid
to draw. - gap_colour – colour in-between tiles, as accepted by
engine.util.normalise_colour()
; may have alpha. - bg_colour – colour within tiles, as accepted by
engine.util.normalise_colour()
. - pos,layer – as taken by
Graphic
.
-
grid
= None¶ As passed to the constructor.
-
view_rect
¶ As the
rect
argument taken by the constructor.Graphic.anchor
is respected when this is changed.
-
gap_colour
¶ As passed to the constructor.
-
bg_colour
¶ As passed to the constructor.
- grid – a