For this assignment, I tried to make an art piece in which the bubbles float behind a for-loop grid of black circles but are only visible in the first and third quadrant of the image. At first I had written the for-loops that created the circle grid under the update function in the class Bubble, which left one errant bubble floating on its own over top of the grid. I named him Spunky (pictured below).
However, after some help from Scott, I was able to domesticate Spunky and get him back behind the grid by writing the for loops just after void draw.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
Bubble[] bubbles; void setup(){ size(800, 600); background(0); bubbles = new Bubble[10]; for( int i =0; i <bubbles.length; i++) { bubbles[i]= new Bubble(); } } void draw (){ background(0); for (int i=0; i<bubbles.length; i ++){ bubbles[i].update(); //reference instance (b), not class (Bubble) bubbles[i].renderBubble(); bubbles[i].checkWalls(); } for(int x= 0; x< width; x= x+40){ for(int y= 0; y< height; y = y+30){ fill(0); ellipse(x+20, y+15, 30, 30); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
class Bubble{ // class name (convention to capitalize class name) float xPos; // int variables float yPos; float xSpeed; float ySpeed; float diam; color fillC; //define color Bubble(){ //constructor (this is where you name attributes) diam=50; xPos = random(diam/2, width-diam/2); yPos = random(diam/2, height-diam/2); xSpeed = random(-5.0, 5.0); ySpeed = random(-5.0, 5.0); fillC=color(random(127, 255), random(127, 255), random (127, 255)); //random color pastel } void update(){ //updates position if(xPos>width/2 && yPos>height/2 || xPos<width/2 && yPos<height/2){ fill(0); } else{ fill(fillC); //renders color } xPos = xPos+xSpeed; yPos = yPos+ySpeed; } void renderBubble(){ ellipse(xPos, yPos, diam, diam); } void checkWalls(){ //check window if (xPos > width-diam/2 || xPos < diam/2) { xSpeed = xSpeed*-1; } if (yPos > height-diam/2 || yPos < diam/2) { ySpeed = ySpeed*-1; } } } |