-1

I created a class Cartel, which inherits from pygame.surface.Surface, to display images

class Cartel(pygame.surface.Surface):
    def __init__(self,width, height,imagen):
        super().__init__((width, height))
        self.imagen=pygame.image.load(imagen).convert_alpha()
        pantalla.blit(self,(200,180))

Although when I try using the Cartel class the image appears in black

while True:
    fon2=Cartel(300,300,"Images\circulo.png")

But this doesn't happen when I blit an image normally, like this:

francia=pygame.image.load("Images\Francia.png").convert_alpha()
francia=pygame.transform.scale(francia, (150, 40))
francia_rect=francia.get_rect(center=(70,60))

while True:
    pantalla.blit(francia,francia_rect)

I'd like to know how to blit correclty an image from my class. Thanks!

ShadowCrafter_01
  • 533
  • 4
  • 19

1 Answers1

-1

The way your approach looks I don't think your class needs to inherit pygame.surface.Surface. It would be easyer to just store the screen and image object in your class. Then you could have a blit method that draws the image to the screen. You just would have to call that method in your game look for the image to display. This would also get rid of the need to reinstantiate the class every tick.

Here is an example of the class

class Cartel:
    def __init__(self, image, screen):
        self.image = pygame.image.load(image).convert_alpha()
        self.screen = screen

    def blit(self):
        self.screen.blit(self.image, (200, 280))

And your game loop could look something like this

cartel = Cartel("image.jpg", screen)
while True:
    cartel.blit()
    pygame.display.flip()
ShadowCrafter_01
  • 533
  • 4
  • 19