pygamelib.gfx.core.SpriteCollection

class pygamelib.gfx.core.SpriteCollection(data={})

SpriteCollection is a dictionnary class that derives collections.UserDict.

Its main goal is to provide an easy to use object to load and save sprite files. On top of traditional dict method, it provides the following capabilities:

  • loading and writing from and to JSON files,
  • data serialization,
  • shortcut to add sprites to the dictionnary.

A SpriteCollection is an unordered indexed list of Sprites (i.e a dictionnary).

Sprites are indexed by their names in that collection.

Example:

# Load a sprite file
sprites_village1 = SpriteCollection.load_json_file('gfx/village1.spr')
# display the Sprites with their name
for sprite_name in sprites_village1:
    print(f'{sprite_name}:\n{sprites_village1[sprite_name]}')
# Add an empty sprite with name 'house_placeholder'
sprites_village1.add( Sprite(name='house_placeholder') )
# This is absolutely equivalent to:
sprites_village1['house_placeholder'] = Sprite(name='house_placeholder')
# And now rewrite the sprite file with the new placeholder house
sprites_village1.to_json_file('gfx/village1.spr')
__init__(data={})

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__([data]) Initialize self.
add(sprite) Add a Sprite to the collection.
clear()
copy()
fromkeys(iterable[, value])
get(k[,d])
items()
keys()
load(data) Load serialized data and return a new SpriteCollection object.
load_json_file(filename) Load a JSON sprite file into a new SpriteCollection object.
pop(k[,d]) If key is not found, d is returned if given, otherwise KeyError is raised.
popitem() as a 2-tuple; but raise KeyError if D is empty.
serialize() Return a serialized version of the SpriteCollection.
setdefault(k[,d])
to_json_file(filename) Export the SpriteCollection object in JSON and writes it on the disk.
update([E, ]**F) If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
values()