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