diff --git a/BouncyBallOOP/Ball.pde b/BouncyBallOOP/Ball.pde index dcdc86d..86ff7e8 100644 --- a/BouncyBallOOP/Ball.pde +++ b/BouncyBallOOP/Ball.pde @@ -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 ; + } + } } \ No newline at end of file diff --git a/BouncyBallOOP/BouncyBallOOP.pde b/BouncyBallOOP/BouncyBallOOP.pde index 97b4782..f27e5da 100644 --- a/BouncyBallOOP/BouncyBallOOP.pde +++ b/BouncyBallOOP/BouncyBallOOP.pde @@ -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(); } \ No newline at end of file