Is it a problem to use pygame.image.load() multiple times?

Asked

Viewed 65 times

1

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

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

That way whenever I create a Button it will read the same image again or will know that you have loaded this image before and will simply pull it from a cache, I don’t know?

  • elaborate more your question, maybe putting a piece of code can help the community more and give you a more accurate answer

1 answer

1

Yes - there’s 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 incorporates no type logic: it will read the disk file and uncompress it each time it is executed.

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

  • This last paragraph all applies not only to pygame and Engines game, but any application that is somewhat image intensive and does not want to have "hiccups" performance

Browser other questions tagged

You are not signed in. Login or sign up in order to post.