Python/处理如何在椭圆距离内鼠标点击并改变颜色

2024-05-03 13:30:18 发布

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

尝试将每个圆从一种颜色更改为另一种颜色(在圆距离内单击时)。这个代码的问题是所有的圆都变为相同的颜色。当鼠标点击一个圆时,只想改变它。可能需要使用dist()函数,但是不知道如何实现它。你知道吗

x = 100
y = 50
circle_x = 50
circle_y = 50
radius= 25
value = 255

def setup ():
    size(400, 400)

def draw (): 
    fill (value, value, 0)
    ellipse (100, y, circle_x, circle_y)
    ellipse (200, y, circle_x, circle_y)
    ellipse (300, y, circle_x, circle_y)

def inside_circle(x, y, circle_x, circle_y, radius):
    if ((x-circle_x)*(x-circle_x) + (y-circle_y)*(y-circle_y)) <= radius*radius:
        return True 
    else:
        return False

def mouseClicked ():
    global x, y, value
    if inside_circle (50, 50, 50, 50, radius):
        fill (0)
    if (value == 255): 
        value = 0

Tags: 函数代码距离returnifvalue颜色dist