Advertisement
TeslaCoilGirl

Dot Bounce

Sep 12th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. Ball[] b = new Ball[100];
  2.  
  3. boolean randBoolX;
  4. boolean randBoolY;
  5.  
  6.  
  7. void setup() {
  8.   fullScreen();
  9.   colorMode(HSB, 100);
  10.  
  11.   for (int i = 0; i < b.length; i++) {
  12.     float randBoolTestX = random(1, 100);
  13.     float randBoolTestY = random(1, 100);
  14.  
  15.     if (randBoolTestX >= 50) {
  16.       randBoolX = true;
  17.     }
  18.     if (randBoolTestX < 50) {
  19.       randBoolX = false;
  20.     }
  21.     if (randBoolTestY >= 50) {
  22.       randBoolY = true;
  23.     }
  24.     if (randBoolTestY < 50) {
  25.       randBoolY = false;
  26.     }
  27.     b[i] = new Ball(false, random(0, 1), randBoolX, randBoolY, random(1, 100), random(1, width), random(1, height), random(1, 10), random(1, 10), random(10, 60));
  28.   }
  29. }
  30. void draw() {
  31.   background (0);
  32.  
  33.   for (int i = 0; i < b.length; i++) {
  34.  
  35.     b[i].ballDraw();
  36.     b[i].ballBounce();
  37.     b[i].ballMove();
  38.     b[i].ballFade();
  39.     b[i].ballTransform();
  40.     b[i].ballHueCycle();
  41.  
  42.  
  43.     if (b[i].ballTransp <= 0) {
  44.       b[i] = new Ball(false, random(0, 1), randBoolX, randBoolY, random(1, 100), random(1, width), random(1, height), random(1, 10), random(1, 10), random(10, 60));
  45.     }
  46.   }
  47. }
  48. class Ball {
  49.  
  50.   float ballHue;
  51.   float ballX;
  52.   float ballY;
  53.  
  54.   float ballTransp = 1;
  55.   float ballOffsetX = 0;
  56.   float ballOffsetY = 0;
  57.  
  58.   float fadeSpeed;
  59.  
  60.   float randX;
  61.   float randY;
  62.  
  63.   float ballDiam;
  64.   boolean bounceDirX;
  65.   boolean bounceDirY;
  66.   boolean ballMaxTransp = false;
  67.   float ballMaxSize = random(80, 120);
  68.   Boolean ballMaxSizeBool;
  69.   float ballEnlarge = 0;
  70.  
  71.   Ball(boolean tempBallMaxSizeBool, float tempFadeSpeed, Boolean tempBounceDirX, Boolean tempBounceDirY, float tempBallHue, float tempBallX, float tempBallY, float tempRandX, float tempRandY, float tempBallDiam) {
  72.     ballHue = tempBallHue;
  73.     ballX = tempBallX;
  74.     ballY = tempBallY;
  75.     randX = tempRandX;
  76.     randY = tempRandY;
  77.     ballDiam = tempBallDiam;
  78.     bounceDirX = tempBounceDirX;
  79.     bounceDirY = tempBounceDirY;
  80.     fadeSpeed = tempFadeSpeed;
  81.     ballMaxSizeBool = tempBallMaxSizeBool;
  82.   }
  83.   void ballDraw() {
  84.     noStroke();
  85.     fill(ballHue, 100, 100, ballTransp);
  86.     ellipse(ballX + ballOffsetX, ballY + ballOffsetY, ballDiam + ballEnlarge, ballDiam + ballEnlarge);
  87.   }
  88.  
  89.   void ballBounce() {
  90.  
  91.     if (ballX + ballOffsetX + 0.5*ballDiam <= 0) {
  92.       ballOffsetX += randX;
  93.       bounceDirX = !bounceDirX;
  94.     }
  95.     if (ballX + ballOffsetX + 0.5*ballDiam >= width) {
  96.       ballOffsetX -= randX;
  97.       bounceDirX = !bounceDirX;
  98.     }
  99.     if (ballY + ballOffsetY + 0.5*ballDiam <= 0) {
  100.       ballOffsetY += randY;
  101.       bounceDirY = !bounceDirY;
  102.     }
  103.     if (ballY + ballOffsetY + 0.5*ballDiam >= height) {
  104.       ballOffsetY -= randY;
  105.       bounceDirY = !bounceDirY;
  106.     }
  107.   }
  108.   void ballMove() {
  109.     if (bounceDirX == true) {
  110.       if (ballX + ballOffsetX + 0.5*ballDiam > 0 && ballX + ballOffsetX + 0.5*ballDiam < width) {
  111.         ballOffsetX += randX;
  112.       }
  113.     }
  114.     if (bounceDirY == true) {
  115.       if (ballY + ballOffsetY + 0.5*ballDiam > 0 && ballY + ballOffsetY + 0.5*ballDiam < height) {
  116.         ballOffsetY += randY;
  117.       }
  118.     }
  119.     if (bounceDirX == false) {
  120.       if (ballX + ballOffsetX + 0.5*ballDiam > 0 && ballX + ballOffsetX + 0.5*ballDiam < width) {
  121.         ballOffsetX -= randX;
  122.       }
  123.     }
  124.     if (bounceDirY == false) {
  125.       if (ballY + ballOffsetY + 0.5*ballDiam > 0 && ballY + ballOffsetY + 0.5*ballDiam < height) {
  126.         ballOffsetY -= randY;
  127.       }
  128.     }
  129.   }
  130.   void ballFade() {
  131.  
  132.     if (ballMaxTransp == false) {
  133.       ballTransp++;
  134.     }
  135.     if (ballTransp >= 100) {
  136.       ballMaxTransp = true;
  137.     }
  138.     if (ballMaxTransp == true) {
  139.       ballTransp -= fadeSpeed;
  140.     }
  141.   }
  142.  
  143.   void ballTransform() {
  144.     if (ballMaxSizeBool == false) {
  145.       ballEnlarge += sin(0.1*randX);
  146.     }
  147.     if (ballDiam + ballEnlarge >= ballMaxSize) {
  148.       ballMaxSizeBool = true;
  149.       ballEnlarge -= sin(0.1*randX);
  150.     }
  151.     if (ballMaxSizeBool == true) {
  152.       ballEnlarge -= sin(0.1*randX);
  153.     }
  154.     if (ballEnlarge <= 0) {
  155.       ballMaxSizeBool = false;
  156.     }
  157.   }
  158.   void ballHueCycle() {
  159.     ballHue++;
  160.     if (ballHue >100) {
  161.       ballHue = 1;
  162.     }
  163.   }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement