making colors to random values
in
Programming Questions
•
1 years ago
How would i organise the code so that fill colors change every time circles intersect?
(I've tried putting this in draw() but of course i get flashing random colors as draw() loops through, i've also tries putting it in boolean intersect, but get 'unreachable code')
- Ball ball1;
- Ball ball2;
- void setup() {
- size (300, 300);
- ball1 = new Ball(52);
- ball2 = new Ball (36);
- }
- void draw() {
- background(0);
- ball1.display();
- ball1.move();
- ball2.display();
- ball2.move();
- if (ball1.intersect(ball2)) {
- ball1.changeColor();
- ball2.changeColorz();
- }
- }
- class Ball {
- float rad;
- float x, y;
- float xspeed, yspeed;
- color c;
- Ball(float _rad) {
- rad= _rad;
- x = random(width);
- y = random(height);
- xspeed = random ( 4);
- yspeed = random (4);
- c= color (0,255,0);
- }
- void display() {
- fill (c);
- ellipse (x, y, rad, rad);
- c= color(0,255,0);
- }
- void changeColor() {
- c= color (255, 0, 0);
- }
- void changeColorz() {
- c=color(0, 0, 255);
- }
- void move() {
- x += xspeed;
- y += yspeed;
- if (x>width||x<0) {
- xspeed *= -1;
- }
- if (y>height||y<0) {
- yspeed *= -1;
- }
- }
- boolean intersect (Ball other) {
- float distance = dist (x, y, other.x, other.y);
- if (distance < rad + other.rad) {
- return true;
- }
- else {
- return false;
- }
- }
- }
1