• fullscreen
  • RANDOM_WALK_1.pde
  • class_Parti.pde
  • //---------------------LIBRARIES
    
    //---------------------GLOBAL VARIABLES
    int particlesSize = 1000;
    int particlesSize2 = 100;
    color c = color(3,180,255,200); //initil particles (blue)
    color c2 = color(255,150,20,200); // seondary particles (orange)
    
    //---------------------DECLARE OBJECT
    ArrayList particles; 
    
    //---------------------SETUP LOOP
    void setup(){
    size (1000,400);
    smooth();
    frameRate(30);
    background (0);
    
    //-------------------INITIALIZE OBJECT
    particles = new ArrayList (); //start with empty list
    for (int i = 0; i< particlesSize; i ++) {
      Parti myParti = new Parti(random(0,width),random(0,height),c);
      particles.add(myParti);
      }
    }
    
    //---------------------DRAW LOOP
    void draw(){
      fill (0,10); // particles fade out
      rect (0,0,width,height);
      for (int i = 0; i < particles.size(); i ++) {
        Parti p = (Parti) particles.get(i);
        p.run();
      }
      if(mousePressed){// add secondary particles
        for (int i = 0; i< particlesSize2; i ++) {
        particles.add(new Parti(random(0,width),random(0,height),c2));
      
      }
    }
    }
    
    //---------------------ADDITIONAL FUNCTION
    void keyPressed(){
      saveFrame();
    }
    
    class Parti {
      
    //VARIABLES
      float x;
      float y;
      float xspeed = random(1,3);
      float yspeed = random(1,3);
      color particlec;
      float r = 8;
      
    //CONSTRUCTOR
    
    Parti(float _x, float _y, color _particlec){
      x = _x;
      y = _y;
      particlec = _particlec;
    }
    
    //Random Walk
    void move() {
      int xstep = int(random(0,3))-1; //random walk for x
      int ystep = int (random(0,3))-1; //random walk for y
      x += xstep*xspeed; // acceleration
      y += ystep*yspeed;
      
    //bounce back
      if ((x > width) || (x < 0)) {
        xspeed = xspeed * -1;
      } 
      if ((y > height) || (y < 0)) {
        yspeed = yspeed * -1;
      }
    }
    
    //draw particles
    void display(){
      noStroke();
      fill(particlec);
      ellipse (x,y, r, r);
    }
    
    //run all function
    void run(){
      display();
      move();
    }
    }
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Sherman Lam

    RANDOM_WALK_1

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    mouse click to add orange particles

    You need to login/register to comment.