How to change speed of ellipses in processing -


i trying create array of circles move @ different speeds , have different colours using classes have done 5 seconds running reason ellipses go edges of window , code doesn't work properly. below have provided code including class , setup:

circle circles = new circle(1, 8);  void setup() {   size(800, 600); }  void draw() {   background(255);   circles.display();   circles.bounce(); }  class circle {   int[] posx = new int [10];   int[] posy = new int [10];   float[] speedx = new float[10];   float[] speedy = new float[10];   int[] red = new int [10];   int[] green = new int [10];   int[] blue = new int [10];   circle(float start, float end) {     (int = 0; < 10; i++) {       speedx[i] = random(start, end);       speedy[i] = random(start, end);       posx[i] = int(random(500, 800));       posy[i] = int(random(500, 600));       red[i] = int(random(0, 255));       green[i] = int(random(0, 255));       blue[i] = int(random(0, 255));     }   }   void display() {     (int = 0; < 10; i++) {       fill(red[i], green[i], blue[i]);       ellipse(posx[i], posy[i], 50, 50);     }   }   void bounce() {      (int = 0; < 10; i++) {       posx[i] += speedx[i];       posy[i] += speedy[i];       if (posx[i] - 50 < 0 || posx[i] + 50 > width) {         speedx[i] = -speedx[i];       }       if (posy[i] - 50 < 0 || posy[i] + 50 > height) {         speedy[i] = -speedy[i];       }     }   } } 

the main problem not setting balls inside screen again! common problem, , dan shiffman used show new processing 3 debugger.

but since i've found many stuff code comment other stuff might programming.

but first let me show working code:

int n_of_circles = 10; circle[] circles = new circle[n_of_circles];  void setup() {   size(800, 600);   (int = 0; < n_of_circles; i++) {     circles[i] = new circle(1, 8);   } }  void draw() {   background(255);   (int = 0; < n_of_circles; i++) {     circles[i].display();     circles[i].move();   } }  class circle {   float posx = random(0, width); //no need set these value in constructor   float posy = random(0, height); //we can set here   float speedx = 666; //we change these on constructor   float speedy = 666; //666 personal convention use indicate :p    //similarly, no need set color variable in constructor   //also, lets use color variable instead of 3 separate variables   color circ_color = color(       (int) random(0, 255),        (int) random(0, 255),       (int) random(0, 255)     );   //defining circle diameter instead of using literal ("50", in our case)   int diameter = 50;   int radius = diameter/2;     circle(float min_speed, float max_speed) {       // purpose of "min_speed" , "max_speed" clearer "start" , "end"       speedx = random(min_speed, max_speed);       speedy = random(min_speed, max_speed);   }    void display() {       //this push pop commands ensure fill command won't        //interfere other stuff, if ever reuse code       pushstyle();         fill( circ_color );         ellipse(posx, posy, diameter, diameter);           popstyle();   }    //notice how separated move declartions form bounce. it's programming practice   //to keep stuff simpler. functions should have 1 single responsibility.   //in case won't help´much, we're yoru move function more complex, would!    void move(){         posx += speedx;     posy += speedy;     bounce();    }    void bounce() {       //note using diameter, should use radius!        //if it's touching left or side edges       if (posx - radius < 0 || posx + radius > width) {         speedx *= -1; //inverts sign          //here's actual missing bits!         if (posx - radius < 0 ){ posx = radius; }         if (posx + radius > width ){ posx = width-radius; }       }        //if it's touching top or bottom edges       if (posy - radius < 0 || posy + radius > height) {         speedy *= -1; //inverts sign          //here's actual missing bits!         if (posy - radius < 0 ){ posy = radius; }         if (posy + radius > height ){ posy = height-radius; }       }    }  } //end of class circle 

just few suggestions:

  • the convention class names upper case. you have circle circles = new circle(1,5); clearer.
  • avoid using literals! (writing hard-coded numbers) example, we're using "ellipse(posx[i], posy[i], 50, 50);". using variable diameter code becomes more modular. should change circles diameter, alter 1 line of code!
  • use arrays of objects instead of classes contains other stuff. that's programming practice makes code easier understand. if need group lots of circles use array, or can create class "group_of_circles" ex.
  • i know i've presented new concepts/syntaxes , changed lot code, tried comment make clear , instructive
  • since i've changed thing didn't use vectors, when dealing positions , should use pvectors! this free book have great chapter on them!

try find tutorials software development. teach godo programming practices keeping code modular possible , etc. don't worry, come natrually on time!

try learncpp.com, tutorialss 1-10a , 1-4b. 2 part of c++ tutorial, starting point , refer programming in general.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -