Recitation 10: Workshops

For this recitation, I chose the object oriented programming workshop. We learned about object, class, and array, especially Arraylist.

For the exercise, I made an animation based on the code we wrote during the class and created a class for the Spiderman symbol. As the background, there are many of it moving and bouncing around. For interactivity, every time I click the mouse, it will add one more white symbol to the screen, and a pink one will be added every time the keyboard is pressed.  And I used map() function to make sure that the white symbol will only start at the center of the screen.

ArrayList<Spider> sList;

void setup(){
  size(1600,800);
 sList= new ArrayList<Spider>();
  for(int i=0; i<100; i++){
  sList.add(new Spider(random(width),random(height),color(random(100,255),random(0,50),random(0,85)),color(0)));
  }
}
void draw(){
background(0);
 for(int i=0; i<sList.size(); i++){
  Spider temp = sList.get(i);
   temp.display();
  temp.move();
 }
}
  void mousePressed(){
   float xx = map(mouseX,0,width,width/4,width/2);
float yy =map(mouseY,0,height,height/4,height/2);
   sList.add(new Spider(xx,yy,255,0));
 }
void keyPressed() {
  float x=random(width);
    float y=random(height); 
     sList.add(new Spider(x,y,255,#EA219A));
}
class Spider {
float x,y;
float size;
color clr;
float spdX;
float spdY;
color str;

Spider(float startingX,float startingY,color startingColor,color startingstr){
 x= startingX;
 y= startingY;
 size= random(50,100);
 clr=startingColor;
 str=startingstr;
spdX= random(0,6);
spdY= random(0,10);
}

void display(){
  fill(clr);
  noStroke();
  ellipse(x,y,size,size);
  stroke(str);
  strokeWeight(size/17);
  fill(255);
  arc(x-size/5,y-size/6,size/3,size/1.5,QUARTER_PI,PI,CHORD);
  arc(x+size/5,y-size/6,size/3,size/1.5,0,QUARTER_PI+HALF_PI,CHORD);
  
}
void move(){
  x+=spdX;
  y+=spdY;
 
if(x>=width || y>=height){
  spdX=spdX*-1;
  spdY=spdY*-1;
}
if(x<=0|| y<=0){
  spdX=spdX*-1;
  spdY=spdY*-1;
}
}

}

Leave a Reply