Python object iteration problem

edited September 2017 in Python Mode

Hi, I am using python-mode to follow Particle System on NOC, the iterator issue couldn't be solved by my own. Apparently, in the particle system object, every operation were made on single object Particle. I've attached my code, please help.

`````

def setup():
    global ps
    size(800,300)
    randomSeed(1)
    ps = ParticleSystem(PVector(width/2,height/3))
    background(255)
    smooth()
    frameRate(60)


def draw():
    global ps
    background(255)
    ps.addParticle()
    ps.run()

class Particle(object):

    def __init__(self,location):
        self.location  = location 
        self.vel = PVector(random(-1,1),random(-1,1))
        self.acc = PVector (0,0.05)
        self.lifespan = 255

    def run(self):
        self.update()
        self.display()

    def update(self):
        self.location.add(self.vel)
        self.vel.add(self.acc)
        self.lifespan -= 2.0

    def display(self):
        stroke(0,self.lifespan)
        strokeWeight(2)
        fill(255,255,0,self.lifespan)
        ellipse(self.location.x,self.location.y,12,12)

    def isDead(self):
        if (self.lifespan<=0.0):            
            return True
        else:
            return False



from Particle import *

class ParticleSystem(object):

    def __init__(self,origin):
        self.origin=origin 
        self.particles = [Particle(self.origin) for i in range(10)]  ###****where i think the problem is ****

    def run(self):    
        for particle in self.particles:
            particle.run()                            ###****where i think the problem is, every opreation is made on single object****
            if particle.isDead() == True:
                self.particles.remove(particle)
        # for  i in range(len(self.particles)):
        #     print i,"---",self.particles[i].location

    def addParticle(self):
        p = Particle(self.origin)
        self.particles.append(p)  

`````

Answers

  • Could you fix the style of the code?

  • edited September 2017 Answer ✓

    There are 2 bugs in your class ParticleSystem: :-SS

    1st, in its method addParticle(), you're passing the very same PVector instance for all created instances of class Particle! @-)
    Instead, you should pass a clone of it when instantiating a Particle via PVector::get() method.
    Or alternatively, modify class Particle to clone the passed PVector argument by itself.

    2nd, you're invoking method remove() on your MutableSequence particles[] while iterating over it.
    B/c it decreases the len() of the MutableSequence, it ends up breaking the whole loop's range()! :-S
    There are some workarounds for it, but my fav is iterating a MutableSequence backwards. *-:)
    This way, the end of a full iteration is always when current index is 0, rather than its initial len(). :ar!

    Below are my suggestions to fix your class ParticleSystem: :-bd

    class ParticleSystem:
        def __init__(ps, origin, initialQuantity=10):
            ps.origin = origin
            ps.particles = []
    
            for i in range(initialQuantity): ps.addParticle()
    
    
        def __str__(ps, INFO='Origin: %s  -  Particles: %d'):
            return INFO % (ps.origin, len(ps.particles))
    
    
        def addParticle(ps):
            ps.particles.append(Particle(ps.origin.get()))
            return ps
    
    
        def run(ps):
            parts = ps.particles
            idx = len(parts)
    
            while idx:
                idx -= 1
                p = parts[idx]
                p.run()
    
                if p.isDead():
                    tail = parts.pop()
                    if idx != len(parts): parts[idx] = tail
    
            return ps
    
Sign In or Register to comment.