From f5524dc98a7addf81c39ccbc460b84831aeae57b Mon Sep 17 00:00:00 2001 From: staylor30072 Date: Fri, 11 Dec 2015 08:57:59 -0500 Subject: [PATCH 1/7] Adds second ball --- BouncyBallOOP/BouncyBallOOP.pde | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/BouncyBallOOP/BouncyBallOOP.pde b/BouncyBallOOP/BouncyBallOOP.pde index 97b4782..79c2567 100644 --- a/BouncyBallOOP/BouncyBallOOP.pde +++ b/BouncyBallOOP/BouncyBallOOP.pde @@ -1,11 +1,13 @@ Ball b; //declare a new ball called b - +Ball c; void setup() { - size(1600, 1200); + size(800, 800); 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 + c.display(); } \ No newline at end of file From 7d9735105fae4a67315039307300623596561c57 Mon Sep 17 00:00:00 2001 From: staylor30072 Date: Fri, 11 Dec 2015 08:59:33 -0500 Subject: [PATCH 2/7] Balls move --- BouncyBallOOP/Ball.pde | 3 +++ BouncyBallOOP/BouncyBallOOP.pde | 2 ++ 2 files changed, 5 insertions(+) diff --git a/BouncyBallOOP/Ball.pde b/BouncyBallOOP/Ball.pde index dcdc86d..af3c230 100644 --- a/BouncyBallOOP/Ball.pde +++ b/BouncyBallOOP/Ball.pde @@ -17,4 +17,7 @@ class Ball { noStroke(); ellipse(loc.x, loc.y, diam, diam); } + void move(){ + loc.add(vel); + } } \ No newline at end of file diff --git a/BouncyBallOOP/BouncyBallOOP.pde b/BouncyBallOOP/BouncyBallOOP.pde index 79c2567..c744233 100644 --- a/BouncyBallOOP/BouncyBallOOP.pde +++ b/BouncyBallOOP/BouncyBallOOP.pde @@ -10,4 +10,6 @@ void draw() { background(0); b.display(); //call b's display() method c.display(); + b.move(); //Gives ball movement + c.move(); } \ No newline at end of file From 020e08cf37e5d3c5a29e23852c44829fe291bad8 Mon Sep 17 00:00:00 2001 From: staylor30072 Date: Fri, 11 Dec 2015 09:00:45 -0500 Subject: [PATCH 3/7] fixed movement --- BouncyBallOOP/Ball.pde | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BouncyBallOOP/Ball.pde b/BouncyBallOOP/Ball.pde index af3c230..9414612 100644 --- a/BouncyBallOOP/Ball.pde +++ b/BouncyBallOOP/Ball.pde @@ -9,6 +9,8 @@ class 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 From eeadd3ac602b2c96e3dbc27e87cc45db36115743 Mon Sep 17 00:00:00 2001 From: staylor30072 Date: Fri, 11 Dec 2015 09:02:35 -0500 Subject: [PATCH 4/7] Balls don;'t ly off screen --- BouncyBallOOP/Ball.pde | 16 ++++++++++++---- BouncyBallOOP/BouncyBallOOP.pde | 2 ++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/BouncyBallOOP/Ball.pde b/BouncyBallOOP/Ball.pde index 9414612..cbecc07 100644 --- a/BouncyBallOOP/Ball.pde +++ b/BouncyBallOOP/Ball.pde @@ -1,10 +1,10 @@ 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; loc = new PVector(random(diam, width-diam), random(diam, height-diam)); @@ -13,13 +13,21 @@ class Ball { vel.mult(20); } -//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(){ + 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; + } + } } \ No newline at end of file diff --git a/BouncyBallOOP/BouncyBallOOP.pde b/BouncyBallOOP/BouncyBallOOP.pde index c744233..79fad39 100644 --- a/BouncyBallOOP/BouncyBallOOP.pde +++ b/BouncyBallOOP/BouncyBallOOP.pde @@ -12,4 +12,6 @@ void draw() { c.display(); b.move(); //Gives ball movement c.move(); + b.bounce(); //Balls do not fly off screen + c.bounce(); } \ No newline at end of file From 495997efdcf03321335276a2f3ac5042e2580e0d Mon Sep 17 00:00:00 2001 From: staylor30072 Date: Fri, 11 Dec 2015 09:04:13 -0500 Subject: [PATCH 5/7] Adds constructor --- BouncyBallOOP/Ball.pde | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/BouncyBallOOP/Ball.pde b/BouncyBallOOP/Ball.pde index cbecc07..99646e6 100644 --- a/BouncyBallOOP/Ball.pde +++ b/BouncyBallOOP/Ball.pde @@ -1,10 +1,10 @@ class Ball { //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)); @@ -12,7 +12,13 @@ class Ball { vel = PVector.random2D(); vel.mult(20); } - + 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); From b20f5b67168bfa0718fffaffde823b6033f45030 Mon Sep 17 00:00:00 2001 From: staylor30072 Date: Fri, 11 Dec 2015 09:05:31 -0500 Subject: [PATCH 6/7] Changed the diameter --- BouncyBallOOP/BouncyBallOOP.pde | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BouncyBallOOP/BouncyBallOOP.pde b/BouncyBallOOP/BouncyBallOOP.pde index 79fad39..54314b9 100644 --- a/BouncyBallOOP/BouncyBallOOP.pde +++ b/BouncyBallOOP/BouncyBallOOP.pde @@ -2,8 +2,8 @@ Ball b; //declare a new ball called b Ball c; void setup() { size(800, 800); - b = new Ball(); //initialize b as a new object of the Ball class - c = new Ball(); + b = new Ball(45); //initialize b as a new object of the Ball class + c = new Ball(45); } void draw() { From d69f7cc36193498adc0fd236ea0f764b4b386739 Mon Sep 17 00:00:00 2001 From: staylor30072 Date: Fri, 11 Dec 2015 09:22:23 -0500 Subject: [PATCH 7/7] Adds wrap function and array --- BouncyBallOOP/Ball.pde | 15 +++++++++++++++ BouncyBallOOP/BouncyBallOOP.pde | 19 +++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/BouncyBallOOP/Ball.pde b/BouncyBallOOP/Ball.pde index 99646e6..c34c1ab 100644 --- a/BouncyBallOOP/Ball.pde +++ b/BouncyBallOOP/Ball.pde @@ -36,4 +36,19 @@ class Ball { 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 54314b9..2d9773e 100644 --- a/BouncyBallOOP/BouncyBallOOP.pde +++ b/BouncyBallOOP/BouncyBallOOP.pde @@ -1,17 +1,20 @@ +int count =4; Ball b; //declare a new ball called b Ball c; +Ball[] balls = new Ball[count]; void setup() { size(800, 800); - b = new Ball(45); //initialize b as a new object of the Ball class - c = new Ball(45); + for (int i=0; i