For this week’s recitation assignment, I created black, white and grey colored boats that bounced off the walls.
Step 1: I set up the variables and arrays that we needed before setup.
Step 2: In void setup (), I established the range of values regarding speed, width and position.
Step 3: In void draw (), I used the for loop function in order to randomize the values we set in void setup.
Step 4: In void house (), I created what the image would look like.
(This is a link to my moving sketch)
int house= 100; //number of graphics float[] posX = new float[house]; //array for position of x, this means that there will be a random start x float[] posY = new float[house]; float[] speedX = new float[house];// array for speed, assigning 0 to 99 one number between float[] speedY = new float[house]; float [] w= new float [house]; //width of triangle float [] h= new float [house]; //height of triangle float []c= new float [house]; //color float []size= new float [house]; //rect size float []size2= new float [house]; //arc //declare an array of the length 100 //float[] opacity = {random(255), random(255), random(255), random(255), random(255)}; void setup (){ //freezed frame at the first moment it starts size (600,600); background (255); for (int i= 0; i<house; i++){ // this is direction related to arrays, ++= count by ones posX[i]= random(width); //this really means from 0 to width of screen posY[i]= random(height); speedX[i]= random(-10,10); //range speedY[i]= random(-10,10); w[i]=random (100); h[i]= random (100); c[i]= random (255); size[i]=random(50,100); size2[i]= random(50,100); } // i is just an integer that represents all of the houses //if for loop is here, then it will look stagnant, just draw shape, just goes through once } void draw () { background(255); for (int i= 0; i<100; i++){ //we are defining a variable i house (posX[i],posY[i], w[i],h[i], size[i], size2[i], color(c[i])); //calling the function posX[i] = posX[i]+ speedX[i]; // adding speed to position, going to be something between -10, 10 posY[i]= posY[i] + speedY[i]; if (posX[i] > width || posX[i]< 0) { //this is when it bounces off the wall , "||"=or speedX[i] = -speedX[i]; } if (posY[i] > height || posY[i]< 0) { speedY[i] = -speedY[i]; } } } void house(float x, float y, float w, float h, float size, float size2, color c) { fill (c); rect (x,y,size,size); triangle(x, y, x+w/2, y-h, x+w, y); arc (x, y, size2, size2, 0, PI+QUARTER_PI,CLOSE); } //void triangleSimple(float x, float y, float w, float h) { // A wrapper for standard triangle() command. // triangleSimple has the lower left corner as x,y. // The top is above the base (or below it with a negative parameter for h). //Use these parameters to create your graphics
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()
If you put “for loop” in setup, it will only run through the code one time, thus be a stagnant image. However, when you put for loop in draw, it will run through the code repeatedly creating the moving image.
Question 2:
What is the benefit of using arrays? How might you use arrays in a potential project?
We use arrays if there is a pattern we want to build but don’t want to have a repetition of code which would take up more space. For a potential project, I might use arrays to create multi-colored butterflies flying around the screen.