Is there any problem in using pygame.image.load () several times?

1

I am creating a class called Button , and one of the properties of this class is an image.

I wanted to know if there is a problem, if within this class I used self.imagem = pygame.image.load() to not have to pass the image as an argument, since in this application I will only have a button image.

So whenever I create a Button it will read the same image again or will know that it has already loaded this image before and will simply pull it out of a cache, I do not know?

    
asked by anonymous 01.12.2017 / 14:46

1 answer

1

Yes - there is a problem.

Unlike the Python module import system, which uses a very complicated mechanic to read a disk file once, no matter how many times you do import , pygame.image.load does not incorporate any logic type: it will read the file from the disk and unzip it every time it is run.

You can have a startup function that reads all your images at the beginning of the program (or all the images of the current scene in the case of a large project), and store them in a dictionary. Then you retrieve the images from the dictionary instead of using the load every time.

    
22.03.2018 / 17:41