Recitation 10: Object-Oriented Programming Workshop – Ashley Zhu

For recitation, I chose to work on the object-oriented programming workshop with Tristan to learn more about this topic as well as animation. Tristan went over some fundamentals about classes, arrayLists, and objects. I learned how to use the animation function with classes and arrayList, so it’s good to practice and use the map function for more interactivity. I manipulated the map function so every time user click on the left arrow, the balls would appear and when clicked on a different key after pressing the left arrow, the balls will disappear.

Video:

CODE:

Ball ashleyBall;
Ball tristanBall;

float x;
ArrayList<Ball> ballList = new ArrayList<Ball>(); 

void setup(){
  size(1600,900);
  ballList = new ArrayList<Ball>(); 
  
  for(int i=0; i<100; i++){
  ballList.add(new Ball(random(-10,10), random(-10,10),color(random (255))));
  x=map(x,0,1023,0,width);
  }
    
}
void draw(){
  background(137,207,240);
  if (key == CODED) {
    if (keyCode == LEFT) {
     for(int i=0; i<ballList.size(); i++){
    Ball temp = ballList.get(i);
    temp.display();
    temp.move();
    } 
    
  }
  }
}

class Ball{
  float x,y;
  color c;
  float spdX, spdY;
  float r;
  Ball(float newSpdX, float newSpdY, color newColor){
    r=50;
    spdX= newSpdX;
    spdY= newSpdY;
    c=newColor;
    
    x=width/2;
    y=height/2;
    
  }
  void move(){
    x= x + spdX;
    y= y + spdY;
    
  }
  void display(){
    fill(c);
    ellipse(x,y,r,r);
  }
  
}

Leave a Reply