Advertisement
xeromino

Rectangular snow globes

Oct 15th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. int num = 2000;
  2. int edge= 50;
  3. Ball[] balls = new Ball[num];
  4. color bg = #ffffff; //background + balls
  5. color f = #2F3440; // fill rect
  6.  
  7. void setup() {
  8.   size(500, 400);
  9.   for (int i=0; i < num; i++) {
  10.     balls[i]= new Ball();
  11.   }
  12. }
  13.  
  14. void draw() {
  15.   background(bg);
  16.   fill(f);
  17.   rect(edge, 50, width-2*edge, 100, 15);
  18.   rect(edge, 250, width-2*edge, 100, 15);
  19.   for (int i=0; i < num; i++) {
  20.     balls[i].run();
  21.   }
  22. }
  23.  
  24. class Ball {
  25.   float x, y, sz;
  26.   float speedx, speedy;
  27.   float speed = 2;
  28.  
  29.   Ball() {
  30.     x = random(width);
  31.     y= random(height);
  32.     speedx = random(-speed,speed);
  33.     speedy = random(-speed,speed);
  34.     sz = random(5,10);
  35.   }
  36.  
  37.   void run() {
  38.     move();
  39.     bounce();
  40.     display();
  41.   }
  42.  
  43.   void move() {
  44.     x += speedx;
  45.     y += speedy;
  46.   }
  47.  
  48.   void bounce() {
  49.     if (x > width-sz/2 || x < sz/2) speedx *= -1;
  50.     if (y > height+10 ||y < -10) speedy *= -1;
  51.   }
  52.  
  53.   void display() {
  54.     fill(bg);
  55.     noStroke();
  56.     ellipse(x, y, sz, sz);
  57.   }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement