[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Best way to access data
On 11 Jul 2006 at 10:56, spotter . wrote:
> Hi everybody,
>
> I had a question as to which way would be better to access data after
> loading it into memory.
>
> The code looks like this now:
> In the main loop file, I cache the images into dictionaries:
> images_list = ["enemy.png","player.png"]
> global cache
> cache = cacheimages.cache_images(images_list)
>
> then in the enemy or player files, I access the images like this:
> import play
> self.rect = play.cache["enemy.png"].get_rect() # I refer to
> the file play and read cache from there
>
> Is it better to read from another file like that? Would there be
> another way to do it that might be better?
>
> Thanks,
> spot.
>
Leave out the unnecessary cache and assign directly to global
variables.
You can do something like this:
# play module
import os.path
images_list = ["enemy.png","player.png"]
# Load <name>.png files as <name>_image global surfaces
for file_name in image_list:
globals()["%s_image" % os.path.splitext(file_name)[0]] = \
load_image(file_name)
# Some other module
import play
self.rect = play.enemy_image.get_rect() # enemy.png image
Lenard Lindstrom
<len-l@xxxxxxxxx>