Home > OS >  pygame.Surface turning into NoneType
pygame.Surface turning into NoneType

Time:12-09

I have a python file called engine.py. I have one specific function that I will put part of, due to knowing the function works fine:

def set_image(self,path : str,spritesheet=None) -> pygame.Surface:
        if (path is not None) and (spritesheet is None): # if path is provided but spritesheet is not
            try:
                self.image = pygame.image.load(path)
                print(type(self.image))
                self.rect = self.image.get_rect()
            except FileNotFoundError as e:
                raise e

But when I try to blit() the image result of the function, I get a TypeError stating that pygame.Surface must be the first argument, not None. I did some testing and found out that the function from engine.py works just fine, and that it does properly set self.image. But the program does not have its self.image to the pygame.Surface object.

Here's where the player object is declared and player.image is set:

player = Engine.Player()

player.image = player.set_image('screensaver/blue_car.png',)

And here's where I try to blit() it onto my display surface (The line where the error pops up):

display.blit(player.image, (0,0))

CodePudding user response:

set_image is a method. The method does not return an image, but it sets the image and rect attributes of the object. Therefore:

player.image = player.set_image('screensaver/blue_car.png',)

player.set_image('screensaver/blue_car.png')
  • Related