SpriteCollection

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

Bases: collections.UserDict

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.
rename(old_key, new_key) Rename a key in the collection.
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()
add(sprite)

Add a Sprite to the collection. This method is simply a shortcut to the usual dictionnary affectation. The collection requires the name of the Sprite to be the key. That method does that automatically.

Parameters:sprite (Sprite) – A Sprite object to add to the collection.

Warning

As SpriteCollection index Sprites by their name if you change the Sprite’s name after adding it to the collection you will need to manually update the keys.

Example:

sprites_village1 = SpriteCollection.load_json_file('gfx/village1.spr')
new_village = SpriteCollection()
new_village.add( copy.deepcopy( sprites_village1.get('bakery') ) )
print( new_village['bakery'] )
clear() → None. Remove all items from D.
copy()
classmethod fromkeys(iterable, value=None)
get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
classmethod load(data)

Load serialized data and return a new SpriteCollection object.

Parameters:data (str) – Serialized data that need to be expanded into objects.
Returns:A new SpriteCollection object.
Return type:SpriteCollection

Example:

sprites_village1 = SpriteCollection.load(
    sprites_village_template.serialize()
)
static load_json_file(filename)

Load a JSON sprite file into a new SpriteCollection object.

Parameters:filename (str) – The complete path (relative or absolute) to the sprite file.
Returns:A new SpriteCollection object.
Return type:SpriteCollection

Example:

sprites_village1 = SpriteCollection.load_json_file('gfx/village1.spr')
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

rename(old_key, new_key)

Rename a key in the collection.

This methods also takes care of renaming the Sprite associated with the old key name.

Parameters:
  • old_key (str) – The key to rename
  • new_key (str) – The new key name

Example:

my_collection.rename('panda', 'panda walk 01')
serialize()

Return a serialized version of the SpriteCollection. The serialized data can be pass to the JSON module to export.

Returns:The SpriteCollection object serialized as a dictionnary.
Return type:dict

Example:

data = sprites_village1.serialize()
setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
to_json_file(filename)

Export the SpriteCollection object in JSON and writes it on the disk.

Parameters:filename (str) – The complete path (relative or absolute) to the sprite file to write.

Example:

sprites_village1.to_json_file('gfx/village1.spr')
update([E, ]**F) → None. Update D from mapping/iterable E and 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() → an object providing a view on D's values