jueves, 25 de agosto de 2016

processing: efécto básico matrix

Una de las ventajas de usar processing es que solamente te enfocas a realizar animaciones, efectos graficos y pueden ser programados de una manera muy sencilla, por ejemplo realizar una animación que trata de emular el código de la matrix, el cual no lleva mas que unos minutos si uno tiene las bases de pintar figuras y algo de programacion orientada a objetos





Código fuente:
m[] m1=new m[101];
void setup() {
  size(800, 600);
  fill(color(0, 255, 0));
  noStroke();
  for (int i=0; i<=100; i++) {
    m1[i]=new m(random(0, width), random(0, height), random(1, 5));
  }
}
void draw() {
  background(0);
  for (int i=0; i<=100; i++) {
    m1[i].mover();
    m1[i].display();
  }
}
class m {
  float x=0, y=0, v=0, t=1, r=0;
  int i, c;
  int[] cc=new int[61];
  m(float xx, float yy, float vv) {
    x=xx;
    y=yy;
    v=vv;
    t=random(1, 60);
    for (i=0; i<=t; i++) {
      cc[i]=(int)random(0, 150);
    }
  }
  void mover() {
    y=y+v;
    if (y>(height+t*5)) {
      y=0;
      v=random(2, 11);
      x=random(0, width);
      t=random(1, 60);
      for (i=0; i<=t; i++) {
        cc[i]=(int)random(0, 150);
      }
    }
  }
  void display() {
    for (i=0; i<=t; i++) {
      fill(0, cc[i], 0);
      rect(x, y-5*i, 5, 5);
    }
    c=(int)random(0, 255);
    fill(0, c, 0);
    rect(x, y, 5, 5);
  }
}