In this recitation, I did a lot of things. I firstly create the “bubbles” in this example posted in the recitation cite:
Here is my code:
float r,g,b; int ran; int [] cw; int [] ch; void setup(){ cw = new int[10]; ch = new int[10]; size(600,600); background(0); } void draw(){ if (mousePressed == true){ drawCircle(); } fill(255); textSize(20); text("Press UP arrow to clear",50,50); } void drawCircle(){ ran = int(random(1,4)); r = map(mouseX,0,width,0,255); g = map(mouseY,0,width,0,255); noStroke(); fill(r,g,255,random(100,255)); circle(mouseX,mouseY,int(random(20,35))); for (int i = 1; i <= ran; i++){ //fill(r,g,255); cw[i] = int(random(-50,51)); ch[i] = int(random(-50,51)); circle(mouseX+cw[i],mouseY+ch[i],int(random(5,20))); } } void keyPressed(){ if (key == CODED){ if (keyCode == UP){ background(0); } } }
And this is the video of my final result:
And then I finished the additional homework. I put two different tasks into two parts and use “button” to open it. I think maybe I can add another “back button” but I didn’t put it there. Here’s my code:
int d1 = 100; int d2 = 70; int i = 0; int timesI = 1; boolean check1 = false, check2 = false; int speedX, speedY; int posX,posY; void setup(){ size(600,600); speedX = 5; speedY = 3; posX = int(random(150,width-150)); posY = int(random(150,height-150)); } void draw(){ if (mousePressed == true && (mouseX>250 && mouseX<350 && mouseY>200 && mouseY<260)){ check1 = true; }else if(mousePressed == true && (mouseX>250 && mouseX<350 && mouseY>300 && mouseY<360)){ check2 = true; } if (check1 == false && check2 == false){ colorMode(HSB, 100); background(0,0,100); fill(0,0,100); rect(250,200,100,60); rect(250,300,100,60); fill(0,0,0); textSize(15); text("Additional", 262, 235); text("Bonus",278,335); }else{ if (check1 == true){ Additional(); }else if(check2 == true){ Bonus(); } } } void Additional(){ background(0,0,100); ring1(300,300); } void Bonus(){ background(0,0,100); posX+=speedX; posY+=speedY; if(posX+d1/2>width || posX-d1/2<0){ speedX = -speedX; } if(posY+d1/2>height || posY-d1/2<0){ speedY = -speedY; } ring2(posX,posY); fill(0,0,0); text("push UP and DOWN arrow to change the size",50,50); if (keyPressed && key == CODED){ if (keyCode == UP && d1<=150){ d1 = d1+5; d2 = d2+5; } if (keyCode == DOWN && d2 >=10){ d1 = d1-5; d2 = d2-5; } } } void ring1(int x,int y){ i = i + timesI; if (i < 0 || i > 100){ timesI = -timesI; } push(); noStroke(); fill(i,100,100); circle(x,y,d1 + i); fill(0,0,100); circle(x,y,d2 + i); pop(); } void ring2(int x,int y){ i = i + timesI; if (i < 0 || i > 100){ timesI = -timesI; } noStroke(); fill(i,100,100); circle(x,y,d1); fill(0,0,100); circle(x,y,d2); }
This is the final result:
I further more tried to use object to create a bunch of “bouncing bubbles” with random size, alpha and original speed and here’s my code:
Bug[] bugs; void setup() { size(600, 600); bugs = new Bug[50]; for (int i = 0; i < 50; i++) { bugs[i] = new Bug(); } } void draw() { background(255, 240, 245); //if (mouseIsPressed){ for (Bug bug : bugs) { bug.move(); bug.display(); bug.bounce(); } } class Bug { float x; float y; float speedx; float speedy; int dia; int t; float ran1,ran2; Bug() { x = random(25,width-25); y = random(25,height-25); speedx = random(1, 3); speedy = random(1, 3); dia = int(random(20, 50)); t = int(random(0, 100)); ran1 = random(-1,1); ran2 = random(-1,1); if(ran1 < 0){ speedx = -speedx; } if(ran2 < 0){ speedy = -speedy; } } void bounce() { if (x - dia/2 < 0 || x + dia/2 > width) { speedx = -speedx; } if (y - dia/2 < 0 || y + dia/2 > height) { speedy = -speedy; } } void move() { x += speedx; y += speedy; } void display() { noStroke(); fill(250, 128, 144, t); circle(x, y, dia); } }
This is the result video:
I think I can add more interactions to this project like every time I clicked the mouse, there will be a new “bubble” or something like this.
Among all these functions, I think mousePressed() and keyPressed() are the most useful because our class is called “interaction lab” so these two functions are very obvious with interaction identity. So I think these two function would must be very useful for our further project making.