Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I blit a rectangular outline to a surface?

I have an obstruction tile/sprite and I want to draw a rectangular outline (32×32 or from its image rect) directly to its image surface. Ideally the colors and outline/border width will also be taken into consideration.

class Obstruction(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load("assets/tile.png").convert_alpha()
        if s.DEBUG:
            outline = pygame.Surface((32, 32))
            self.image.blit(outline, (0, 0))

Few things I’ve tried:

  1. The code above generates a filled rectangle rather than an outline.
  2. The prominent answer I found online is with the use of pygame.draw.rect() but this does not blit directly to the image.
  3. I’ve tried creating rect1 and rect2 and did subtraction between their areas using https://www.pygame.org/wiki/SubtractRects, however the result is a Rect object which can’t be blit into a Surface object.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Draw a black rectangular outline on the image with pygame.draw.rect. The las parameter of pygame.draw.rect is optional and is the with of the line:

class Obstruction(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load("assets/tile.png").convert_alpha()
        if s.DEBUG:
            pygame.draw.rect(self.image, "black", (0, 0, 32, 32), 1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading