Recitation 11: Workshops on Object Oriented Programming by Kaycee

For this recitation, I chose Object-Oriented Programming workshop because I will do a game as my final project and this workshop could benefit me the most in my opinion.

Based on what I learned in the class on Thursday, I made some improvements and interaction function to the code.

I changed the interaction form with the mouse from producing more balls to make the balls disappear. Since two kinds of interaction were impossible to perform at the same time, so I deleted the original code and set the number of the balls in the setup loop as 20. In my first trial, I directly put an “if” function, saying that the ball which the mouse clicked on would disappear. However, my alternation didn’t work since the program couldn’t recognize the radius of each different ball. And I realized that to eventually achieve my purpose, another attribute was also needed in the Balls function to represent its radius. And when the position where the mouse clicks falls in the area that is within the radius of that circle, the circle would disappear.

I added the interaction with the keyboard in order to make it more interactive. Since I kept the part of the code of bounce, the balls would still move in the adverse direction when touching the rim of the canvas. Addition to that, users could also control the movement of the balls by clicking on the direction keys. However, when I pressed one of the direction keys, although all the balls will generally move in the intended direction, some balls will ignore the bounce code and disappear at the rim of the canvas. And I wondered why this would happen.

Code:

ArrayList<Ball> balls = new ArrayList<Ball>();

void setup() {
size(500, 600);
noStroke();
balls = new ArrayList<Ball>();
for (int i=0; i<20; i++) {
balls.add(new Ball(width/2, height/2, random(30,75)));
}

}
void draw() {
background(255);
for (int i=0; i<balls.size(); i++) {
Ball b = balls.get(i);
b.move();
b.bounce();
b.display();
if (key==CODED) {
if (keyCode==UP) {
b.y–;
}
if (keyCode==DOWN) {
b.y++;
}
if (keyCode==LEFT) {
b.x–;
}
if (keyCode==DOWN) {
b.x++;
}
}
}
if (mousePressed) {
for (int i=0; i<balls.size(); i++) {
Ball b = balls.get(i);
float diameter=b.w;
float dx = abs(mouseX-b.x);
float dy = abs(mouseX-b.y);
float distance= sqrt(sq(dx)+sq(dy));
if (distance<= diameter) {
balls.remove(i);
}
}
}
}

ArrayList<Ball> balls = new ArrayList<Ball>();

void setup() {
size(500, 600);
noStroke();
balls = new ArrayList<Ball>();
for (int i=0; i<20; i++) {
balls.add(new Ball(width/2, height/2, random(30,75)));
}

}
void draw() {
background(255);
for (int i=0; i<balls.size(); i++) {
Ball b = balls.get(i);
b.move();
b.bounce();
b.display();
if (key==CODED) {
if (keyCode==UP) {
b.y–;
}
if (keyCode==DOWN) {
b.y++;
}
if (keyCode==LEFT) {
b.x–;
}
if (keyCode==DOWN) {
b.x++;
}
}
}
if (mousePressed) {
for (int i=0; i<balls.size(); i++) {
Ball b = balls.get(i);
float diameter=b.w;
float dx = abs(mouseX-b.x);
float dy = abs(mouseX-b.y);
float distance= sqrt(sq(dx)+sq(dy));
if (distance<= diameter) {
balls.remove(i);
}
}
}
}

Leave a Reply