Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int num = 2000;
- int edge= 50;
- Ball[] balls = new Ball[num];
- color bg = #ffffff; //background + balls
- color f = #2F3440; // fill rect
- void setup() {
- size(500, 400);
- for (int i=0; i < num; i++) {
- balls[i]= new Ball();
- }
- }
- void draw() {
- background(bg);
- fill(f);
- rect(edge, 50, width-2*edge, 100, 15);
- rect(edge, 250, width-2*edge, 100, 15);
- for (int i=0; i < num; i++) {
- balls[i].run();
- }
- }
- class Ball {
- float x, y, sz;
- float speedx, speedy;
- float speed = 2;
- Ball() {
- x = random(width);
- y= random(height);
- speedx = random(-speed,speed);
- speedy = random(-speed,speed);
- sz = random(5,10);
- }
- void run() {
- move();
- bounce();
- display();
- }
- void move() {
- x += speedx;
- y += speedy;
- }
- void bounce() {
- if (x > width-sz/2 || x < sz/2) speedx *= -1;
- if (y > height+10 ||y < -10) speedy *= -1;
- }
- void display() {
- fill(bg);
- noStroke();
- ellipse(x, y, sz, sz);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement