0

I am making a game on Pygame with a scrolling background made of tiles. So I made the following code to render them:

def printAllTiles(map, x_scroll, y_scroll):
    for i, l in enumerate(map): #map is a double entry table
        for j, t in enumerate(l):
            if t != None and t != 0:
                window.blit(Img.loadImage(Img.findImage(t + ".png", "Tiles")), (j*tile_size+x_scroll, i*tile_size+y_scroll))

Later on, I wanted to be able to resize the display window and all the images in it. This included the tiles. So I modified my code as shown below. Here, the Img.resizeImage function does quite the same as pygame.transform.scale, but with a growth ratio as argument.

def printAllTiles(map, x_scroll, y_scroll):
    for i, l in enumerate(map):
        for j, t in enumerate(l):
            if t != None and t != 0:
                current_tile = Img.loadImage(Img.findImage(t + ".png", "Tiles"))
                window.blit(Img.resizeImage(current_tile, screen_resize_ratio[0]), (j*tile_size+x_scroll, i*tile_size+y_scroll))

This new code raises a pygame.error: Out of memory. In other Stack Overflow questions concerning pygame.transform and pygame memory, I couldn't find an explanation on how does pygame.transform use more memory.

Lolrenz Omega
  • 163
  • 1
  • 7
  • 3
    You shouldn't load the tiles every time you blit them. Is that what Img.loadImage does? You should load each image you use once and keep a reference to it, so you can blit it later. This will use much less memory and be much faster. – Starbuck5 Mar 27 '22 at 03:57
  • 3
    It's not that probably not that "pygame.transform" is using more memory, it's that you're using that function to create larger surfaces, which is pushing you over the top of memory usage, especially since you seem to load the same image over and over. – Starbuck5 Mar 27 '22 at 04:00
  • 2
    you show load images only once, and you should resize them only once. At this moment you waste time and memory. – furas Mar 27 '22 at 04:52
  • I changed my code so it only reloads the images if their size or the map is changed. It works! Thank you! – Lolrenz Omega Mar 28 '22 at 01:32

0 Answers0