My code of using functions and arrays are down below:
int speed[] = new int[20]; int tag = 0; void setup() { size(1400, 900); background(255); } void draw() { if (mousePressed) { if (tag == 0){ background(255); tag = 1; } for (int i = 0; i < 5; i++) { strokeWeight(3); stroke(0); face(random(5,100),random(width),random(height),color(255,0,0)); face(random(5,100),random(width),random(height),color(235,205,0)); } noStroke(); bface(50,width/2,height/2,color(0,0,255)); } else { background(255); tag = 0; for (int i = 0; i < 80; i++) { noStroke(); float red = random(255); float green = random(255); float blue = random(255); face(random(20,200),random(width),random(height),color(red,green,blue)); } } } void face(float size, float x, float y, color c) { fill(c); ellipse(x, y, size, size); fill(255); ellipse(x-size*0.3, y-size*0.1, size*0.05, size*0.05); ellipse(x+size*0.3, y-size*0.1, size*0.05, size*0.05); arc(x, y, size*0.6, size*0.6, 0, PI); line(x-size*0.3, y,x+size*0.3, y); } void bface(float size, float x, float y, color c) { noStroke(); fill(c); ellipse(x, y, size, size); fill(255); ellipse(x-size*0.2, y-size*0.1, size*0.2, size*0.2); ellipse(x+size*0.2, y-size*0.1, size*0.2, size*0.2); }
And it works like :
Question 1:
In your own words, please explain the difference between having your for loop from Step 2 in setup()
as opposed to in draw()
.
The main distinctions between setup() codes and draw() codes is that setup() codes only run once while draw() codes run every millisecond (unless you use delay() to control the frequency).
Question 2:
What is the benefit of using arrays? How might you use arrays in a potential project?
The benefit of using arrays is, instead of controlling each variable in one sentence, you can do this once by a for loop. And it’s clearer and more good-looking defining a array rather than defining lots of variables.