On this week’s recommendation, we need to use array and for loop to complete the code.
Q1
I think for loop in setup is a code that is completed in an instant. Since the tasks in setup are only executed once, the code in setup will be completed in an instant. Draw means that the whole big program runs in a loop.
Q2
When using array, you can simplify the code, such as arranging the numbers in order. Different numbers represent a value. The advantage of array is that you don’t need to set them one by one. In future projects, I think we can use array to simplify the program in coding or to design games.
The final code
int circle = 100; float[] x = new float[circle]; float[] y = new float[circle]; float[] speedX = new float[circle]; float[] speedY = new float[circle]; float[] size = new float[circle]; color[] c = new color[circle]; void setup() { size(600,500); background(255); for (int i=0; i < 100; i++) { x[i] = random(width); y[i] = random(height); speedX[i] = random(-2, 2 ); speedY[i] = random( -2, 2 ); size[i] = random(10,500); c[i] = color(random(255),random(255),random(255)); drawCircle(50,50,color((255),color(255),color(255))); } } //void display(float x, float y, color c) { //} void draw() { background(255); for (int i=0; i< 100; i++){ drawCircle(x[i], y[i], c[i]); circle(x[i], y[i], 100); circle(x[i]-25, y[i]-25, 20); circle(x[i]+25, y[i]-25, 20); arc(x[i], y[i], 80, 80, 0, PI); x[i] = x[i] + speedX[i]; y[i] = y[i] + speedY[i]; if (x[i] > width || x[i]< 0) { speedX[i] = -speedX[i]; } if (y[i] > height || y[i]< 0) { speedY[i] = -speedY[i]; } } } void drawCircle(float u, float v, color c) { //noStroke(); fill(c); ellipse(u,v,50,50); }