// Dartmouth CS 2, Winter 2009, Chris Bailey-Kellogg // Notes 5 | Sketch 5 // The current coordinates of the ball, starting at the center (in setup) float x, y; // The current color of the ball, and whether it's increasing or decreasing int gray=255, dgray=-1; // The current size of the ball, and how much it's increasing or decreasing float sz=5, dsz=0.1; void setup() { smooth(); noStroke(); background(0); x=width/2; y=height/2; } void draw() { fill(0,3); rect(0,0,width,height); fill(gray); ellipse(x,y,sz,sz); // Move the position by random steps in x and y x += random(-2,2); y += random(-2,2); // Increase/decrease the color, changing direction at the extremes gray += dgray; if (gray == 128) { // Decreased too far; start increasing dgray = 1; } else if (gray == 255) { // Increased too far; start decreasing dgray = -1; } // Increase/decrease the size, changing direction at the extremes sz += dsz; if (sz <= 2 || sz >= 15) dsz = -dsz; }