Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions BouncyBallOOP/Ball.pde
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,55 @@ class Ball {
Ball() {
diam = 200;
loc = new PVector(random(diam, width-diam), random(diam, height-diam));
vel = PVector.random2D();
vel.mult(20);
c = color(random(255), random(50), random(100, 255));
}

Ball(int tDiam) {
diam = tDiam;
loc = new PVector(random(diam, width-diam), random(diam, height-diam));
vel = PVector.random2D();
vel.mult(10);
c = color(random(255), random(50), random(100, 255));
}

Ball(float tX, float tY) {
diam = 50;
loc = new PVector(tX, tY);
vel = PVector.random2D();
vel.mult(30);
c = color(0, 255, 0);
}
Ball(float tX, float tY, int tDiam) {
diam = tDiam;
loc = new PVector(tX, tY);
vel = PVector.random2D();
vel.mult(5);
c = color( 255, 0 ,45);
}

//after declaring fields and setting up constructors, you can define your methods
void display() {
fill(c);
noStroke();
ellipse(loc.x, loc.y, diam, diam);
}
void move() {loc.add(vel);
}

void bounce() {
if (loc.x >= width){
vel.x *= -1;
}
if (loc.x <= 0){
vel.x *= -1;
}
if (loc.y >= height){
vel.y *= -1;
}
if (loc.y <= 0){
vel.y *= -1;
}
}
}
15 changes: 14 additions & 1 deletion BouncyBallOOP/BouncyBallOOP.pde
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
Ball b; //declare a new ball called b

Ball c;
Ball d;
void setup() {
size(1600, 1200);
b = new Ball(); //initialize b as a new object of the Ball class
c = new Ball();
d = new Ball(25, 13, 14);
}


void draw() {
background(0);
b.display(); //call b's display() method
c.display();
d.display();
b.move();
c.move();
d.move();
d.bounce();
b.bounce();
c.bounce();

}