lordmauve / pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
https://pygame-zero.readthedocs.io/
GNU Lesser General Public License v3.0
520 stars 189 forks source link

Make it possible to inject additional image into image loader cache #254

Open lordmauve opened 3 years ago

lordmauve commented 3 years ago

Originally created by @jhutar in #232

I have images dirt.png and bush.png and with this I can create temporary image on the fly when starting the game (so my actor do not need to draw two overlapping images when running the game):

import pygame.image
img_dirt = pygame.image.load('images/dirt.png').convert_alpha()
img_bush = pygame.image.load('images/bush_small.png').convert_alpha()
img_dirt.blit(img_bush, (0,0))
images.add('tmp_dirt_with_bush', img_dirt)
my_actor.image = 'tmp_dirt_with_bush'

Please let me know if it makes sense.

lordmauve commented 3 years ago

I see why you'd want to do this, but I'm not quite happy with the API. I think add() feels wrong; it should be more like

images["tmp_dirt_with_bush"] = img_dirt

with the implication that you can also read it back as images["tmp_dirt_with_bush"] or unload it with del images["tmp_dirt_with_bush"].

lordmauve commented 3 years ago

I think either ResourceLoader needs to duck-type as a dict of loaded resources, or the images object should be a wrapper around ImageLoader that presents a dict-like API.