简体   繁体   English

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

[英]global variable not updating (Processing.py)

I'm using processing.py to make an application for drawing simple lines this is my code:我正在使用 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])

at one point I want to clear my "points" array but it's not updating有一次我想清除我的“点”数组,但它没有更新

what is causing this?这是什么原因造成的?

Problem is in different place because you use the same name points in function draw() in loop for points so it assigns last element from pointList to points问题出在不同的地方,因为您在循环中的函数draw()中使用了相同的名称points for points因此它将pointList最后一个元素分配给points

You have to use different name in draw() - ie.您必须在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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM