From 374b31006b6a9ac83bba62953dfd6d2ee8d43e74 Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Thu, 10 Dec 2015 14:38:59 -0500 Subject: [PATCH 1/2] Move --- BouncyBallOOP/Ball.pde | 25 +++++++++++++++++++++---- BouncyBallOOP/BouncyBallOOP.pde | 7 ++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/BouncyBallOOP/Ball.pde b/BouncyBallOOP/Ball.pde index dcdc86d..d1e986c 100644 --- a/BouncyBallOOP/Ball.pde +++ b/BouncyBallOOP/Ball.pde @@ -1,20 +1,37 @@ 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; -//this is a constructor. you can have more than one constructor for a given class + //this is a constructor. you can have more than one constructor for a given class Ball() { - diam = 200; + diam = 100; loc = new PVector(random(diam, width-diam), random(diam, height-diam)); + vel = PVector.random2D(); c = color(random(255), random(50), random(100, 255)); } -//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..ba50715 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); + size(800, 600); b = new Ball(); //initialize b as a new object of the Ball class + c = new Ball(); } 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 From 4dd139ce2952a5e776d76965714cafe3e2085b31 Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Thu, 10 Dec 2015 14:49:53 -0500 Subject: [PATCH 2/2] Fixed --- BouncyBallOOP/Ball.pde | 6 +++++- BouncyBallOOP/BouncyBallOOP.pde | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/BouncyBallOOP/Ball.pde b/BouncyBallOOP/Ball.pde index d1e986c..86ff7e8 100644 --- a/BouncyBallOOP/Ball.pde +++ b/BouncyBallOOP/Ball.pde @@ -3,13 +3,17 @@ class Ball { 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() { + 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 diff --git a/BouncyBallOOP/BouncyBallOOP.pde b/BouncyBallOOP/BouncyBallOOP.pde index ba50715..f27e5da 100644 --- a/BouncyBallOOP/BouncyBallOOP.pde +++ b/BouncyBallOOP/BouncyBallOOP.pde @@ -3,8 +3,8 @@ Ball c; void setup() { size(800, 600); - b = new Ball(); //initialize b as a new object of the Ball class - c = new Ball(); + b = new Ball(10); //initialize b as a new object of the Ball class + c = new Ball(.9); } void draw() {