Zaed
Zaed

Reputation: 19

How do I change a sprite from a rectangle to an image?

So I copied some code from the internet (http://programarcadegames.com/python_examples/f.php?file=platform_moving.py) just to experiment with pygame...

I've tried replacing the self.image.fill(BLUE) with self.rect = pygame.image.load("TheArrow.png")

Here's a little snippet of my code..

 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.
        width = 40
        height = 60
        self.image = pygame.Surface([width, height])
        self.image.fill(BLUE)
        self.rect = pygame.image.load("TheArrow.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

Here's the original code...

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.
        width = 40
        height = 60
        self.image = pygame.Surface([width, height])
        self.image.fill(RED)

        # 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

I want the image TheArrow.png to show up instead of a rectangle....

Upvotes: 1

Views: 1211

Answers (1)

Valentino
Valentino

Reputation: 7361

Rect object are not meant to store images. pygame.image.load() returns a Surface with the image. It can be used directly or blitted on another Surface.

 def __init__(self):
    """ Constructor function """

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

    width = 40
    height = 60
    self.image = pygame.image.load("TheArrow.png") #use the image Surface directly
    self.rect = self.image.get_rect()
    #the rest as in the original code

or:

 def __init__(self):
    """ Constructor function """

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

    width = 40
    height = 60
    myimage = pygame.image.load("TheArrow.png")
    self.image = pygame.Surface([width, height])
    self.image.blit(myimage) #blit the image on an existing surface
    self.rect = self.image.get_rect()
    #the rest as in the original code

In the former case, the size of the Surface (its associated rect, which you can get with self.image.get_rect() is the same of the loaded image file.
In the latter, you set the size with [with, height]. If these does not correspond to the image size, the image will be cut (if bigger).

By the way, blitting a Surface on another Surface is what you do display the Surface on the screen. In pygame the screen is just another Surface, a bit special.

Have a look at the intro tutorial for more info.

Upvotes: 2

Related Questions