jdf / Processing.py-Bugs

A home for all bugs and feature requests about Python Mode for the Processing Development Environment.
41 stars 8 forks source link

problem with lists (append and popleft) #365

Closed Negar-Amiri closed 1 year ago

Negar-Amiri commented 1 year ago

I wrote a little program about a bouncer ball with an effortless physics engine and want to show the path that ball passed. so I decide to store the position of the ball in a list as a queue and when the list goes full, pop one of them from the left. but it seems to append() replaces all the list elements with the last position, instead of appending them. and also popleft() doesn't work. and also if anyone has a better idea for drawing passed paths, please let me know it.

pos = PVector(250,250)
vel = PVector(0,0)
acc = PVector(0,0)
posHistory = []

def setup():
    global pos
    global vel
    global acc
    global posHistory
    frameRate(50)
    size(500 , 500)

def draw():
    global pos
    global vel
    global acc
    global posHistory
    acc = PVector(mouseX, mouseY)
    background(0)
    fill(255)
    circle(pos.x, pos.y, 30)
    posHistory.append(pos)
    c = 0
    for p in posHistory:
        c+=1
    if c > 300:
        print(posHistory)
        posHistory.popleft()
    acc.sub(pos)
    acc.setMag(0.05)
    vel.add(acc)
    pos.add(vel)
villares commented 1 year ago

Cheers @Negar-Amiri!

I couldn't have a careful look at your code right now, but I'm afraid Python's lists do not have a popleft() method.

You could use .pop(0) which can be slow/inefficient for large collections or create a deque data structure that does have a .popleft() and is perfect for these "history" things! https://github.com/jdf/processing.py/blob/master/mode/examples/Basics/Input/StoringInput/StoringInput.pyde

Please do open a thread at https://discourse.processing.org/c/processing-py/9 which is best place for us to discuss these things (here is for Processing.py bugs only, you might want to close this).

Negar-Amiri commented 1 year ago

hello, and thank you so much for your answer I find popleft() here in python docs 5.1.2. Using Lists as Queues but anyway pop(0) works as well, thanks a lot.