# Need Help Understanding this code

**URL:** <https://discourse.processing.org/t/need-help-understanding-this-code/3992>\
**Category:** Coding Questions\
**Created:** [September 30, 2018, 10:03pm UTC](https://discourse.processing.org/t/need-help-understanding-this-code/3992 "2018-09-30T22:03:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ant](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@ant](https://discourse.processing.org/u/ant)\
**Post date:** [September 30, 2018, 10:03pm UTC](https://discourse.processing.org/t/need-help-understanding-this-code/3992/1 "2018-09-30T22:03:08Z")

</div>

Need help understanding how these lines of codes work. I know they are variables and methods, but beyond that I’m confused.

```auto
//
int x=50;
Circles[]circle=new Circles [x];
//
void setup(){
  size(800,600);
  frameRate(120);
  //
  for(int i=0;i<circle.length;i++){
    circle[i]=new Circles();
  }
}
//
void draw(){
  for(int i=0;i<circle.length;i++){
    circle[i].move();
    circle[i].display();
  }
}
//
class Circles{
  float x;
  float y;
  float xspeed;
  float yspeed;
  float circlesize;
//
Circles(){
    x=random(100,700);
    y=random(100,700);
    xspeed=random(0.5,1);
    yspeed=random(0.5,1);
    circlesize=random(40,100);
  }
//
void move(){
    x =x+xspeed;
    y =y+yspeed;
    if(x+(circlesize/3)>800||x-(circlesize/3)< 0){
      xspeed=xspeed*-1;
    }
    if(y+(circlesize/3)>600||y-(circlesize/3)< 0) {
      yspeed=yspeed*-1;
    }
  }
// 
void display() {
  fill(254,127,156);
  ellipse(x,y,circlesize,circlesize);
  }}

```

---

<div class="post-metadata">

**Author:** ![Quillbert1](https://avatars.discourse-cdn.com/v4/letter/q/f475e1/32.png) [@Quillbert1](https://discourse.processing.org/u/Quillbert1)\
**Post date:** [October 1, 2018, 2:16am UTC](https://discourse.processing.org/t/need-help-understanding-this-code/3992/2 "2018-10-01T02:16:40Z")

</div>

The bottom part below draw is a class named circle. This is a generic description of a circle that can be replicated.

The following code is called whenever a copy of the circle class is created.

```auto
Circles(){
    x=random(100,700);
    y=random(100,700);
    xspeed=random(0.5,1);
    yspeed=random(0.5,1);
    circlesize=random(40,100);
  }

```

It sets x, y, xspeed, yspeed and circlesize to random values.

The following code moves the circle.

```auto
void move(){
    x =x+xspeed;
    y =y+yspeed;
    if(x+(circlesize/3)>800||x-(circlesize/3)< 0){
      xspeed=xspeed*-1;
    }
    if(y+(circlesize/3)>600||y-(circlesize/3)< 0) {
      yspeed=yspeed*-1;
    }
  }

It adds xspeed to the Circles x, and yspeed to the Circles y, as long as the circle is within certain coordinates.

The following code draws the circle.

```

```auto
void display() {
  fill(254,127,156);
  ellipse(x,y,circlesize,circlesize);
  }

```

This code at the top creates a list of Circles.

```auto
Circles[]circle=new Circles [x];

```

This list is called an array.

This code in setup puts a circle in every spot in the array.

```auto
  for(int i=0;i<circle.length;i++){
    circle[i]=new Circles();
  }

```

This is a loop that repeats until it has acted once for every spot in the array.

The following code in draw moves and displays every circle.

```auto
for(int i=0;i<circle.length;i++){
    circle[i].move();
    circle[i].display();
  }

```

It cycles through every circle in the list calling the move and display methods that were defined in the circle class.
