我正在尝试在程序中实现绘图框,例如当您按住鼠标按钮时,当您移动鼠标时会出现一个矩形。试图用一个 pygame rect 对象来做这件事,这是我迄今为止想出的:
def mouseDown(self, button, pos):
if button == 1:
self.pressing = True
self.start = pos
def mouseUp(self, button, pos):
if button == 1:
self.pressing = False
def mouseMotion(self, buttons, pos):
if self.pressing == True:
width = abs(self.start[0] - pos[0])
height = abs(self.start[1] - pos[1])
self.box = pygame.Rect(self.start, width, height)
pygame.draw.rect(self.screen, (0,0,0), self.box, 1)
所以 pos 是点击的坐标,(0,0) 是左上角。我尝试使用 abs 通过比较鼠标移动的距离来获取大小,但 abs 只返回正值,因此它不起作用。
我们如何改变它以使框选择成为可能?