Advertisement
vencinachev

OOP Processing

Jan 10th, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.89 KB | None | 0 0
  1. class Ball{
  2.   float x;
  3.   float y;
  4.   float d;
  5.   float speedx;
  6.   float speedy;
  7.   color c;
  8.  
  9.   Ball(){
  10.     x = random(100, 500);
  11.     y = random(100, 500);
  12.     d = random(5, 100);
  13.     speedx = random(1, 5);
  14.     speedy = random(1, 5);
  15.     c = color(random(0, 255), random(0, 255), random(0, 255));
  16.   }
  17.  
  18.   void display(){
  19.     fill(c);
  20.     ellipse(x, y, d, d);
  21.   }
  22.  
  23.   void move(){
  24.     x += speedx;
  25.     y += speedy;
  26.   }
  27.   void reflect(){
  28.     if (x > width || x < 0){
  29.       speedx = -speedx;
  30.     }
  31.     if (y > height || y < 0){
  32.       speedy = -speedy;
  33.     }
  34.   }
  35. }
  36.  
  37.  
  38. Ball[] balls;
  39.  
  40. void setup(){
  41.   size(600, 600);
  42.   balls = new Ball[100];
  43.   for (int i = 0; i < balls.length; i++){
  44.     balls[i] = new Ball();
  45.   }
  46. }
  47.  
  48. void draw(){
  49.   background(255);
  50.   for (int i = 0; i < balls.length; i++){
  51.     balls[i].display();
  52.     balls[i].move();
  53.     balls[i].reflect();
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement