GMU:Immersive Strategien/Inhalte/Bälle/Code

From Medien Wiki

Code

Ball[] ball = new Ball[15];
int a,b,c;

void setup() {
  size(1280,1024);
  smooth();
  noStroke();
  //frameRate(5);
  colorMode(HSB,360,100,100,100);
  
  a = 500;
  b = 400;
  c = 40;
  
  for (int i=0; i < 5; i++) {
    ball[i] = new Ball();
    
    ball[i].x = random(ball[i].g,a-ball[i].g);
    ball[i].y = random(ball[i].g,height-ball[i].g);
    
    ball[i].h = (int)random(360);
    
    ball[i].vx = random(1,10);
    ball[i].vy = random(1,10);
  }
  
  for (int i=5; i < 10; i++) {
    ball[i] = new Ball();
    
    ball[i].x = random(a+c+ball[i].g,a+c+b-ball[i].g);
    ball[i].y = random(ball[i].g,height-ball[i].g);
    
    ball[i].h = (int)random(360);
    
    ball[i].vx = random(1,10);
    ball[i].vy = random(1,10);
  }
  
  for (int i=10; i < ball.length; i++) {
    ball[i] = new Ball();
    
    ball[i].x = random(a+c+b+c+ball[i].g,width-ball[i].g);
    ball[i].y = random(ball[i].g,height-ball[i].g);
    
    ball[i].h = (int)random(360);
    
    ball[i].vx = random(1,10);
    ball[i].vy = random(1,10);
  }
}

void draw() {
  background(0);
  fill(255);
  rect(a,0,c,height);
  fill(255);
  rect(a+c+b,0,c,height);
  
  for (int i=0; i < 5; i++) {
    ball[i].maleBall();
    
    ball[i].x = ball[i].x + ball[i].vx;
    ball[i].y = ball[i].y + ball[i].vy;
    
    if (ball[i].x <= 0+ball[i].g/2 || ball[i].x >= a-ball[i].g/2) {
      ball[i].vx = -ball[i].vx;
    }
    if (ball[i].y <= 0-ball[i].g) {
      ball[i].y = height;
    }
    if (ball[i].y >= height+ball[i].g) {
      ball[i].y = 0-ball[i].g;
    }
  }
  
  for (int i=5; i < 10; i++) {
    ball[i].maleBall();
    
    ball[i].x = ball[i].x + ball[i].vx;
    ball[i].y = ball[i].y + ball[i].vy;
    
    if (ball[i].x <= a+c+ball[i].g/2 || ball[i].x >= a+c+b-ball[i].g/2) {
      ball[i].vx = -ball[i].vx;
    }
    if (ball[i].y <= 0-ball[i].g) {
      ball[i].y = height;
    }
    if (ball[i].y >= height+ball[i].g) {
      ball[i].y = 0-ball[i].g;
    }
  }
  
  for (int i=10; i < ball.length; i++) {
    ball[i].maleBall();
    
    ball[i].x = ball[i].x + ball[i].vx;
    ball[i].y = ball[i].y + ball[i].vy;
    
    if (ball[i].x <= a+c+b+c+ball[i].g/2 || ball[i].x >= width-ball[i].g/2) {
      ball[i].vx = -ball[i].vx;
    }
    if (ball[i].y <= 0-ball[i].g) {
      ball[i].y = height;
    }
    if (ball[i].y >= height+ball[i].g) {
      ball[i].y = 0-ball[i].g;
    }
  }
}

Objekt Ball

class Ball {
  
  int g; //Größe
  float x,y; //Position
  
  int h,s,b,a; //Farbe
  
  float vx,vy; //Geschwindigkeit
  
  Ball() {
    g = 100;
    
    s = 100;
    b = 100; 
  }
  
  void maleBall() {
    fill(h,s,b);
    ellipse(x,y,g,g);
  }
}