Corner missing on rectangle

Viewed 230

While working on my game, I noticed that when rendering rectangles, sometimes the bottom-right corners are just cut off by one pixel.

Notice the missing pixels on the gray boxes.

I have a Box class that updates every frame:

class Box:
    def __init__(self, x, y, width, height):
        self.rectbox = pyg.Rect(x, y, width, height)
    def update(self):
        pyg.draw.rect(screen, light_gray, self.rectbox) # Main box
        pyg.draw.rect(screen, black, self.rectbox, 2) # Box outline
    def getRect(self):
        return self.rectbox

Is this my mistake or just how pygame renders rectangles?

1 Answers

It seems to be just when drawing rectangles with an outline. A simple fix is to draw a solid rectangle black and a slightly smaller rectangle light_gray on top of it

class Box:
    def __init__(self, x, y, width, height, w):
        self.rectbox_outline = pyg.Rect(x, y, width, height) 
        self.rectbox_main = pyg.Rect(x+w//2,y+w//2,width-w,height-w)
    def update(self):
        pyg.draw.rect(screen, black, self.rectbox_outline) # Box outline
        pyg.draw.rect(screen, light_gray, self.rectbox_main) # Main box
    def getRect(self):
        return self.rectbox
Related