Pygame NameError: name 'image' is not defined

I'm modifying a program I found online and I am trying to load a .png instead of the previous block sprite, but I think I am coding it incorrectly when trying to load it. Here is the code:

class Player(pygame.sprite.Sprite):
    """
    This class represents the bar at the bottom that the player controls.
    """

    # -- Methods
    def __init__(self):
        """ Constructor function """

        # Call the parent's constructor
        super().__init__()

        # Create an image of the block, and fill it with a color.
        # This could also be an image loaded from the disk.
        image.self.pygame ("Sprite-01.png")

        # Set a referance to the image rect.
        self.rect = self.image.get_rect()

        # Set speed vector of player
        self.change_x = 0
        self.change_y = 0

        # List of sprites we can bump against
        self.level = None

And this is the Error:

Traceback (most recent call last):   

File "C:/Users/1234/AppData/Local/Programs/Python/Python36-32/class 10 program St.Patrick.py", line 452, in <module>
    main()   
File "C:/Users/1234/AppData/Local/Programs/Python/Python36-32/class 10 program St.Patrick.py", line 360, in main
    player = Player()   
File "C:/Users/1234/AppData/Local/Programs/Python/Python36-32/class 10 program St.Patrick.py", line 51, in __init__
    image.self.pygame ("Sprite-01.png") 
NameError: name 'image' is not defined

1 answer

  • answered 2018-01-13 17:21 skrx

    image.self.pygame ("Sprite-01.png")
    

    Change that line to:

    self.image = pygame.image.load("Sprite-01.png").convert_alpha()
    

    or just .convert() if the image has no transparent parts.

    I also recommend loading the images in the global scope or another module before the program starts and then just reuse them instead of loading them again. Reading from the hard disk is slow.