矩形上缺少角点

2024-10-03 19:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的游戏中,我注意到在渲染矩形时,有时右下角只被切掉一个像素

Notice the missing pixels on the gray boxes.

我有一个Box类,它会更新每一帧:

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

这是我的错误还是pygame渲染矩形的方式


Tags: therectselfbox游戏def像素width
1条回答
网友
1楼 · 发布于 2024-10-03 19:22:45

这似乎只是在绘制带有轮廓的矩形时。 一个简单的解决方法是在上面画一个黑色实心矩形和一个稍小的浅灰色矩形

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

相关问题 更多 >