0

I am developing a game in pygame and everything is working. But I thought of something and wasn't sure if it mattered.

My game makes use of projectiles using the sprite class and currently code looks like this

class Enemy_Attack_Sprite(pygame.sprite.Sprite):
    def __init__(self, image, w=200, h=20):
        pygame.sprite.Sprite.__init__(self)
        image_load = pygame.image.load(image)
        self.image = pygame.transform.smoothscale(image_load,(w,h))

With 'Image' being the file name as string. So I would call

projectile = Enemy_Attack_Sprite('projectile.png')

Does that mean every single time a shot is fired the programe is loading the .png again? would it be more efficient or make a significant difference to do it like this:

projectile_image = pygame.image.load('projectile.png')
class Enemy_Attack_Sprite(pygame.sprite.Sprite):
    def __init__(self, image, w=200, h=20):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.smoothscale(image,(w,h))

and then call it with:

projectile = Enemy_Attack_Sprite(projectile_image)

Everything is working okay now, But I'm wondering if re-writing the images and sprite functions in my game would make a difference in performance?

Strayer
  • 31
  • 4
  • 1
    This is more suited for code review and not stack overflow. – Austin A Mar 27 '18 at 18:09
  • 1
    Yes, `pygame.image.load` will load the image from the hard disk again and again which is inefficient. Also, convert your images: https://stackoverflow.com/a/48574695/6220679 – skrx Mar 27 '18 at 19:46

0 Answers0