How to make a border for a Rect in Pygame?

Viewed 1186

So in my game, I have a normal rectangle made by rect.

So how can I give this a border of some other color?

3 Answers

You should use width keyword argument:

(x, y, width, height) = (300, 300, 50, 50)
border_width = 5
pg.draw.rect(window, pg.Color("Red"), (x, y, width, height))
pg.draw.rect(window, pg.Color("Green"), (x, y, width, height), width=border_width)

You should post what code you have so far, like what you have tried

You could draw a small rectangle on top of a bigger rectangle

border_col = (0, 0, 0)
rect_col = (255, 255, 0)

x = 100
y = 100
w = 100
h = 100
border = 3

#draw border first
pygame.draw.rect(screen, border_col, (x, y, w, h))
#then inside
pygame.draw.rect(screen, rect_col, (x + border, y + border, w - border*2, h - border*2))

I would draw 4 lines on each side using pygame.draw.lines():

# draw a rectangle with a border
fill_col, border_col = (100, 255, 100), (100, 100, 255)
x, y = 50, 50
rect_width, rect_height = 200, 100 

# draw the rectangle
pygame.draw.rect(surface, fill_col, (x, y, x+rect_width, y+rect_height))
# draw the border
border_coords = ((x, y), (x+rect_width, y), (x+rect_width, y+rect_height), (x, y+rect_height))
border_thickness = 3
pygame.draw.lines(surface, border_col, True, (border_coords), border_thickness)

A link to the docs: https://www.pygame.org/docs/ref/draw.html#pygame.draw.lines

Related