// Ball class class Ball { float x, y; float yspeed = 0; float xspeed = random(8)-4; float r; float gravity = 0.1; int redvalue = 255; int bluevalue = 255; int greenvalue = 255; int bouncecount = 0; // three states for alive: // 0 = dead, do nothing // 1 = alive, move normally // 2 = dead, ready to be archived int alive = 0; float px, py; // initialize a new Ball instance Ball(float inx, float iny, float inr) { x = inx; y = iny; r = inr; } // reset a Ball after it has ben deactivated void reset() { x = 0; y = 0; yspeed = 0; xspeed = 0; alive = 0; bouncecount = 0; } // manual color reset void colorreset() { if (y > 0 && redvalue >= 0 && redvalue <= 255) { redvalue = 255 - floor(0.3 * y); } if (y > 0 && bluevalue >= 0 && bluevalue <= 255) { bluevalue = 255-floor(0.4 * y); } if (x > 0 && greenvalue >=0 && greenvalue <= 255) { greenvalue = 255-floor(0.5 * x); } } // attempt to move a ball void move() { // if the ball hits the right edge of the screen if (x > width-25) { xspeed = -1 * xspeed / 1.2; x = width-25; } // if the ball hits the left edge of the screen if (x < 25) { xspeed = -1 * xspeed / 1.2; x = 25; } // if the ball is at the bottom of the screen and moving downwards if (y > height-25 && yspeed > 0) { // if speed is high enough, bounce if (yspeed > 0.5) { yspeed = -1 * yspeed / 2; } // otherwise deactivate this ball else { alive = 2; } } // if the ball is active and moving normally, continue it along it's path if (alive == 1) { px = x; py = y; y = y + yspeed; x = x + xspeed; yspeed = yspeed + gravity; //if (bluevalue > yspeed && bluevalue <= 255) { bluevalue = bluevalue - floor(yspeed); } //if (greenvalue > yspeed && greenvalue <= 255) { greenvalue = greenvalue - floor(yspeed); } if (y > 0 && redvalue >= 0 && redvalue <= 255) { redvalue = 255 - floor(0.3 * y); } if (y > 0 && bluevalue >= 0 && bluevalue <= 255) { bluevalue = 255-floor(0.4 * y); } //if (y > 0 && greenvalue >= 0 && greenvalue <= 255) { greenvalue = 255-floor(0.5 * y); } if (x > 0 && greenvalue >=0 && greenvalue <= 255) { greenvalue = 255-floor(0.5 * x); } fill (redvalue, bluevalue, greenvalue); ellipse (x, y, r, r); } } }