Wednesday, February 8, 2017

BOLITA CLASS ARRAY

Bolita [] miBol;
int cant;
void setup() {
  size(1080,720);
  cant = 20;
  miBol = new Bolita[cant];
  for (int i=0; i<cant; i+=1){
    miBol [i] = new Bolita (random(width), random(height), random(10), random(35),1,1,color (random(120), random(80), random(255)));
  }
}
void draw() {
  background(0);
  for (int i=0; i<cant; i+=1){
    miBol[i].move();
    miBol[i].display();
  }
}
class Bolita {
  float posX, posY, speed, tam;
  int dirX, dirY;
  color col;

  Bolita (float tposX, float tposY, float tspeed, float ttam, int tdirX, int tdirY, color tcol) {
    posX=tposX;
    posY=tposY;
    speed=tspeed;
    tam=ttam;
    dirX=tdirX;
    dirY=tdirY;
    col = tcol;
  }
  void move() {
    posX += speed*dirX;
    posY += speed*dirY;
    if (posX>width-(tam/2)||posX<tam/2) {
      dirX=-dirX;
    }
    if (posY>height-(tam/2)||posY<tam/2) {
      dirY=-dirY;
    }
  }
  void display() {
    fill (col);
    ellipse(posX, posY, tam, tam);
  }
}

No comments:

Post a Comment