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
31 changes: 26 additions & 5 deletions BouncyBallOOP/Ball.pde
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
class Ball {
//declaring all information (fields) contained within the Ball class
//declaring all information (fields) contained within the Ball class
PVector loc, vel;
int diam;
color c;
float speed;

//this is a constructor. you can have more than one constructor for a given class
Ball() {
diam = 200;
//this is a constructor. you can have more than one constructor for a given class
Ball(float tSpeed) {
diam = 100;
speed = tSpeed;
loc = new PVector(random(diam, width-diam), random(diam, height-diam));
vel = PVector.random2D();
vel.mult(tSpeed);
c = color(random(255), random(50), random(100, 255));
speed = 1;
}

//after declaring fields and setting up constructors, you can define your methods
//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);

//wrap the ball's position
if (loc.x >= width) {
loc.x = 0;
} else if (loc.x <= 0) {
loc.x = width ;
}
if (loc.y >= height) {
loc.y = 0;
} else if (loc.y <= 0) {
loc.y = height ;
}
}
}
9 changes: 7 additions & 2 deletions BouncyBallOOP/BouncyBallOOP.pde
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
Ball b; //declare a new ball called b
Ball c;

void setup() {
size(1600, 1200);
b = new Ball(); //initialize b as a new object of the Ball class
size(800, 600);
b = new Ball(10); //initialize b as a new object of the Ball class
c = new Ball(.9);
}

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