Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. int numberOfCircles;
  2. int lineX;
  3. //is the line moving right? To check if the line is moving left you just check if it's NOT moving right (!isLineMovingRight)
  4. boolean isLineMovingRight;
  5. //The index of the array is always the number of the circle. So if I do xPosition[5] I get the x value for the 6th circle. Remember, arrays start at 0, not 1!
  6. int[] xPositions;
  7. int[] yPositions;
  8. int[] radii;
  9.  
  10. void setup() {
  11.   size(500, 500);
  12.   background(0);
  13.   ellipseMode(CORNER);
  14.   lineX = 0;
  15.   numberOfCircles = 100;
  16.   isLineMovingRight = true;
  17.   xPositions = new int[numberOfCircles];
  18.   yPositions = new int[numberOfCircles];
  19.   radii = new int[numberOfCircles];
  20.   //I've put the whole generating circle code into its own method, so I can call it in other places without re-writing it again. Specifically, we need it again when the line reaches one of the window borders.
  21.   calculateCircles();
  22. }
  23.  
  24. void draw() {
  25.   background(50);
  26.   //code to draw circles and check if they should be moved
  27.   noStroke();
  28.   fill(255, 125);
  29.   for (int i = 0; i < numberOfCircles; i++) {
  30.     if (isLineMovingRight && xPositions[i] == lineX) {
  31.       xPositions[i]++;
  32.     }
  33.     //when the line is moving left, we have to add the radius to the xPosition of the circle. To see what happens if we don't, just remove the "+radii[i]". This is because the "corner" of each circle is in the top left.
  34.     if (!isLineMovingRight && xPositions[i]+radii[i] == lineX) {
  35.       xPositions[i]--;
  36.     }
  37.     //using the circle function instead of the ellipse function, well, because uh that's what it's made for. Of course you could just use ellipse and add another argument with radii[i] again *shrug*
  38.     circle(xPositions[i], yPositions[i], radii[i]);
  39.   }
  40.   //code to draw and move line
  41.   stroke(255, 0, 0);
  42.   line(lineX, 0, lineX, height);
  43.   //another way to write the following 5-line-if-else-statement would be:
  44.   //lineX = isLineMovingRight ? lineX+1 : lineX-1
  45.   //the question mark is called a "ternary operator". You'll use it all the time once you've learned about it because it unclutters the code greatly. In this case it saves 4 lines of code!
  46.   if (isLineMovingRight) {
  47.     lineX++;
  48.   } else {
  49.     lineX--;
  50.   }
  51.   lineX = isLineMovingRight ? lineX-1 : lineX+1
  52.   //if the line hits either the right or left side, we need to switch direction and calculate new circle positions.
  53.   if (lineX == width || lineX == 0) {
  54.     isLineMovingRight = !isLineMovingRight;
  55.     calculateCircles();
  56.   }
  57. }
  58.  
  59. void calculateCircles() {
  60.   for (int i = 0; i < numberOfCircles; i++) {
  61.     radii[i] = ceil(random(1, 30));
  62.     yPositions[i] = ceil(random(0, height-radii[i]));
  63.     xPositions[i] = ceil(random(0, width-radii[i]));
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement