Processing example - Autumn

General posts about art, interactivity, programming - Processing, OpenFrameworks, Arduino, Pure Data, Max/MSP, etc

Processing example - Autumn

Postby BenChang » Tue Nov 05, 2013 11:53 pm

Screen shot 2013-11-05 at 6.49.27 PM.png


Code: Select all


// the leaf class
// draws a leaf
// wind motion
// fall down to the ground
// maybe color change

class Leaf
{
  float x;
  float y;
  float r;
  float g;
  float b;

  // define states:
  // 1=on the tree
  // 2=falling
  // 3=on the ground
 
  static final int ONTREE=1;
  static final int FALLING=2;
  static final int ONGROUND=3;

  int state;
 
  Leaf (float x, float y)
  {
    this.x=x;
    this.y=y; 
   
    r=48;
    g=random(180,225);
    b=79;
   
    state=ONTREE;  // start off on the tree
  }
 
  // update and draw the leaf
  void draw()
  {
   fill(r,g,b);
   noStroke();
   
   if (state==ONTREE)
   {
     float wiggleX = random (-3,3);
     float wiggleY = random (-3,3);
     
     rect(x + wiggleX,y + wiggleY,10,10);
     
     if (random(0,10000)>9998)
     {
        state=FALLING;
        r=random(128,255);
        g=random(0,180);
     }
   }
   else if(state==FALLING)
   {
     y=y+1;
     float wiggleX = random (-3,3);
   
      x=x+wiggleX;
     rect(x,y,10,10);
     
     if (y>590)
     {
        state=ONGROUND;
     }
   }
   else if (state==ONGROUND)
   {
     rect(x,y,10,10);
   }
  }
}

// declare variables for the leaves

Leaf[] leaves = new Leaf[200];

void setup()
{
    size(600,600);
    // initialize the leaves   
   
    for (int i=0;i<200;i++)
    {
       float angle = random(0,TWO_PI);
       float distance = random(0,50);
       
       float x= cos(angle) * distance + 300;
       float y= sin(angle) * distance + 150;
       
       leaves[i] = new Leaf (x,y);
    }
}

void draw ()
{
    background(60,146,229);
    // update leaf positions, draw them
   
    for (int i=0;i<200;i++)
    {
       leaves[i].draw();
    }
}
User avatar
BenChang
 
Posts: 176
Joined: Mon Jan 17, 2011 4:48 pm

Return to Interactive Art && Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron