res—resource loading and caching


ResourceManager loader for images ('img').

Takes the filename to load from, under conf.IMG_DIR.

load_font(fn, size)[source]

ResourceManager loader for Pygame fonts ('font').

mk_font_keys(fn, size)

  • fn – font filename, under conf.FONT_DIR.
  • size – size this font should render at.
load_text(text, renderer, options={}, **kwargs) → (surface, num_lines)[source]

ResourceManager loader for rendering text ('text').

Parameters:renderertext.TextRenderer instance or the name a renderer is stored under in Game.text_renderers.

Other arguments are as taken by and the return value is as given by TextRenderer.render().

load_snd(snd) → new_sound[source]

ResourceManager loader for rendering sounds ('snd').

Parameters:snd – sound filename under conf.SOUND_DIR to load.
Returns:pygame.mixer.Sound object.
class ResourceManager[source]

Bases: object

Manage the loading and caching of resources.

Builtin resources loaders are in resource_loaders; to load a resource, you can use load(), or you can do, eg.

manager.img('border.png', pool='gui')

Documentation for builtin loaders is found in the load_<loader> functions in this module.


A list of the resource loaders available to this manager.


A list of the resource pools contained by this manager.

load(loader, *args, **kwargs, pool=conf.DEFAULT_RESOURCE_POOL, force_load=False) → data[source]

Load a resource.

  • loader – resource loader to use, as found in resource_loaders.
  • args – positional arguments to pass to the resource loader.
  • kwargs – keyword arguments to pass the the resource loader.
  • pool – the pool to cache the resource in.
  • force_load – whether to bypass the cache and reload the object through loader.

the loaded resource data.

This is equivalent to getattr(manager, loader)(*args, **kwargs, pool=conf.DEFAULT_RESOURCE_POOL).

register(name, load, mk_keys[, measure])[source]

Register a new resource loader.

  • name – the name to give the loader, as used in resource_loaders; must be hashable, and must be a string and a valid variable name if you want to be able to load resources like ResourceManager.img(). If already used, the existing loader is replaced.
  • load – a function to load a resource. Takes whatever arguments are necessary (you’ll pass these to load() or the generated dedicated method).
  • mk_keys – a function to generate hashable caching keys for a resource, given the same arguments as load. It should return an iterable object of keys, and the resource will be cached under all of them.
  • measure – a function to measure a resource’s size. Takes a resource as returned by load, and returns its size as a number. The default is to return 1 for any resource.
use(pool, user)[source]

Add a user to a pool, if not already added.

If a pool ever has no users, all resources cached in it are removed.

The pool need not already exist.

drop(pool, user)[source]

Drop a user from a pool, if present.

The pool need not already exist.


Get a set of users using the given pool.


Measure the resources cached in the given pools.

Returns:{loader: size} dict giving the total size of the resources cached for each loader, summed over all pools given. Missing loaders have no cached resources in these pools.

Previous topic

util—utilities for graphics

Next topic

entity—things that exist in the world

This Page