全局变量未更新 (Processing.py)

global variable not updating (Processing.py)

我正在使用 processing.py 制作绘制简单线条的应用程序 这是我的代码:

pointsList =[ ]
points = []


def setup():
    global pointsList, points
    size(400,400)
    stroke(255)
    strokeWeight(5)

def draw():
    global pointsList, points
    background(0)
    for points in pointsList:
        draw_points(points)
    draw_points(points)


def keyPressed():
    global pointsList, points
    if key == 'e':
        try:
            pointsList.append(points)
            points = [] #<--- this right here not updating
        except Exception as e:
            print(e)
        print(pointsList)

def mouseClicked():
    global points
    print(points)
    points.append((mouseX,mouseY))


def draw_points(points):
    for i in range(len(points)-1):
        draw_line(points[i],points[i+1])

def draw_line(p1,p2):
    line(p1[0],p1[1],p2[0],p2[1])

有一次我想清除我的 "points" 数组 但它没有更新

这是什么原因造成的?

问题出在不同的地方,因为您在循环 draw() 中的函数 points 中使用了相同的名称 points,因此它会将 pointList 中的最后一个元素分配给 points

您必须在 draw() 中使用不同的名称 - 即。 items

def draw():
    global pointsList, points

    background(0)

    for items in pointsList:  # <-- use `items`
        draw_points(items)    # <-- use `items`

    draw_points(points)

pointsList = []
points = []

def setup():
    global pointsList, points

    size(400,400)
    stroke(255)
    strokeWeight(5)

def draw():
    global pointsList, points

    background(0)

    for items in pointsList:  # <-- use `items`
        draw_points(items)    # <-- use `items`

    draw_points(points)


def keyPressed():
    global pointsList, points

    if key == 'e':
        try:
            pointsList.append(points)
            points = []
        except Exception as e:
            print(e)

        print(pointsList)

def mouseClicked():
    global points

    points.append((mouseX,mouseY))
    print(points)

def draw_points(points):
    for i in range(len(points)-1):
        draw_line(points[i], points[i+1])

def draw_line(p1, p2):
    line(p1[0], p1[1], p2[0], p2[1])