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
44 changes: 39 additions & 5 deletions BouncyBallOOP/Ball.pde
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
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;
float diam;
color c;

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

//after declaring fields and setting up constructors, you can define your methods
Ball(float tDiam) {
diam = tDiam;
loc = new PVector(random(diam, width-diam), random(diam, height-diam));
vel = PVector.random2D();
vel.mult(20);
c = color(0, random(50, 255), random(100, 255));
}
//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 || loc.x < 0) {
vel.x *= -1;
}
if (loc.y > height || loc.y < 0) {
vel.y *= -1;
}
}

void wrap() {
if (loc.x >width) {
loc.x=0;
}
if (loc.x <0) {
loc.x = width;
}
if (loc.y >height) {
loc.y=0;
}
if (loc.y<0) {
loc.y=height;
}
}
}
17 changes: 13 additions & 4 deletions BouncyBallOOP/BouncyBallOOP.pde
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
int count =4;
Ball b; //declare a new ball called b

Ball c;
Ball[] balls = new Ball[count];
void setup() {
size(1600, 1200);
b = new Ball(); //initialize b as a new object of the Ball class
size(800, 800);
for (int i=0; i<count; i++) {
balls[i] = new Ball(20); //initialize b as a new object of the Ball class
balls[i] = new Ball(45);
}
}

void draw() {
background(0);
b.display(); //call b's display() method
for (int i=0; i<count; i++) {
balls[i].display(); //call b's display() method
balls[i].move(); //Gives ball movement
balls[i].bounce(); //Balls do not fly off screen
}
}