REFLECTION:
For this week’s recitation, I attended the Object Oriented Programming workshop to better familiarize myself with using O.O.P. since I had trouble understanding it during class. I took what I had learned both in class and during the workshop to create my own work. During the workshop, I was able to better understand that OOP simplifies the workplace within programming and makes the code easier to understand. I was able to practice with the different parts of the class. I know that this is going to be very helpful to utilize for our final project.
For this exercise, I decided to create boxes that bounce off the borders and when the mouse is pressed, the boxes disappear. Although I initially struggled because I was not familiar with the different sections of the class, I was finally able to create my own work after some trial and error.
CODE:
ArrayList <Box> box;
void setup() {
size(600, 600);
noStroke();
background(0);
box = new ArrayList <Box> ();
for( int i=0; i<30; i++) {
box.add(new Box(random(width), random(height), random(30, 100)));
}
}
void draw() {
background(50,60);
for (int i=0; i < box.size(); i++) {
box.get(i).display();
box.get(i).move();
box.get(i).bounce();
}
}
void mousePressed() {
if(box.size() > 0) {
box.remove( box.size()-1 );
}
}
class Box {
float x, y, size, speedX, speedY;
color col;
boolean onScreen;
Box (float _x, float _y, float _size) {
x = _x;
y = _y;
size = _size;
col = color (random( 225), random(50), random(100));
onScreen = true;
speedX = random(-5,5);
speedY = random(-5,5);
}
void display() {
fill(col);
rect(x, y, size, size);
}
void move() {
x += speedX;
y += speedY;
}
void bounce() {
if (x<0||x>width) {
speedX=-speedX;
}
if (y<0||y>height) {
speedY=-speedY;
}
}
}